• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Doubt about For Loop

Yan18

Member
Joined
Jun 14, 2014
Messages
104
Solutions
3
Reaction score
17
I have a doubt with _(underscore) in the Lua for loop. Follow an example below:

Code:
function onUse(cid)
    for _, pos in pairs(tile_pos) do
        pos.stackpos = 255
        players = getThingfromPos(pos)
        
    if isPlayer(players.uid) then
            doTeleportThing(players.uid, to_pos, false)
        end
    end
    return true
end

What do _(underscore) in a for loop?
 
As you can check here: Loops (https://stigmax.gitbook.io/lua-guide/fundamentals-1/loops)
Lua arrays are key indexed so while you using for it will save two variables
one with the key position and the second with the actual key value, so using underscore in any of those two params its just because you are not going to use it, you can always save that var even if you are not using it.
 
Solution
As @StreamSide; mentioned, _ is a key in your loop.
To elaborate more, in Pythonic languages it has few purposes, but in Lua directly, it is throwaway variable, something like "I don't care it's value".
 
As you can check here: Loops (https://stigmax.gitbook.io/lua-guide/fundamentals-1/loops)
Lua arrays are key indexed so while you using for it will save two variables
one with the key position and the second with the actual key value, so using underscore in any of those two params its just because you are not going to use it, you can always save that var even if you are not using it.
Thanks bro, I think I understand now haha.

As @StreamSide; mentioned, _ is a key in your loop.
To elaborate more, in Pythonic languages it has few purposes, but in Lua directly, it is throwaway variable, something like "I don't care it's value".
Thanks bro, then would be as I don't import with the index, just the value, alright?
 
Back
Top