[2.0.69] on_player_placed_equipment gets cancelled if character is assigned

Bugs which we just recently fixed in our development version and will reach you in the next release.
Honktown
Smart Inserter
Smart Inserter
Posts: 1059
Joined: Thu Oct 03, 2019 7:10 am
Contact:

[2.0.69] on_player_placed_equipment gets cancelled if character is assigned

Post by Honktown »

What you expected:
1) Multiple equipment gets inserted into armor
2) Changing the character during on_player_placed_equipment does not "cancel" the rest of the events

What happens:
1) Multiple equipment gets inserted into armor
2) Changing the character during on_player_placed_equipment "cancels" the rest of the events


Code: Select all

/c
local player = game.player
local surface = player.surface
local character = player.character
character.insert("power-armor-mk2")
character.insert({name = "solar-panel-equipment", count = 10})

local position = character.position
position.x = position.x + 1
character = surface.create_entity{
	name = "character",
	position = position,
	force = player.force
}



local event_trigger_count = 0
script.on_event(defines.events.on_player_placed_equipment, function(event)
	local player = game.get_player(event.player_index)
	event_trigger_count = event_trigger_count + 1
	game.print("event_trigger_count: "..event_trigger_count)
	local cancel_events_bug = true
	if cancel_events_bug then
		player.character = character
	end
end)
Start a new game, run command, open your armor and fast-transfer solar panels. if cancel_events_bug is true, only one event is raised. If false, all 10 get raised.

Why this matters:
Someone was making a mod where the character entity changes, if certain equipment is inserted into armor. This behavior is quite unexpected.
I have mods! I guess!
Link
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4222
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [2.0.69] on_player_placed_equipment gets cancelled if character is assigned

Post by boskid »

Since the event does not contain character that seems like a reasonable expectation. It looks like changing a character causes player's controller to be invalidated and this causes the code that raises those events to stop sending events since that code uses a controller reference to obtain a player and when controller is invalidated then the events are no longer being raised since player cannot be obtained from an invalid controller and a player is a required field of that event.

-- edit:

There is a workaround available: since there are 2 events raised, on_player_placed_equipment and on_equipment_inserted, if a controller gets invalidated then this first event is no longer fires, but you should be able to use the second one since it is still possible to be raised as it does not need a player field inside.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4222
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [2.0.69] on_player_placed_equipment gets cancelled if character is assigned

Post by boskid »

Thanks for the report. Issue is now fixed for the next release.
Post Reply

Return to “Resolved for the next release”