Page 1 of 1

[1.1.72] on_trigger_created_entity has no last_user when player.character is destroyed before the entity is created

Posted: Tue Nov 15, 2022 6:48 pm
by Wannie
When a projectile is created via LuaSurface.create_entity() with the parameter “player” set, but the player.character is destroyed before the projectile has time to trigger the “on_trigger_created_entity” event, the variable last_user in the created event.entity is nil.

The issue can be reproduced by using a projectile with spawns an entity and has the property “trigger_created_entity” set to true. If the projectile is created via the method create_entity() of the LuaSurface right before the player.character is destroyed, the entity in the event “on_trigger_created_entity() will have no last_user.

I was able to reproduce this issue by using my mod “Combat Robots Overhaul” multiple times. Using one of the new combat units, I spawned one unit via capsule and immediately destroyed my character while the projectile was flying. (Since there is only a limited timeframe to achieve this, I used the mod “speed control” to slow down the sim speed) When the entity spawned, it had no last_user set which caused the mod to crash.

The reason why I think this is a bug, is because of the parameter “player” for the method create_entity() of the class LuaSurface. This parameter has the type “playerIdentification” which includes “LuaPlayer”. Since LuaPlayer is not the same prototype as the player character (which is LuaEntity), the destruction of the character should have no influence over last_user. Instead, I expect the parameter last_user to correctly refer to the LuaPlayer Class of a player.

If I’m wrong feel free to ignore this thread. If you need more information or would like a save to replicate the issue let me know.

Thanks for all the amazing work you do.

Re: [1.1.72] on_trigger_created_entity has no last_user when player.character is destroyed before the entity is created

Posted: Mon Dec 12, 2022 4:04 pm
by Rseding91
Thanks for the report; when you say you are destroying the character and passing the player in create_entity in what order are you doing that?
  • local player = game.players[...]
  • player.character.destroy()
  • surface.create_entity{player = player, ...}
Or:
  • game.players[...].character.destroy()
  • surface.create_entity{player = game.players[...], ...}

Re: [1.1.72] on_trigger_created_entity has no last_user when player.character is destroyed before the entity is created

Posted: Mon Mar 20, 2023 5:19 pm
by Wannie
Sorry for the late reply. Its neither one of these. I try to clarify the situation more.

1. First the player is read via local player = game.players[...]
2. Second, a capsule is created via command surface.create_entity{ player = player }. This capsule has a projectile asscociated to it. The projectile has the target_effect "create_entity" and also the variable "trigger_created_entity" set to true.
3. While the animation of the projectile plays, the player which has "thrown" the capsule dies. (Or is destroyed via command player.character.destroy() )
4. When the event "on_trigger_created_entity" fires, the variable "last_user" in event.entity is empty.

So the correct order would be:
1. local player = game.players[..]
2. surface.create_entity{ player = player]
3. player.character_destroy()

The last action needs to be executed while the projectile animation of the capusle is playing.

Please let me know if you need further clarification.

Re: [1.1.72] on_trigger_created_entity has no last_user when player.character is destroyed before the entity is created

Posted: Mon Mar 20, 2023 6:34 pm
by Rseding91
Thanks for the steps. In that case; unfortunately that's just how it works. The projectile doesn't link to a player but an entity and if the entity it originated from is destroyed it acts as if it came from no entity (no player).

Re: [1.1.72] on_trigger_created_entity has no last_user when player.character is destroyed before the entity is created

Posted: Mon Mar 20, 2023 10:07 pm
by Wannie
Thanks for the clarification. I assume its not possible to change the projectile so it can also link to "LuaPlayer" or "playeridentification"? Because according to the API documentation, both the parameter "player" in surface.create_entity() and the attribute "last_user" of the class "luaEntity" refer to the class LuaPlayer.

So I assumed this attribute would be passed from the parameter to the projectile to the created entity.