Creating a ghost of an entity somewhere else

Place to get help with not working mods / modding interface.
mrvn
Smart Inserter
Smart Inserter
Posts: 5969
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Creating a ghost of an entity somewhere else

Post 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?
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Creating a ghost of an entity somewhere else

Post 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
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Creating a ghost of an entity somewhere else

Post 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)
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Creating a ghost of an entity somewhere else

Post 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).
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
mrvn
Smart Inserter
Smart Inserter
Posts: 5969
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Creating a ghost of an entity somewhere else

Post 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?
mrvn
Smart Inserter
Smart Inserter
Posts: 5969
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Creating a ghost of an entity somewhere else

Post 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.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Creating a ghost of an entity somewhere else

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
mrvn
Smart Inserter
Smart Inserter
Posts: 5969
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Creating a ghost of an entity somewhere else

Post 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.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Creating a ghost of an entity somewhere else

Post by eradicator »

Yes all of the above. But you asked for alternative ways to do it and there isn't much else around ;).
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Post Reply

Return to “Modding help”