[Solved] on_player_left_game player_index

Place to get help with not working mods / modding interface.
rookhaven
Inserter
Inserter
Posts: 22
Joined: Sun May 07, 2017 9:21 pm
Contact:

[Solved] on_player_left_game player_index

Post 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.
Last edited by rookhaven on Thu Oct 15, 2020 8:37 am, edited 1 time in total.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 3023
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: on_player_left_game player_index

Post by boskid »

90418.png
90418.png (147.32 KiB) Viewed 762 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".
rookhaven
Inserter
Inserter
Posts: 22
Joined: Sun May 07, 2017 9:21 pm
Contact:

Re: on_player_left_game player_index

Post by rookhaven »

Ok thank you for the quick reply! Marking solved.
Post Reply

Return to “Modding help”