Jump to content

Tell me, pls (Events)


Recommended Posts

Hi all. I have 1 resource (gamemode), this resource contains more *.lua files.

Each event i make so (example):

  
addEventHandler("onColShapeHit", root, 
    function(hitElem, matching) 
        ... 
    end 
) 
  

But, in each *.lua file this event may be repeated.

Or better to use a single event, than contains more functions. Example:

  
addEventHandler("onColShapeHit", root, 
    function(hitElem, matching) 
        colShapeHitInFirstLuaFile(hitElem, matching) 
        colShapeHitInSecondLuaFile(hitElem, matching) 
        ... 
        colShapeHitInNLuaFile(hitElem, matching) 
    end 
) 
  

Is there a difference in performance or not important?

Link to comment

Well, it's a smart idea to have one event and call all the functions that would need it, at the same time no it shouldn't impact server performance and if it did then probably a little, but it does reduce the size of the file as more char's or string's would make the size more larger.

Link to comment

but there is an inconvenient. if you want to call a function declared on a different file, lua need to run first that lua script.

you need to change the order in which scripts are loaded. (changing meta.xml file)

if you add an addEventHandler (...) on each file, you avoid this problem.

Link to comment
but there is an inconvenient. if you want to call a function declared on a different file, lua need to run first that lua script.

you need to change the order in which scripts are loaded. (changing meta.xml file)

if you add an addEventHandler (...) on each file, you avoid this problem.

That is correct but if order of called functions is necessary then 1 event handler is better. The file with that 1 event handler would be the last script file defined in meta.xml.

@Jadore,

If you don't need to worry about the order the functions get called then you can define each event handler in separate files but if you need to call functions in specific order then use only 1 event handler. The event handlers are triggered asynchronously that means 1 event handler may be triggered 1st but the next time it may be triggered 2nd.

Eg. You may want to take player's weapons when he enters a col shape and give them back when he leaves. You may also want to check if player has specific weapon when he enters the col shape but in different event handler. It may fail sometimes because 1 event handler will check if player has the weapon, when triggered 1st it will work because the other event handler will take the weapons away. But if the event handler that takes the weapons away will be triggered first then checking if player has the weapon in the other handler will fail, even though he had the weapon.

I hope that clarifies when to use 1 event handler and when you can use multiple event handlers.

Link to comment

you are right.

conclussion ->

if you don´t care about the order in which functions are executed, they could individually be handlers for that event.

if you have to take into account the order in which functions are executed, you must use a single handler for that event which will execute the functions in a particular order.

you say that?

for the 1º case, you can add your event handlers in different files. (no matter the order in which scripts are executed)

for the 2º case, you must take into account how order in which lua loads your scripts. (the functions must be visible for your event handler, so they can be called)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...