English [LUA] Is this for loop safe?

3 replies
Goto Page
To the start Previous 1 Next To the start
01.03.19 12:49:17 am
Up
Goo
User
Offline Off
Hello there!

I am wondering if it's possible to run through the list of players (living and all, but for the example I chose living).

But in my head it might break if this happens:

ID = 1 NAME = BOB
ID = 3 NAME = NOT BOB

There are only two values so the for loop will never reach ID 3 would it? Would it?
It's confusing but I realized how much faster it is to NOT use pairs or ipairs: https://springrts.com/wiki/Lua_Performance#TEST_9:_for-loops

Code:
1
2
for id = 1, #player(0, 'tableliving') do
...
01.03.19 12:58:48 am
Up
Cure Pikachu
User
Offline Off
Do something like
Code:
local tl = player(0,"tableliving")
for i = 1, #tl do
     local id = tl[i]
     if player(id,"name") == "BOB" then
     end
end

It's basically like doing this:
Code:
for i, id in pairs(player(0,"tableliving")) do
     if player(id,"name") == "BOB" then
     end
end
IMG:https://i.imgur.com/5UWPaft.png
IMG:https://i.imgur.com/xpsyQRX.png
01.03.19 01:12:52 am
Up
Goo
User
Offline Off
Thanks!
01.03.19 01:49:09 pm
Up
VADemon
User
Offline Off
tables returned by player() are always normal, ordered arrays without empty holes. So there will never be a hole between ID=1 and ID=3.
To the start Previous 1 Next To the start