Wojak Posted June 22, 2011 Share Posted June 22, 2011 Subject: The point of using “on(Client)ResourceStart” event for "this" (current) resource. Lets consider the fallowing examples: function chatbox() outputChatBox("Starting resource x")-- this line is a example and represents many things... end addEventHandler("onResourceStart", getResourceRootElement(getThisResource()),chatbox) and outputChatBox("Starting resource x") both codes will have the exact same effect, the only difference is the memory usage (first calls 3 more functions: addEventHandler, getThisResource and getResourceRootElement) so why everybody are using the first method? Link to comment
BabY Posted June 23, 2011 Share Posted June 23, 2011 Uhmmm.. well I think it confirm the function loading.. or if it's for huge functions .. example : addCommandHandler("globalmute", function() for index, player in ipairs(getElementsByType("player")) do setPlayerMuted(player, true) end end) This is a command, it doesn't need any event handlers.. but if it's : function gMute() for x,y in ipairs(getElementsByType("player")) do setPlayerMuted(player, true) end end addEventHandler("onResourceStart", getRootElement(), gMute) This script will need the event handler, because "for x,y in ipairs....etc." will of course cause an error, and the script won't load. According to my knowleg, event handlers is added for multiply functions or 2+ functions in one script.. but if it's one script, it's not recommended to use the handlers. And somehow, people may use it for increasing script's lines only Link to comment
DarkLink Posted June 23, 2011 Share Posted June 23, 2011 Well I dont know Lua much yet, but you need an handler to call a function. If u just write a function on ur code, and u dont call it using and handler or a setTimer.. the function will not be called, its there, but dont do anything.. I guess thats how Lua on MTA Link to comment
AGENT_STEELMEAT Posted June 23, 2011 Share Posted June 23, 2011 When you have stuff that's outside of a function, it will be compiled.executed while the resource starts. The point of using an event handler is to either accept arguments passed by an event (in onResourceStart, it will pass the started resource's element), or for code readability. There is no performance difference - and if there was, it isn't THAT important as resources don't get started many times (in most cases). You don't have to use getResourceRootElement(getThisResource()), instead, use the global variable resourceRoot that is predefined by MTA. Also, you don't need a handler to use a function. function resStartFunc(startedRes) doSomething(startedRes) end addEventHandler("onResourceStart", resourceRoot, resStartFunc) function doSomething(res) outputChatBox(getResourceName(res).." started!") end The function resStartFunc is executed when the resource starts, and passes on the startedRes argument to doSomething - which in this case outputs the resource's name. Link to comment
Wojak Posted June 23, 2011 Author Share Posted June 23, 2011 code : for x,y in ipairs(getElementsByType("player")) do setPlayerMuted(player, true) end will run without any problems on resource start, also if you want a function so much you can do: function gMute() for x,y in ipairs(getElementsByType("player")) do setPlayerMuted(player, true) end end gMute() and it still will run on resource start with no need of events The point of using an event handler is to either accept arguments passed by an event (in onResourceStart, it will pass the started resource's element), or for code readability. There is no performance difference - and if there was, it isn't THAT important as resources don't get started many times (in most cases). that means 99,9% of scripters use this event for no reason, ass they don't use the resource parameter... also code: function resStartFunc(startedRes) doSomething(startedRes) end addEventHandler("onResourceStart", resourceRoot, resStartFunc) function doSomething(res) outputChatBox(getResourceName(res).." started!") end shourd have same efect as: outputChatBox(getResourceName(getThisResource()).." started!") My theory is that using this event has its origin in copying examples from MTA wiki. Link to comment
eAi Posted June 23, 2011 Share Posted June 23, 2011 The point of the event is that you're guaranteed that all the script files have been completely loaded before it is called. In many cases, this won't matter, in some it might. It allows you to get away from caring about the order you've specified lua files to be loaded and the order that code is written within a single file. Writing code outside events may well be a good thing to do - it's certainly less expensive than adding event handlers. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now