surface.create_entity throwing strange errors when fast replacing a Tank

Place to get help with not working mods / modding interface.
guy smiley
Inserter
Inserter
Posts: 24
Joined: Mon Aug 21, 2017 7:40 am
Contact:

surface.create_entity throwing strange errors when fast replacing a Tank

Post by guy smiley »

currently trying to get the following code to work:

Code: Select all

if entity.type == 'car' then
		entity.surface.create_entity{
			name = buffed_name,
			position = entity.position,
			quality = entity.quality,
			force = entity.force_index,
			snap_to_grid = false,
			fast_replace = true,
			spill = false,
			raise_built = true,
			orientation = entity.orientation,
--			grid = entity.grid,
--			trunk_inventory = entity.trunk_inventory,
--			enable_logistics_while_moving = entity.enable_logistics_while_moving,
--			ammo_inventory = entity.ammo_inventory,
--			request_filters = entity.request_filters, -- not needed cos the fast replace handles it
		}
	else
		entity.surface.create_entity{
			name = buffed_name,
			position = entity.position,
			direction = entity.direction,
			quality = entity.quality,
			force = entity.force_index,
			snap_to_grid = false,
			fast_replace = true,
			spill = false,
			raise_built = true,
		}
	end
As is, that code functions just fine. It replaces the targeted Tank entity with another Tank-adjacent entity with different properties. All good, so far. The problems come from the fact that various of the Tank's specifics are not carried over. Things like the contents of the Tank's equipment grid (grid), any filter settings that were applied to the Tank's inventory slots (trunk_inventory), are not applied. Uncommenting the lines in the code above for 'grid' and 'trunk_inventory', either or both, produces errors that seem to make no sense. The same is also true for the other currently commented lines regarding 'enable_logistics_while_moving', 'ammo_inventory' and 'request_filters', but those are not as relevant to my specific purposes so I'm not as worried about them for now.

Below are screenshots of the errors the game produces when the specified lines are uncommented

grid = entity.grid:
grid error.PNG
grid error.PNG (22.66 KiB) Viewed 343 times
trunk_inventory = entity.trunk_inventory:
trunk_inventory error.PNG
trunk_inventory error.PNG (21.59 KiB) Viewed 343 times
The other three lines produce basically the same error as the trunk_inventory line, so I'll skip them.

So, what's the problem here? Aside from the 'grid' error maybe suggesting that grid is a protected keyword or something similar, the other errors are basically saying "that data doesn't exist for this entity type", which is not the case.

I included the if statement at the start to demonstrate that the entity I'm replacing definitely is a 'car' type, and this screenshot below of the modding API page for surface.create_entity:
please excuse the shoddy crossing out of my bookmarks bar, it includes some personal information.PNG
please excuse the shoddy crossing out of my bookmarks bar, it includes some personal information.PNG (37.92 KiB) Viewed 343 times
From this we can see that, yes, 'car' type entities (which the Tank is) DO have those data structures. At least, I think they do. They definitely do have them as options that can be specified when being created. And the entity I am replacing for these tests has equipment grid items, and has inventory filters specified, so neither of those should be empty data fields.

But, maybe I dunno anything about anything (very possible). Maybe I'm completely confused and turned around, and maybe the errors I'm getting are totally logical and sound. Regardless, I am quite stumped as to why my code is breaking and could use some help.
robot256
Smart Inserter
Smart Inserter
Posts: 1319
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: surface.create_entity throwing strange errors when fast replacing a Tank

Post by robot256 »

FYI, it is not possible to directly reassign a LuaEquipmentGrid or LuaInventory object from one entity to another, i.e. change the owner of an existing object. The contents of the old one must be copied somehow into the new one that is automatically created for the new entity.

The error messages are telling you exactly what you need to know. You must read the documentation for LuaSurface::create_entity() carefully. Each parameter specifies what type of data it accepts and what format it should be in. Also pay close attention to the LuaEntity documentation which shows exactly what you can request from the existing LuaEntity object.

The type of value you must pass for the "grid" parameter is shown as "array[BlueprintEquipment]" where each element of the array is a nested table of the form BlueprintEquipment. It will not accept the LuaEquipmentGrid object reference returned by LuaEntity::grid. You will have to write code, or find a library module, that serializes the old grid into the correct form of array.

SImilarly, the type of value you must pass for the two inventory parameters is shown as "BlueprintInventoryWithFilters", which is a nested table of the form BlueprintInventoryWithFilters. Again, you must write or find code to serialize the contents of the original LuaInventory object into the correct format of table. To access existing entity's trunk inventory, you need to use "entity.get_inventory(defines.inventory.car_trunk)", then use the various function calls available in LuaInventory to retrieve the values you need to populate the serialized table.

The only thing I'm confused about is enable_logistics_while_moving, because that does exist in LuaEntity and is a boolean in both places.
My mods: Multiple Unit Train Control, RGB Pipes, Shipping Containers, Rocket Log, Smart Artillery Wagons.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
guy smiley
Inserter
Inserter
Posts: 24
Joined: Mon Aug 21, 2017 7:40 am
Contact:

Re: surface.create_entity throwing strange errors when fast replacing a Tank

Post by guy smiley »

Ah, I see. I hadn't actually been looking at LuaEntity at all, guess I was kinda approaching this in a lazy way. Thank you for the answer.
guy smiley
Inserter
Inserter
Posts: 24
Joined: Mon Aug 21, 2017 7:40 am
Contact:

Re: surface.create_entity throwing strange errors when fast replacing a Tank

Post by guy smiley »

Yup, all sorted now, tyvm.
robot256
Smart Inserter
Smart Inserter
Posts: 1319
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: surface.create_entity throwing strange errors when fast replacing a Tank

Post by robot256 »

guy smiley wrote: Sat Dec 13, 2025 5:58 am Yup, all sorted now, tyvm.
Glad to hear it. The documentation is the best part of Factorio's API, if you ignore it you are missing out.
My mods: Multiple Unit Train Control, RGB Pipes, Shipping Containers, Rocket Log, Smart Artillery Wagons.
Maintainer of Auto Deconstruct, Cargo Ships, Vehicle Wagon, Honk, Shortwave.
Post Reply

Return to “Modding help”