Jump to content

What if


Karoffe

Recommended Posts

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

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 by Guest
Link to comment

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...