AlfreDCS Posted December 21, 2023 Posted December 21, 2023 Hello! Sorry for my poor English. First of all, I'm pretty new to programming... BUT... I have a strange problem... the following code simply randomly removes objects at the start of a mission. BUT ONLY FULLY WORKS if the "for...end" goes from 1 to 3... otherwise (for example from 1 to 10), the lines that come AFTER the End of the For are NOT EXECUTED... What am I doing wrong? The code: local nroObjeto = 1 local strObjeto = "Static" .. nroObjeto for i=1, 10 do if math.random(1,100) > 50 then Unit.getByName(strObjeto):destroy() trigger.action.outText("Erased".. strObjeto,5) end nroObjeto = nroObjeto + 1 strObjeto = "Static" .. nroObjeto end trigger.action.outText("End Random process...",15) -- NOT EXECUTED WITH for i=1, 10 Unit.getByName("Static7"):destroy() -- NOT EXECUTED WITH for i=1, 10
Solution cfrag Posted December 21, 2023 Solution Posted December 21, 2023 (edited) 1 hour ago, AlfreDCS said: I have a strange problem... You are happily invoking "destroy()" on something that you did not make sure that it exists, inviting an error. If you don't get an error dialog and for some reason some lines in your code aren't executed, make sure that you have enabled the error box, else debugging becomes impossible without checking the logs. Try below simplified code of yours (untested, sorry, on a train). I suspect that some of your units that should be named "Static1" through "Static10" do not exist. ... which brings me to the next point: You call them "Static", but try access them with Unit.getByName(). If whatever you are trying to remove are units, that is fine, just ensure that their name matches. If they are static objects (i.e. NOT units), you need to access them vie StaticObject.getByName() for i=1, 10 do if math.random(1,100) > 50 then local strObjeto = "Static" .. i local o = Unit.getByName(strObjeto) if o then Unit.destroy(o) trigger.action.outText("Erased ".. strObjeto,5) else trigger.action.outText("unit <" .. strObjeto .. "> does not exist", 30) end end end Edited December 21, 2023 by cfrag
AlfreDCS Posted December 21, 2023 Author Posted December 21, 2023 Excellent, cfrag...! My fault! Obviously I was mixing Units with Static Objects... and wanting to interact with what doesn't exist! Your code worked perfectly. Thank you so much
Recommended Posts