Page 1 of 1

Creating a ghost of an entity somewhere else

Posted: Tue Sep 03, 2019 10:12 am
by mrvn
I need to clone an existing entity to another surface / position as a ghost so that it retains all its configuration.

What I found is that a LuaItemStack can create and build blueprints like this:

Code: Select all

              stack.create_blueprint{surface = entity.surface,
                                     force = entity.force,
                                     area = {left_top = {x - 0.1, y - 0.1}, right_bottom = {x + 0.1, y + 0.1}},
                                     always_include_tiles = true}
              stack.build_blueprint{surface = slave.surface,
                                    force = slave.force,
                                    position = {x = sy, y = sy},
                                    direction = defines.direction.north,
                                    skip_fog_of_war = false,
                                    by_player = event.player_index}
but how do I get a blank LuaItemStack that's an empty blueprint?

Any other method to clone an entity as ghost?

Re: Creating a ghost of an entity somewhere else

Posted: Tue Sep 03, 2019 10:33 am
by DaveMcW
mrvn wrote: Tue Sep 03, 2019 10:12 ambut how do I get a blank LuaItemStack that's an empty blueprint?
You have to create a real item, then you can manipulate its LuaItemStack.

Code: Select all

local item = surface.create_entity{
  name = "item-on-ground",
  position = {0,0},
  stack = "blueprint",
}
local stack = item.stack

Re: Creating a ghost of an entity somewhere else

Posted: Tue Sep 03, 2019 10:38 am
by eradicator
mrvn wrote: Tue Sep 03, 2019 10:12 am but how do I get a blank LuaItemStack that's an empty blueprint?

Code: Select all

local bp =  nauvis.create_entity{name='item-on-ground',stack='blueprint',position=here}.stack
mrvn wrote: Tue Sep 03, 2019 10:12 am Any other method to clone an entity as ghost?
LuaEntity.clone(), LuaEntity.die(LuaEntity.force)

Re: Creating a ghost of an entity somewhere else

Posted: Tue Sep 03, 2019 10:39 am
by eradicator
DaveMcW wrote: Tue Sep 03, 2019 10:33 am

Code: Select all

local item = surface.create_entity{
  name = "item-on-ground",
  position = {0,0},
  stack = "blueprint",
}
local stack = item.stack
I highly recommend using the actual target position as {0,0} is not guaranteed to exist (i.e. player used a chunk-deletition mod).

Re: Creating a ghost of an entity somewhere else

Posted: Tue Sep 03, 2019 11:12 am
by mrvn
eradicator wrote: Tue Sep 03, 2019 10:38 am
mrvn wrote: Tue Sep 03, 2019 10:12 am but how do I get a blank LuaItemStack that's an empty blueprint?

Code: Select all

local bp =  nauvis.create_entity{name='item-on-ground',stack='blueprint',position=here}.stack
mrvn wrote: Tue Sep 03, 2019 10:12 am Any other method to clone an entity as ghost?
LuaEntity.clone(), LuaEntity.die(LuaEntity.force)
Clone doesn't turn the entity into a ghost, or am I missing something? Or do you mean to create the entity and then kill it so it becomes a ghost?

Re: Creating a ghost of an entity somewhere else

Posted: Tue Sep 03, 2019 11:27 am
by mrvn
eradicator wrote: Tue Sep 03, 2019 10:39 am
DaveMcW wrote: Tue Sep 03, 2019 10:33 am

Code: Select all

local item = surface.create_entity{
  name = "item-on-ground",
  position = {0,0},
  stack = "blueprint",
}
local stack = item.stack
I highly recommend using the actual target position as {0,0} is not guaranteed to exist (i.e. player used a chunk-deletition mod).
Thanks DaveMcW, that works.

Good point about the position. The blueprint is placed on potentially many other surfaces so "actual target position" is a bit murky. But I have a save position on the source surface I can use.

Re: Creating a ghost of an entity somewhere else

Posted: Tue Sep 03, 2019 12:13 pm
by eradicator
mrvn wrote: Tue Sep 03, 2019 11:12 am Or do you mean to create the entity and then kill it so it becomes a ghost?
That's what i wrote. Yes.

Btw calling LuaItemStack.clear() on the bp stack afterwards will also automatically remove the item-on-ground entity. That's why my original one-line version does not keep a handle on the entity.

Re: Creating a ghost of an entity somewhere else

Posted: Tue Sep 03, 2019 3:30 pm
by mrvn
eradicator wrote: Tue Sep 03, 2019 12:13 pm
mrvn wrote: Tue Sep 03, 2019 11:12 am Or do you mean to create the entity and then kill it so it becomes a ghost?
That's what i wrote. Yes.

Btw calling LuaItemStack.clear() on the bp stack afterwards will also automatically remove the item-on-ground entity. That's why my original one-line version does not keep a handle on the entity.
A few things that bother me with clone + die:

1) doesn't die only leave a ghost once you have researched blueprints?
2) the kill gets attributed to a force. They didn't kill anything.
3) on_entity_died gets called, a corpse and loot remains.

So I don't think that's a viable alternative to the blueprint way.

Re: Creating a ghost of an entity somewhere else

Posted: Tue Sep 03, 2019 3:58 pm
by eradicator
Yes all of the above. But you asked for alternative ways to do it and there isn't much else around ;).