Karoffe Posted February 1, 2015 Share Posted February 1, 2015 What if I want to use break inside a loop that got another loop, which loop will be broken ? And what if I want to break both loops ? here is an example code function theLoop() for k, ped in ipairs(getElementsByType("ped")) do for i=1, 16 do if getElementData(ped, "orderNumber") == i then if getElementHealth(ped) < 20 then outputChatBox("Found a ped with lower than 20 hp") break end end end end end For example in that code which loop will be broken the numeric one or the table loop ? Also if only one breaks, where should I place the other break to break the other loop (only if we found a ped with lower than 20hp).. Link to comment
Gallardo9944 Posted February 1, 2015 Share Posted February 1, 2015 (edited) Doing break in the last loop will stop the last loop (for i=1, 16...). If you want to stop both, you can simply use return - it will end the entire function and of course all the loops. If you want to stop both, but continue the function, you can try something like this: function theLoop() for k, ped in ipairs(getElementsByType("ped")) do local stop = false -- don't stop by default for i=1, 16 do if getElementData(ped, "orderNumber") == i then if getElementHealth(ped) < 20 then outputChatBox("Found a ped with lower than 20 hp") stop = true -- specify we want to end this break end end end if stop then break end -- stop the loop if the inner one stopped end end I'm not sure if there are other methods, cause i'm unable to test that at the moment. Edited February 1, 2015 by Guest Link to comment
TAPL Posted February 1, 2015 Share Posted February 1, 2015 In your example it will break the numeric loop only. Use return if you want to break both loop (whole function will stop also). If you want to break both loop without stopping the whole function you can use variable. function theLoop() local brk = false for k, ped in ipairs(getElementsByType("ped")) do for i=1, 16 do if getElementData(ped, "orderNumber") == i then if getElementHealth(ped) < 20 then outputChatBox("Found a ped with lower than 20 hp") brk = true break end end end if brk then break end end -- any code here will still execute if you used break. end function theLoop() local brk = false for k, ped in ipairs(getElementsByType("ped")) do for i=1, 16 do if getElementData(ped, "orderNumber") == i then if getElementHealth(ped) < 20 then outputChatBox("Found a ped with lower than 20 hp") return end end end end -- any code here won't execute if you used return. end Anyway, wouldn't it be easier just to do this? function theLoop() for k, ped in ipairs(getElementsByType("ped")) do if getElementData(ped, "orderNumber") >= 1 and getElementData(ped, "orderNumber") <= 16 then if getElementHealth(ped) < 20 then outputChatBox("Found a ped with lower than 20 hp") break end end end end Link to comment
Karoffe Posted February 1, 2015 Author Share Posted February 1, 2015 Thanks TAPL and Gallardo @TAPL Yes it would be easier, but that was just an example, this isn't my actual code I will have to use the variable since there is more code to be executed in the original script.. Thanks guys Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now