Hell-Mate Posted March 3, 2014 Share Posted March 3, 2014 Hellp community,, i just heared alot about this loop thingy. i want anyone to tell me what is it and where it can be used and why .. Thanks Regards,, Link to comment
Moderators Citizen Posted March 3, 2014 Moderators Share Posted March 3, 2014 The Lua loops ? http://lua.gts-stolberg.de/en/schleifen.php Mainly used to repeat a task with different args that can be defined with the index (current loop number). Also used to iterate over a table. Link to comment
Hell-Mate Posted March 3, 2014 Author Share Posted March 3, 2014 Well i didnt understand, can you give me an example ? and put a note for every line ? --note Link to comment
myonlake Posted March 3, 2014 Share Posted March 3, 2014 (edited) 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 March 3, 2014 by Guest Link to comment
Solstice. Posted March 3, 2014 Share Posted March 3, 2014 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
myonlake Posted March 3, 2014 Share Posted March 3, 2014 Also in addition, keep in mind that the multi-dimensional table example is not very good. For-looping through each dimension is not efficient and you should totally go for another approach on that. Link to comment
Hell-Mate Posted March 3, 2014 Author Share Posted March 3, 2014 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
myonlake Posted March 3, 2014 Share Posted March 3, 2014 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
WhoAmI Posted March 3, 2014 Share Posted March 3, 2014 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
Hell-Mate Posted March 3, 2014 Author Share Posted March 3, 2014 can you explain me easier way ? well, consider that you are learning a child. btw my english is not professional so try to use an easier words to make it more easier to me to understand @myonlake Link to comment
Hell-Mate Posted March 3, 2014 Author Share Posted March 3, 2014 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
myonlake Posted March 3, 2014 Share Posted March 3, 2014 getElementsByType returns a table containing all elements for a specific type. In that script it looks for all pedestrians and draws a dxDrawText for each of them. Link to comment
Hell-Mate Posted March 3, 2014 Author Share Posted March 3, 2014 big thanks and best regards to you myonlake, you really helped me thanks, got it 100%. 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