Page 1 of 1

surface.create_entity_from_item(...)

Posted: Sun Dec 13, 2020 1:19 am
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.

Re: surface.create_entity_from_item(...)

Posted: Sun Dec 13, 2020 8:11 am
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!

Re: surface.create_entity_from_item(...)

Posted: Sun Dec 13, 2020 4:04 pm
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.

Re: surface.create_entity_from_item(...)

Posted: Sun Dec 20, 2020 9:00 pm
by Rseding91
Ok, I added "item" to create_entity for the next release.

Re: surface.create_entity_from_item(...)

Posted: Tue Dec 22, 2020 12:06 am
by Earendel
That's great, thanks.