Forum

> > CS2D > Scripts > [LUA] Is this for loop safe?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch [LUA] Is this for loop safe?

3 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt [LUA] Is this for loop safe?

Mami Tomoe
User Off Offline

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

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

Cure Pikachu
User Off Offline

Zitieren
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

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

VADemon
User Off Offline

Zitieren
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.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht