Page 1 of 1

create_entity not reliably firing?

Posted: Sat May 15, 2021 4:13 am
by vedrit
A user posted about a bug their encountering in my mod, and after some digging, it seems the issue lies with

Code: Select all

game.surfaces[1].create_entity{name, position, direction, force}
If I leave the surface index as 1, then no entity is created*. If I set the index as 2, then an entity is created, but seemingly of another faction (game says I can't mine it)
And it seems that, since this is firing during on_built_entity, I can't use entity.surface or entinty.force (Get a LuaEntity not valid error)

The intent of it is that, my mod has entity tiers that are discreetly replaced when a new tier is researched (tiers have different entity health values)
If anyone has any insight into what's going on and can help me fix this, it'd be much appreciated.


*: Sometimes. In my personal saves, it works fine. In the save that the user provided, it does not work. My mod is the only enabled mod in either case.

Re: create_entity not reliably firing?

Posted: Sat May 15, 2021 4:57 am
by DaveMcW
You should not hard-code the surface number. Get the surface (and the force) from the entity you want to replace.

To prevent LuaEntity not valid error, test entity.valid before accessing any of its properties.

Re: create_entity not reliably firing?

Posted: Sat May 15, 2021 7:48 am
by vedrit
DaveMcW wrote:
Sat May 15, 2021 4:57 am
You should not hard-code the surface number. Get the surface (and the force) from the entity you want to replace.

To prevent LuaEntity not valid error, test entity.valid before accessing any of its properties.
Found out that the issue was that I was deleting the entity after checking if it was valid, but before trying to reference values it held.