[0.13.12] LuaEntity.revive does not trigger on_built_entity

Bugs that are actually features.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

[0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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
My mods on the Factorio Mod Portal :geek:
Rseding91
Factorio Staff
Factorio Staff
Posts: 15430
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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.
If you want to get ahold of me I'm almost always on Discord.
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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?
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
Rseding91
Factorio Staff
Factorio Staff
Posts: 15430
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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
If you want to get ahold of me I'm almost always on Discord.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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...
My mods on the Factorio Mod Portal :geek:
User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1207
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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.)
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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 ?
My mods on the Factorio Mod Portal :geek:
Rseding91
Factorio Staff
Factorio Staff
Posts: 15430
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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.
If you want to get ahold of me I'm almost always on Discord.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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)
My mods on the Factorio Mod Portal :geek:
User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1207
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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. ;)
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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...
My mods on the Factorio Mod Portal :geek:
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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?
Rseding91
Factorio Staff
Factorio Staff
Posts: 15430
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post by Rseding91 »

I'll make revive() return the revived entity for 0.13.14.
If you want to get ahold of me I'm almost always on Discord.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: [0.13.12] LuaEntity.revive does not trigger on_built_entity

Post 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)
Post Reply

Return to “Not a bug”