Page 1 of 1

[1.1.59] Events on_player_changed_position and on_player_promoted run before on_player_created

Posted: Sun May 15, 2022 5:17 pm
by GlassBricks
In a mod, I initialize some data in on_player_created (and during on_init), and use it during on_player_changed_position. However, when creating a new save (freeplay scenario), on_player_changed_position was raised before on_player_created, which led to a crash (attempt to index nil, the player data).

on_player_created is usually when mods initialize player related data; so I think it makes sense that instead of the current behavior, it should fire before other player related events.

Furthermore, this issue was hard to catch, as it didn't happen when loading an existing save with the mod (it was handled in on_init instead).

I also found that on_player_promoted also fires early, although I didn't use that in my mod.

Re: [1.1.59] Events on_player_changed_position and on_player_promoted run before on_player_created

Posted: Sun May 29, 2022 3:22 pm
by Rseding91
Do you have any steps to reproduce this?

Re: [1.1.59] Events on_player_changed_position and on_player_promoted run before on_player_created

Posted: Sat Sep 24, 2022 11:15 pm
by GlassBricks
Late reply is better than never!

Still happens in 1.1.69

Apparently, this only happens with the "freeplay" scenario (maybe has something to do with the cutscene?)

Steps to reproduce:
1. Create a mod that uses on_player_created and on_player_changed_position; example control.lua:

Code: Select all

script.on_init(function()
	global.players = {}
end)

script.on_event(defines.events.on_player_created, function(e)
	global.players[e.player_index] = {
		foo = 0,
	}
end)

script.on_event(defines.events.on_player_changed_position, function(e)
    -- this runs BEFORE on_player_created when a new scenario is created
	local player_data = global.players[e.player_index]

	game.print(serpent.block(player_data)) 
end)
2. Create a new game with the "freeplay" scenario
3. on_player_changed_position runs before on_player_created, so it will print "nil"

If it helps, here's the mock info.json I used to test this

Code: Select all

{
  "name": "bug",
  "version": "0.0.0",
  "title": "bug",
  "author": "",
  "factorio_version": "1.1"
}

Re: [1.1.59] Events on_player_changed_position and on_player_promoted run before on_player_created

Posted: Sun Sep 25, 2022 2:04 am
by Rseding91
Thanks for the reproduction steps. I looked into it and it is related to the cutscene. AT least in this case. The general issue is: during the player-created event in freeplay it changes the position of the player which fires the player-changed-position event. This can happen with any combination of mods provided any mod registered before yours moves the player.