Register entity-related events in entity instead of script?

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1190
Joined: Wed May 18, 2016 4:55 pm
Contact:

Register entity-related events in entity instead of script?

Post by Mooncat »

I had ideas of requesting events like on_entity_inventory_changed - fired when an item is picked from or inserted into an entity's inventory. No wonder this event will be fired millions of times when inserters and even robots are involved.
(I don't really need the event at this moment so didn't write a request, and don't even know whether it has been requested or not.)

However, I saw a reply from Rseding91 saying that performance for something like that will be a problem.
Rseding91 wrote:Such an event is unlikely to be added due to performance reasons.

Such an event would be fired hundreds/thousands of times per second during normal combat while not adding a lot of value for the few times it was used.
(The original thread here)

As a (not so professional) programmer, I think I might understand that it is caused by the overhead for communication between the game engine and lua scripts.

If that is the case, then how about eliminating unnecessary communications?

Currently, the events are registered in "script" without specific targets. But what if we can register the entity-related events to the wanted entity when it is built? (Note: we will still need on_built_entity and on_robot_built_entity to be in "script", because we don't have the entity before them.)
In this way, the events will no longer need to fire for all entities, but only fire for the entities that have the events registered and when each individual meets the requirements. As the result, performance can be increased!

This can also be applied to player-related events, and maybe GUI-related ones too.

But, it is just my (not so professional) assumption without knowing how the game engine actually works. Correct me if I am wrong. :mrgreen:

User avatar
mojo2012
Inserter
Inserter
Posts: 25
Joined: Fri Apr 01, 2016 8:05 am
Contact:

Re: Register entity-related events in entity instead of script?

Post by mojo2012 »

I would even go further and create a way so that we can set an even handler function in the data.lua directly on the entity.

User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1190
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: Register entity-related events in entity instead of script?

Post by Mooncat »

mojo2012 wrote:I would even go further and create a way so that we can set an even handler function in the data.lua directly on the entity.
Yes, this can be an additional option.

Registering the handlers in contorl.lua can give more control on specifying entities according to player, force, build time, etc. But if you want to register the handlers for all entities of the same prototype, which is the more usual case, it will be easier to do in data.lua and data-final-fixes.lua.

Maybe it can be something like this:
data.lua

Code: Select all

{
    type = "rocket-silo",
    name = "mymod.dont-deconstruct-me-or-die",
    ...
    on_marked_for_deconstruction = { {"mymod-interface", "function_name"}, {"mymod-interface", "destroy_the_planet"} }
}
or in control.lua

Code: Select all

function on_entity_built(event)
    if event.created_entity.name == "mymod.dont-deconstruct-me-or-die" then
        table.insert(event.created_entity.on_marked_for_deconstruction, {"mymod-interface", "function_name"})
        table.insert(event.created_entity.on_marked_for_deconstruction, {"mymod-interface", "destroy_the_planet"})
    end
end

-- Handler interface = not available to players in game, only for event handlers.
script.add_handler_interface("mymod-interface", {
    function_name = function(event) ... end,
    destroy_the_planet = function(event) ... end
})

Post Reply

Return to “Modding discussion”