[1.1.59] Events on_player_changed_position and on_player_promoted run before on_player_created

We are aware of them, but they have low priority. We have more important things to do. They go here in order not to take space in the main bug thread list.
Post Reply
GlassBricks
Inserter
Inserter
Posts: 25
Joined: Fri Jun 11, 2021 5:20 pm
Contact:

[1.1.59] Events on_player_changed_position and on_player_promoted run before on_player_created

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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13198
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post by Rseding91 »

Do you have any steps to reproduce this?
If you want to get ahold of me I'm almost always on Discord.

GlassBricks
Inserter
Inserter
Posts: 25
Joined: Fri Jun 11, 2021 5:20 pm
Contact:

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

Post 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"
}

Rseding91
Factorio Staff
Factorio Staff
Posts: 13198
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Minor issues”