Page 1 of 1

[Solved] on_player_left_game player_index

Posted: Thu Oct 15, 2020 8:26 am
by rookhaven
I'm trying to track player guis in mp with the on_player_left_game player_index parameter which shows as uint in the api.

I get this error when running the following code which looks like it's treating player_index as a table not an integer? Didn't see any bugs on this one - with a 2 second glance. My on_player_joined_game version of the same thing is working fine but it uses event as a parameter.

Code: Select all

Error while running event CityPeeps::on_player_left_game (ID 44)
__CityPeeps__/scripts/civ_logic.lua:89: attempt to concatenate local 'player_index' (a table value)
My on_event code:

Code: Select all

script.on_event({defines.events.on_player_left_game},
function(player_index)
	Civ:player_left(player_index)
end)
My execution:

Code: Select all

function Civ:player_left(player_index)
game.players[1].print("Player Left Index: "..player_index)  -- THIS IS THE LINE THAT ERRORS (89)
	for index, gui in ipairs(self.state.guis) do
		if (gui.state.player_index == player_index) then
			table.remove(self.state.guis, index)         -- THIS LINE NEVER RUNS (which is why I was trying to see the player_index value)
Civ:debug("P1")
		end
	end
Civ:debug("Player Gui Size: "..#self.state.guis)
end
Can someone please tell me how to do this correctly? Thanks.

Edit: I also tried using event.player_index as a parameter but that didn't work either.

Re: on_player_left_game player_index

Posted: Thu Oct 15, 2020 8:32 am
by boskid
90418.png
90418.png (147.32 KiB) Viewed 763 times
Your first attempt failed because first parameter called "player_index" got a table with 3 values inside from which you should extract the "player_index" itself. I guess second attempt failed because you were using "event" name while having the parameter still called the "player_index". Just change the parameter to be called "event" and then use "event.player_index".

Re: on_player_left_game player_index

Posted: Thu Oct 15, 2020 8:37 am
by rookhaven
Ok thank you for the quick reply! Marking solved.