Jump to content

loop


Recommended Posts

Well, here is a small example on how a numeric for loop works.

for index=0,10 do 
    print( "This number is " .. index .. "." ) 
end 

It repeats the contents of that for loop 10 times, as 0,10 means from 0 to 10. That for loop will print the following:

This number is 0. 
This number is 1. 
This number is 2. 
This number is 3. 
This number is 4. 
This number is 5. 
This number is 6. 
This number is 7. 
This number is 8. 
This number is 9. 
This number is 10. 

Now, there is also a generic for loop, which is based on doing something to each and every division of a table. An example of a generic for loop could be the following:

local animals = { 
    cat = " says meow!", 
    dog = " says woof!", 
    car = " says wroom wroom!" 
} 
  
for key,value in pairs( animals ) do 
    print( key .. value ) 
end 

It repeats the contents of that for loop as well, but this time it doesn't do it between numbers, but between the amount of table divisions (for cat, dog and car).

So this will print the following:

car says wroom wroom! 
dog says woof! 
cat says meow! 

Now, if you had a table inside of a table division, you would have to make another for loop for that table, like the following:

local animals = { 
    cats = { 
        male = { 
            "Robert", 
            "Daniel" 
        }, 
        female = { 
            "Holly", 
            "Britney" 
        } 
    }, 
    dogs = { 
        male = { 
            "Jack", 
            "Patrick" 
        }, 
        female = { 
            "Emma", 
            "Caitlin" 
        } 
    } 
} 
  
for category,genders in pairs( animals ) do 
    print( "My " .. category .. ":" ) 
    for gender,animal in pairs( genders ) do 
        for _,name in ipairs( animal ) do 
            print( " " .. name .. " is a " .. gender ) 
        end 
    end 
end 

And that would print the following:

My dogs: 
 Jack is a male 
 Patrick is a male 
 Emma is a female 
 Caitlin is a female 
My cats: 
 Robert is a male 
 Daniel is a male 
 Holly is a female 
 Britney is a female 

When you're handling generic for loops with just a number key (0, 1, 2...) and they don't change or get deleted, you should be using index-value pairs (ipairs) instead of key-value pairs (pairs). The big difference being, that index-value relays on a properly formed, numerical list of contents - while key-value relays on just simply everything without any type of forming or so. It is good to know the difference and when to use either. The main rule is that you use pairs for string keys, and ipairs for numerical keys - again, there are exceptions, such as if the key is suddenly removed, which will mess up the order and will return a nil, or a partial table without the rest.

Hopefully this helped you out.

Edited by Guest
Link to comment

You'll probably use it because it eliminates redundancy. myonlake's example is good. You can also use it to trigger certain functions on numerous objects instead of having to call the functions on each object individually. It helps to give structure to your code and saves space.

Link to comment

big thx to you myonlake for your effort. i think i got it 50%, can you explain what loop do or why the loop is needed in this script ?

addEventHandler( "onClientRender", root, 
        function( ) 
            local peds = getElementsByType("ped") 
            if (#peds > 0) then 
                local x,y,z = getElementPosition(localPlayer) 
                for k,ped in ipairs(peds) do 
                    if (getElementData(ped, "robberped")) then 
                        local pX,pY,pZ = getElementPosition(ped) --ped's position 
                        local tX,tY,tZ = pX,pY,pZ+1 --text's position 
                        local maxDistance = 30 
                        local distance = getDistanceBetweenPoints3D(x, y, z, tX, tY, tZ) 
                        if (distance <= maxDistance) then 
                            local wX, wY = getScreenFromWorldPosition(tX, tY, tZ) 
                            if (wX and wY) then 
                                dxDrawText("Robber Job", wX + 2, wY + 2, _, _, tocolor(0, 0, 0, 200), 2, "arial", "center", "center") 
                                dxDrawText("Robber Job", wX, wY, _, _, tocolor(0, 255, 0, 200), 2, "arial", "center", "center") 
                            end 
                        end 
                    end 
                end 
            end 
        end 
    ) 
  
 
Link to comment

Right there.

local peds = getElementsByType( "ped" ) 
for _,ped in ipairs( peds ) do 
    -- ... 
end 

It is used to repeat the contents of that loop to each and every pedestrian on the server. It is useful for nametags, scoreboards, and such.

Link to comment

The function 'getElementsByType' returns a table of current elements. You can chcek how many elements are in table by adding hash (#) before table. For example.

local players = getElementsByType("player") 
outputChatBox(#players) 

The chatbox will output how many players are in the server.

Link to comment
The function 'getElementsByType' returns a table of current elements. You can chcek how many elements are in table by adding hash (#) before table. For example.
local players = getElementsByType("player") 
outputChatBox(#players) 

The chatbox will output how many players are in the server.

im not asking about this .. im asking about loop thingy, i just know this function ( getElementsbyType ) and know this hash thingy. and thx anyway

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...