on_player_created bug?

Place to get help with not working mods / modding interface.
trionicb
Burner Inserter
Burner Inserter
Posts: 13
Joined: Fri Mar 23, 2018 1:27 am
Contact:

on_player_created bug?

Post by trionicb »

With the release of v1.0 I've got a strange bug in my mod

Code: Select all

script.on_event(defines.events.on_player_created, function(event)
	local items
	local armor

	items = {
		{"power-armor-mk2", 1},
		{"construction-robot", 50},
	}
	armor = {
		{"fusion-reactor-equipment"},
		{"energy-shield-mk2-equipment"},
	}

	local player = game.players[event.player_index]
	for i, v in pairs(items) do
		player.insert{name = v[1], count = v[2]}
	end

	local grid = player.get_inventory(defines.inventory.character_armor)[1].grid
	for  i, v in pairs(armor) do
		grid.put({name = v[1]})
	end

end)
I get a "attempt to index a nil value" when calling get_inventory. This only happens in freeplay when the starting animation is running so it's something related to event of the animation, if I play a scenario there is no problem. I've checked the release notes but I can't find reference to what event I should be triggering on.

Thanks for the help
Bilka
Factorio Staff
Factorio Staff
Posts: 3310
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: on_player_created bug?

Post by Bilka »

During the cutscene the player does not have a character. You are trying to get a character inventory, so that does not work. I'd recommed to use the remote interface of the freeplay scenario to add extra starting item to players.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
trionicb
Burner Inserter
Burner Inserter
Posts: 13
Joined: Fri Mar 23, 2018 1:27 am
Contact:

Re: on_player_created bug?

Post by trionicb »

Wouldn't that be a bug, calling the event when the character is not actually created? Could you point me to the API docs covering the remote interfaces. All I found is this https://lua-api.factorio.com/latest/LuaRemote.html
User avatar
ptx0
Smart Inserter
Smart Inserter
Posts: 1507
Joined: Wed Jan 01, 2020 7:16 pm
Contact:

Re: on_player_created bug?

Post by ptx0 »

Bilka wrote: Fri Aug 14, 2020 1:48 pm During the cutscene the player does not have a character. You are trying to get a character inventory, so that does not work. I'd recommed to use the remote interface of the freeplay scenario to add extra starting item to players.
LOL way to break a shitload of mods just as it's released, isn't that exactly what you didn't want?
User avatar
ytsejam
Long Handed Inserter
Long Handed Inserter
Posts: 59
Joined: Wed Jun 29, 2016 2:14 am
Contact:

Re: on_player_created bug?

Post by ytsejam »

ptx0 wrote: Fri Aug 14, 2020 3:43 pm
Bilka wrote: Fri Aug 14, 2020 1:48 pm During the cutscene the player does not have a character. You are trying to get a character inventory, so that does not work. I'd recommed to use the remote interface of the freeplay scenario to add extra starting item to players.
LOL way to break a shitload of mods just as it's released, isn't that exactly what you didn't want?
OMG, mods made in beta/experimental are broken in 1.0...

[Moderated by Koub. Please be civil here.]
Rseding91
Factorio Staff
Factorio Staff
Posts: 14594
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_player_created bug?

Post by Rseding91 »

trionicb wrote: Fri Aug 14, 2020 3:20 pm Wouldn't that be a bug, calling the event when the character is not actually created?
The event is "player created"; not character created. In fact the character has been created just the player is in a cutscene. The player could also be in the god controller or the map editor or be a spectator; none of those have character entities. You can't assume the player controller is anything :) You need to check what it is before trying to use it: https://lua-api.factorio.com/latest/Lua ... oller_type

As for the freeplay scenario stuff; that also depends if the freeplay scenario is being used - which isn't guaranteed. It could be other scenarios which have no remote interface. I don't know if there's any documentation on what remote interfaces freeplay provides.
If you want to get ahold of me I'm almost always on Discord.
GTX2GvO
Manual Inserter
Manual Inserter
Posts: 1
Joined: Thu Mar 14, 2019 9:06 pm
Contact:

Re: on_player_created bug?

Post by GTX2GvO »

Seeing the "player created" event no longer gives access to the character inventory during freeplay, due to the cutscene.

What would allow access to the proper character inventory?

This little change basically broke just about every quick start mod out there relying on the character access during this event.

It's just to understand things myself, I don't make these mods. (I play with them :P )
trionicb
Burner Inserter
Burner Inserter
Posts: 13
Joined: Fri Mar 23, 2018 1:27 am
Contact:

Re: on_player_created bug?

Post by trionicb »

