surface.create_entity throwing strange errors when fast replacing a Tank
Posted: Fri Dec 12, 2025 12:19 pm
currently trying to get the following code to work:
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: trunk_inventory = entity.trunk_inventory: 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: 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.
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,
}
endBelow are screenshots of the errors the game produces when the specified lines are uncommented
grid = entity.grid: trunk_inventory = entity.trunk_inventory: 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: 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.