Jump to content

just wanted to ask something about addEventHandler


Recommended Posts

Posted

i just want to ask is there any difference between this:

addEventHandler("onPlayerJoin",root 
--other part of code 
end) -- ) closing the up addEventHandler 

and

addEventHandler("onPlayerJoin",root,functionName) 

only thing i've noticed is that up example is used when theres no function name,but i am curious is there any difference other than that?

Posted
addEventHandler("onPlayerJoin",root 
function ()  
end -- This end is for the function 
) 
  

And there is no other difference.

Posted

You use

  
addEventHandler("onPlayerJoin",root, 
function() 
end 
) 
  

for functions which you don't call anymore in your script , i call those functions just anonymouse functions.

Posted

You may view them in a line to see the difference better:

addEventHandler("onPlayerJoin",root,function() end) 
addEventHandler("onPlayerJoin",root,functionName) 

So what we can see here is that the first one has the actual function inside the handler as a parameter, this is useful when you want to call a function inside another function and thereby access local variables from the first function inside the second function. setTimer() has some good samples of this. Anyway by using local variables you save memory so it's pretty good for the performance if you want to optimize your scripts.

The second one adds a command handler for an already existing function within the same source file, just remember that the function need to be defined before this is executed so it should be added below the function it trigger or inside another function basically. This is more useful for large functions which contains a lot of code but both works no matter what actually.

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...