So I put in some game.print statements to see what was happening. All this is in FreePlay, everything works in scenarios. While the intro is running controller.type = 6 (which is probably defines.controllers.cutscene from the docs). So at some point that controller type has to change to defines.controllers.character, is there an event I could trap? on_player_changed_force doesn't seem to be the right one. Also it's worth noting that it's only when the tries to add stuff to the grid that it crashes, it has no problem adding items to the inventory.
roliver
Manual Inserter
Manual Inserter
Posts: 2
Joined: Fri Aug 14, 2020 11:44 pm
Contact:

Re: on_player_created bug?

Post by roliver »

In the mod constution robots a different way of filling in the 10 robots is chosen. Unfortunately, I haven't fully understood that yet, but with the mod, the 10 bot can also be used in freeplay
Peppe
Fast Inserter
Fast Inserter
Posts: 223
Joined: Fri Nov 28, 2014 6:48 pm
Contact:

Re: on_player_created bug?

Post by Peppe »

GTX2GvO wrote: Fri Aug 14, 2020 6:26 pm Seeing the "player created" event no longer gives access to the character inventory during freeplay, due to the cutscene.

What would allow access to the proper character inventory?

This little change basically broke just about every quick start mod out there relying on the character access during this event.

It's just to understand things myself, I don't make these mods. (I play with them :P )
defines.events.on_cutscene_cancelled can replace on_player_created.

Don't know if there are other cutscenes in the map though, so if another cutscene triggers the mod may run again. Or if other mods can create cutscenes.


I think the way Bilka suggested is to use the interface built into the freeplay.lua map file:

Code: Select all

local freeplay_interface =
{
  get_created_items = function()
    return global.created_items
  end,
  set_created_items = function(map)
    global.created_items = map or error("Remote call parameter to freeplay set created items can't be nil.")
  end,
  get_respawn_items = function()
    return global.respawn_items
  end,
  set_respawn_items = function(map)
    global.respawn_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.")
  end,
  set_skip_intro = function(bool)
    global.skip_intro = bool
  end,
  set_chart_distance = function(value)
    global.chart_distance = tonumber(value) or error("Remote call parameter to freeplay set chart distance must be a number")
  end,
  set_disable_crashsite = function(bool)
    global.disable_crashsite = bool
  end,
  get_ship_items = function()
    return global.crashed_ship_items
  end,
  set_ship_items = function(map)
    global.crashed_ship_items = map or error("Remote call parameter to freeplay set created items can't be nil.")
  end,
  get_debris_items = function()
    return global.crashed_debris_items
  end,
  set_debris_items = function(map)
    global.crashed_debris_items = map or error("Remote call parameter to freeplay set respawn items can't be nil.")
  end
}

if not remote.interfaces["freeplay"] then
  remote.add_interface("freeplay", freeplay_interface)
end


Then i guess we are supposed to move all the fast start items to either load with the ship/debris or player inventory:

Code: Select all

script.on_init(function()
	
	remote.call("freeplay", "set_created_items", {coal=50})
	remote.call("freeplay", "set_ship_items", {coal=50})	
	remote.call("freeplay", "set_debris_items", {coal=50})
	
end)
Or disable the crashsite:

Code: Select all

script.on_init(function()
	
	-- removed crashsite and cutscene start, so on_player_created inventory safe
	remote.call("freeplay", "set_disable_crashsite", true)	
	
	-- Skips popup message to press tab to start playing
	remote.call("freeplay", "set_skip_intro", true)
end)
trionicb
Burner Inserter
Burner Inserter
Posts: 13
Joined: Fri Mar 23, 2018 1:27 am
Contact:

Re: on_player_created bug?

Post by trionicb »

Thanks Peppe,

defines.events.on_cutscene_cancelled works. I've added a check to the player create event so that it ensures controller is in the right state.

Cheers
Peppe
Fast Inserter
Fast Inserter
Posts: 223
Joined: Fri Nov 28, 2014 6:48 pm
Contact:

Re: on_player_created bug?

Post by Peppe »

Rseding91 wrote: Fri Aug 14, 2020 5:52 pm
The event is "player created"; not character created. In fact the character has been created just the player is in a cutscene. The player could also be in the god controller or the map editor or be a spectator; none of those have character entities. You can't assume the player controller is anything :) You need to check what it is before trying to use it: https://lua-api.factorio.com/latest/Lua ... oller_type

As for the freeplay scenario stuff; that also depends if the freeplay scenario is being used - which isn't guaranteed. It could be other scenarios which have no remote interface. I don't know if there's any documentation on what remote interfaces freeplay provides.
There is one on_character event: on_character_corpse_expired.

Every other event around characters / players is on_player_xx?

on_player_died -- what actually died then?
the character controller the player was in? or the player?

