Forum

> > CS2D > Scripts > [LUA] Is this for loop safe?
Forums overviewCS2D overview Scripts overviewLog in to reply

English [LUA] Is this for loop safe?

3 replies
To the start Previous 1 Next To the start

old [LUA] Is this for loop safe?

Mami Tomoe
User Off Offline

Quote
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

1
2
for id = 1, #player(0, 'tableliving') do
...

old Re: [LUA] Is this for loop safe?

Cure Pikachu
User Off Offline

Quote
Do something like
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:
for i, id in pairs(player(0,"tableliving")) do
	if player(id,"name") == "BOB" then
	end
end

old Re: [LUA] Is this for loop safe?

VADemon
User Off Offline

Quote
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
Log in to reply Scripts overviewCS2D overviewForums overview