create_entity not reliably firing?

Place to get help with not working mods / modding interface.
vedrit
Filter Inserter
Filter Inserter
Posts: 293
Joined: Sun Aug 03, 2014 2:25 am
Contact:

create_entity not reliably firing?

Post 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.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3716
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: create_entity not reliably firing?

Post 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.
vedrit
Filter Inserter
Filter Inserter
Posts: 293
Joined: Sun Aug 03, 2014 2:25 am
Contact:

Re: create_entity not reliably firing?

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

Return to “Modding help”