I'm working on a mod at the moment, does anyone know how to change the lifespan of an already created ghost (blueprint) entity?
Re: Ghost entities.
Posted: Mon May 26, 2014 12:24 pm
by darius456
If you want to change something related to ghost you can use commandlocal ghostentity = game.findentitiesfiltered{area = {{-10, -10}, {10, 10}}, type="ghost"} (remember to change position) it will return entity. And then you can simply try to ghostentity.health = xxx or ghostentity.destroy(). I don't know if this will work with blueprint ghost, but with normal ghost after destroy it work.
Re: Ghost entities.
Posted: Tue May 27, 2014 1:53 am
by JamesOFarrell
I tried to destroy a ghost as it was created in the onentitydied event handler and found that it would cause the game to crash. Just a heads up.
Re: Ghost entities.
Posted: Tue May 27, 2014 2:35 am
by FreeER
JamesOFarrell wrote:I tried to destroy a ghost as it was created in the onentitydied event handler and found that it would cause the game to crash.
...how did you even manage that? I tried testing this and my code doesn't even find a ghost in onentitydied? The only thing I could guess is that you tried destroying the event.entity, which is a reference to the original (dead) entity not the ghost, and attempting to destroy the already dead entity did crash Factorio to the desktop however...a bit of a "bug" there that I've never encountered before
However, if you store the original entity's position and then scan that area ontick (aka outside of onentitydied so the c++ code's had time to place it), it finds the ghost and destroys it with no problem.
here's my test code for the "working" version
require "defines"
game.oninit(function() glob.ghosts = {} end)
game.onload(function() if not glob.ghosts then glob.ghosts = {} end end)
game.onevent(defines.events.onentitydied, function(event)
table.insert(glob.ghosts, event.entity.position)
end)
function getBoundingBox(pos, radius)
return {{pos.x - radius, pos.y - radius},{pos.x + radius, pos.y + radius}}
end
game.onevent(defines.events.ontick, function(event)
for i, pos in ipairs(glob.ghosts) do
for _, ghost in ipairs(game.findentitiesfiltered{type="ghost", area=getBoundingBox(pos, 0.1)}) do
ghost.destroy()
end
end
glob.ghosts = {}
end)
edit: as for the original question: I'm not certain that you can other than through the tech modifier "ghost-time-to-live", I think increasing that will increase the time of all existing ghosts...but I'm not 100% certain on that.
Re: Ghost entities.
Posted: Tue May 27, 2014 2:43 am
by JamesOFarrell
This is what I had, it causes the game to crash. If you comment out the destroy line it will still log the ghost count so it is finding something.
game.onevent(defines.events.onentitydied, function(event)
if isKnownProxy(event.entity) then
local ghosts = game.findentitiesfiltered{area = {{event.entity.position.x, event.entity.position.y}, {event.entity.position.x, event.entity.position.y}}, "ghost"}
for i, ghost in ipairs(ghosts) do
debugLog("Ghost to destroy: " .. i)
ghost.destroy()
end
end
end)
I also tried to destroy the entity before it could finish the event pipeline to see if that stopped the game from creating the ghost. it caused it to crash.. obviously
Thanks for the code. I come up with another solution but I still might use that to stop the ghosts from being created.
Re: Ghost entities.
Posted: Tue May 27, 2014 3:11 am
by FreeER
JamesOFarrell wrote:This is what I had, it causes the game to crash.
ah I see, you are essentially doing event.entity.destroy()
If you look at the findentitiesfiltered code I had (and what darius wrote) and what you have you'll notice that you simply pass "ghost" and not type="ghost", which makes "ghost" go into index 1 of the table you pass to findentitiesfiltered rather than the "type" index and so findentitiesfiltered simply ignores it and acts as if you had called findentities and returns everything at that position (which happens to be/include the entity that died, or event.entity. Try adding debugLog(event.entity.equals(ghost)) to your for loop and you'll see that it prints true (at least I'll assume that it would since I had to create the debugLog function myself (just a serpent.block proxy)...small hint, when you upload code snippets don't include function that aren't in the base game or include those functions as well, I've no idea what isKnownProxy should return so I could only strip it out).
Re: Ghost entities.
Posted: Tue May 27, 2014 3:18 am
by JamesOFarrell
Well, would you look at that, I do indeed. I used it correctly elsewhere in my code. I should have stripped that unlinked code I was just being lazy. debugLog() is not a standard function either but i figured it didn't really matter to much for the discussion. I'll be less lazy next time.