surface.create_entity_from_item(...)

User avatar
Earendel
Factorio Staff
Factorio Staff
Posts: 716
Joined: Sun Nov 23, 2014 11:57 am
Contact:

surface.create_entity_from_item(...)

Post by Earendel »

I would like the ability to build/deploy an entity directly from an item, so that if there is entity data saved in the item it would be carried over to the new entity automatically just as though the entity had been built by a player.

Ideally the function would be similar to surface.create_entity except that instead of specifying a name you specify item = LuaItemStack

The entity I'm struggling with is the spidertron as an item. I can't create a spider that properly matches the item because not all values are readable (as far as I can tell), but even if they were the ability to do this in 1 call would be much better than lengthy code trying to replicate the item placement process in lua.
PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: surface.create_entity_from_item(...)

Post by PFQNiet »

A workaround may be to put the item in a player's cursor and then use LuaPlayer#build_from_cursor() to place it, before restoring the player's cursor to whatever it was before. "Temporarily saving the cursor" could be done by creating a one-slot script inventory, swapping the player's cursor into the script inventory's slot, then putting the item to place in the cursor, building it, and finally swapping it back and destroying the temporary inventory.

It's a hack, but if it all happens in one tick then it's completely invisible to the player.

A simple method to do this would be much better though!
User avatar
Earendel
Factorio Staff
Factorio Staff
Posts: 716
Joined: Sun Nov 23, 2014 11:57 am
Contact:

Re: surface.create_entity_from_item(...)

Post by Earendel »

The player hack is a good idea but has some unwanted side-effects and would stop working if nobody was logged onto the server. My use case is rocket-deployed spidertrons for colonising alien planets that are different surfaces.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14250
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: surface.create_entity_from_item(...)

Post by Rseding91 »

Ok, I added "item" to create_entity for the next release.
If you want to get ahold of me I'm almost always on Discord.
User avatar
Earendel
Factorio Staff
Factorio Staff
Posts: 716
Joined: Sun Nov 23, 2014 11:57 am
Contact:

Re: surface.create_entity_from_item(...)

Post by Earendel »

That's great, thanks.
Post Reply

Return to “Implemented mod requests”