Page 1 of 1
[0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Sat Jul 30, 2016 7:54 pm
by binbinhfr
Hi,
it seems that the LuaEntity.revive() function does not trigger the on_built_entity event when the ghost transforms into a real entity.
It creates problems with most mods that are scanning for entity creations.
The Instant Blueprint mod uses this to instant create objects from blueprints :
Code: Select all
script.on_event(defines.events.on_built_entity, function(ev)
if (ev.created_entity.name == "entity-ghost" or ev.created_entity.name == "tile-ghost") then
ev.created_entity.revive()
end
end)
The revive does not trigger on_built_entity event of the real entity and other mods cannot learn that this entity was revived.
Or can you trigger an on_built_entity from inside a on_built_entity ?
subject related :
viewtopic.php?f=11&t=27608&p=174801&hil ... ve#p174754
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Sat Jul 30, 2016 8:04 pm
by Rseding91
That's how the mod API works. When you do most things through the API they don't fire events. surface.create_entity doesn't fire an event, entity.destroy() doesn't fire an event, and so on.
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Sat Jul 30, 2016 8:44 pm
by Ranakastrasz
Rseding91 wrote:That's how the mod API works. When you do most things through the API they don't fire events. surface.create_entity doesn't fire an event, entity.destroy() doesn't fire an event, and so on.
In that case, we need a way to manually trigger an event. Is there a function call that triggers on_built_entity with whatever arguments?
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Sat Jul 30, 2016 9:15 pm
by Rseding91
Ranakastrasz wrote:Rseding91 wrote:That's how the mod API works. When you do most things through the API they don't fire events. surface.create_entity doesn't fire an event, entity.destroy() doesn't fire an event, and so on.
In that case, we need a way to manually trigger an event. Is there a function call that triggers on_built_entity with whatever arguments?
Yes:
http://lua-api.factorio.com/latest/LuaG ... aise_event
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Sun Jul 31, 2016 12:13 am
by binbinhfr
Ok I get it, so I suppose that it's the modder that created "Instant Blueprint mod" who has to add an event manual trigger to the revived entity...
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Mon Aug 01, 2016 4:20 pm
by Mooncat
Hi, my mod just encountered this problem since a user told me the "revived" entities are not working. And I just saw this post.
Yes, the author of Instant Blueprint can raise a custom event when any entity is revived. But I think it is not the best solution as it only works after other modders have also added listener to the event in their mods. Listening to custom events require acknowledgment of the former mod. It is more like you want to add support to a specific mod. But in this case, Instant Blueprint, or LuaEntity.revive() and destroy(), is more like a feature that it may be implemented in more other mods for cheats, like my Creative Mode. Custom event will be troublesome to other modders.
I think there are better solutions, like:
For entity.revive():
1. it should trigger on_built_entity; or
2. add on_entity_revived event which will be triggered when calling entity.revive(); or
3. add LuaPlayer.build_entity to replace both surface.create_entity and entity.revive(). Calling it will trigger on_built_entity
For entity.destroy():
1. add on_entity_destroyed event which will be triggered when calling entity.destroy(); or
2. add LuaPlayer.destory_entity function and on_destroyed_entity event
(But because in our mods, we have already checked whether the registered entities are valid, it is not too important for us to know the destruction of entities.)
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Mon Aug 01, 2016 4:31 pm
by binbinhfr
Mooncat wrote:Yes, the author of Instant Blueprint can raise a custom event when any entity is revived.
I thought that Rseding was talking about raising the original event defines.events.on_built_entity, not a custom one.
like raise_event(defines.events.on_built_entity,{created_entity = ..., player_index = ...}) ?
Rseding, could you confirm what you meant ?
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Mon Aug 01, 2016 6:19 pm
by Rseding91
binbinhfr wrote:Mooncat wrote:Yes, the author of Instant Blueprint can raise a custom event when any entity is revived.
I thought that Rseding was talking about raising the original event defines.events.on_built_entity, not a custom one.
like raise_event(defines.events.on_built_entity,{created_entity = ..., player_index = ...}) ?
Rseding, could you confirm what you meant ?
yes.
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Mon Aug 01, 2016 6:35 pm
by binbinhfr
And is it the right way to call it ? We are not supposed to pass the common {name,tick} part of the event data ?
because this do not seems to work and creates an error (the type of unprecise error spoke about in my other report...)
I assume in this function that the revived ghost becomes the real entity and does not change ID... because otherwise, how would I know the ID of the new revived entity ?
Code: Select all
script.on_event(defines.events.on_built_entity, function(ev)
if ev.created_entity.name == "entity-ghost" then
ev.created_entity.revive()
game.raise_event(defines.events.on_built_entity,{created_entity = ev.created_entity, player_index = ev.player_index})
end
end)
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Tue Aug 02, 2016 7:00 am
by Mooncat
Oh, I didn't know we can raise built-in event. It is good to know! Hope the devs can show us the right way to call it.
By the way, I have invited the author of Instant Blueprint to join us here.

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Wed Aug 03, 2016 2:53 pm
by binbinhfr
binbinhfr wrote:And is it the right way to call it ? We are not supposed to pass the common {name,tick} part of the event data ?
because this do not seems to work and creates an error (the type of unprecise error spoke about in my other report...)
I assume in this function that the revived ghost becomes the real entity and does not change ID... because otherwise, how would I know the ID of the new revived entity ?
Code: Select all
script.on_event(defines.events.on_built_entity, function(ev)
if ev.created_entity.name == "entity-ghost" then
ev.created_entity.revive()
game.raise_event(defines.events.on_built_entity,{created_entity = ev.created_entity, player_index = ev.player_index})
end
end)
Rseding, are you around ? Because I still cannot manage how to call it on this revived entity...
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Wed Aug 03, 2016 5:25 pm
by Nexela
Some observations I have found so far.
First after entity.revive() the entity is no long valid so I ended up doing some long winded approach to find_entity() the non ghost entity
This doesn't work
Code: Select all
function onBuiltEntity()
...
game.raise_event(myevent, {created_entity=entity, player_index=player_index}
end
script.on_event(myevent, function(event) onBuiltEntity(event) end) --nothing no error just doesn't run onBuiltEntity again
---------------
however this works
Code: Select all
script.on_event(myevent, function(event) log("myevent is working") end) --prints to log
---------------
[edit: fixed typo] also I used a custom event just to test the behavior. possibly a scope issue?
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Wed Aug 03, 2016 5:40 pm
by Rseding91
I'll make revive() return the revived entity for 0.13.14.
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Wed Aug 03, 2016 5:49 pm
by Nexela
Rseding91 wrote:I'll make revive() return the revived entity for 0.13.14.
Awesome!
Any thoughts on the recursion part? I think it boils down to on_event not calling the function if we are already in the function.
Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity
Posted: Wed Aug 03, 2016 5:54 pm
by Nexela
expanding on my second code example when event is raised from onBuiltEntity
Code: Select all
script.on_event(myevent, function(event)
log("myevent is working") --prints to log
onBuiltEntity(event) -- doesn't do anything
log("after building") -- prints to log
end)