on_player_main_inventory_changed? Players don't always have an inventory. So is this only when a character's inventory changes while a player is inside that character?
trionicb
Burner Inserter
Burner Inserter
Posts: 13
Joined: Fri Mar 23, 2018 1:27 am
Contact:

Re: on_player_created bug?

Post by trionicb »

I always double check the controller_type after trapping the event to ensure the player is in the right state before adding the items. I'll wait for feedback from users but so far those playing scenario and freeplay haven't reported issues.
Schallfalke
Fast Inserter
Fast Inserter
Posts: 162
Joined: Sun Oct 28, 2018 7:57 am
Contact:

Re: on_player_created bug?

Post by Schallfalke »

This leads me to a new question though:
How to make it work with both freeplay and sandbox (and other scenarios)?
Does it mean I need to duplicate my insertion code, so keeping using on_player_created event for sandbox, and using the dedicated remote interface of freeplay or on_cutscene_cancelled event for freeplay?

Any suggestions on a better way of coding here, without the need to run the inventory insertion twice?
trionicb
Burner Inserter
Burner Inserter
Posts: 13
Joined: Fri Mar 23, 2018 1:27 am
Contact:

Re: on_player_created bug?

Post by trionicb »

Well without a character you can't have armor, thus no grid, thus my mod will not work.
User avatar
morsk
Fast Inserter
Fast Inserter
Posts: 145
Joined: Fri Dec 15, 2017 1:00 am
Contact:

Re: on_player_created bug?

Post by morsk »

Schallfalke wrote: Mon Aug 17, 2020 12:41 pm This leads me to a new question though:
How to make it work with both freeplay and sandbox (and other scenarios)?
Does it mean I need to duplicate my insertion code, so keeping using on_player_created event for sandbox, and using the dedicated remote interface of freeplay or on_cutscene_cancelled event for freeplay?
Duplicate it or put it in a function and call it from two places. But it only has to run once:

on_player_created:
- If player.controller_type isn't a cutscene, do the change immediately.
- If it's a cutscene, add the player's id to a table.

on_cutscene_cancelled:
- If the player's id is in the table, remove it, and do the change.
Schallfalke
Fast Inserter
Fast Inserter
Posts: 162
Joined: Sun Oct 28, 2018 7:57 am
Contact:

Re: on_player_created bug?

Post by Schallfalke »

morsk wrote: Mon Aug 17, 2020 8:18 pm
Schallfalke wrote: Mon Aug 17, 2020 12:41 pm This leads me to a new question though:
How to make it work with both freeplay and sandbox (and other scenarios)?
Does it mean I need to duplicate my insertion code, so keeping using on_player_created event for sandbox, and using the dedicated remote interface of freeplay or on_cutscene_cancelled event for freeplay?
Duplicate it or put it in a function and call it from two places. But it only has to run once:

on_player_created:
- If player.controller_type isn't a cutscene, do the change immediately.
- If it's a cutscene, add the player's id to a table.

on_cutscene_cancelled:
- If the player's id is in the table, remove it, and do the change.
Right, found out it has to be called from both events to work correctly in both sandbox and freeplay modes.

Eventually, I just used existence of remote interfaces "freeplay" as condition within on_cutscene_cancelled:

Code: Select all

  if remote.interfaces["freeplay"] then   -- Added for 1.0 freeplay cutscene
    set_inventory(e)
  end
I did not use player's id table, as when this event is called in freeplay mode, I should already grab all players already joined the game.
(Unless a player really joined and ninja-exited the game before the cutscene even ended, and rejoined the game after that... I guess these ninjas do not need my extra inventory items.) 8-)
(EDIT: Not sure about the joined offline players when the event happens. Perhaps the event will also start the call set_inventory(e) for offline players, IDK... Do not have the time to really test it out.)
Pi-C
Smart Inserter
Smart Inserter
Posts: 1742
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: on_player_created bug?

Post by Pi-C »

Peppe wrote: Sat Aug 15, 2020 1:12 am defines.events.on_cutscene_cancelled can replace on_player_created.
Is that something official? I know that on_cutscene_cancelled triggers both when I actively cancel the cutscene by pressing TAB and when I let it run until it ends of its own. But:
Rseding91 wrote: Fri Aug 14, 2020 5:52 pm As for the freeplay scenario stuff; that also depends if the freeplay scenario is being used - which isn't guaranteed. It could be other scenarios …
Listening to on_cutscene_cancelled will definitely work as far as the vanilla cutscene is concerned. But why? Is it because the end of the cutscene will always trigger the event, or could it be that the message dialog you get on starting a new game sends the equivalent of "TAB was pressed" to the game? This could make a difference if another scenario was loaded that begins with a cutscene but doesn't show a dialog at the end of the cutscene.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Post Reply

Return to “Modding help”