KingMofo Posted May 20, 2010 Share Posted May 20, 2010 Hello all. I have a question that i have been meaning to ask for awhile now. I prefer to use anonymous functions and when i try to do 2 eventhandlers, it errors. For example: addEvent("playerNotChatting", true) addEventHandler("playerNotChatting", root) addEventHandler("onPlayerQuit", root, function() triggerClientEvent("updateChatList", root, source, false) end ) If i do just addEvent and addEventHandler, it will work but when i try to do 2 event handlers inside a anonymous function, it never works. So my question is, can you use more than 1 command/event handler inside a anonymous function? if yes, would you be kind enough to tell me how? Thanks in advanced. Link to comment
50p Posted May 21, 2010 Share Posted May 21, 2010 Your line 2 is invalid. You better check addEventHandler parameters. No wonder why you get error. Link to comment
KingMofo Posted May 21, 2010 Author Share Posted May 21, 2010 Your line 2 is invalid. You better check addEventHandler parameters. No wonder why you get error. * eventName: The name of the event you want to attach the handler function to. * attachedTo: The element you wish to attach the handler to. The handler will only be called when the event it is attached to is triggered for this element, or one of its children. Often, this can be the root element (meaning the handler will be called when the event is triggered for any element). * handlerFunction: The handler function you wish to call when the event is triggered. This function will be passed all of the event's parameters as arguments, but it isn't required that it takes all of them. Now, i missed the 3rd argument on purpose as how can i put that 3rd argument when there is no function name? Link to comment
norby89 Posted May 21, 2010 Share Posted May 21, 2010 You cannot 'miss it on purpose', it's a required parameter. You can only leave out optional parameters which are marked with [] brackets but this is not the case. If you think about you will see it makes no sense, how would the handler know what to do when the event is triggered if you didn't tell it to do anything. I'm not quite sure what you're trying to achieve, but if you want the 2nd addEventHandler within the first one, then here goes: addEvent("playerNotChatting", true) addEventHandler("playerNotChatting", root, function() addEventHandler("onPlayerQuit", root, function() triggerClientEvent("updateChatList", root, source, false) end ) end ) Link to comment
Antibird Posted May 21, 2010 Share Posted May 21, 2010 I think he just wants to use a function as an argument for 2 event handlers, so i don't see any other way rather than making the function "non-anonymous", declaring a ( local ) variable for it. Link to comment
robhol Posted May 21, 2010 Share Posted May 21, 2010 Yup, definitely impossible. Either make the function back into a regular function, or.. write it twice. Obviously this last solution isn't quite ideal. Link to comment
Dark Dragon Posted May 21, 2010 Share Posted May 21, 2010 addEvent("playerNotChatting", true) addEventHandler("playerNotChatting", root, function() triggerClientEvent("updateChatList", root, source, false) end ) addEventHandler("onPlayerQuit", root, function() triggerClientEvent("updateChatList", root, source, false) end ) personally i don't see a reason not to give each function a name, the benefits of not doing so are minor Link to comment
KingMofo Posted May 21, 2010 Author Share Posted May 21, 2010 You cannot 'miss it on purpose', it's a required parameter. You can only leave out optional parameters which are marked with [] brackets but this is not the case. If you think about you will see it makes no sense, how would the handler know what to do when the event is triggered if you didn't tell it to do anything. I'm not quite sure what you're trying to achieve, but if you want the 2nd addEventHandler within the first one, then here goes: addEvent("playerNotChatting", true) addEventHandler("playerNotChatting", root, function() addEventHandler("onPlayerQuit", root, function() triggerClientEvent("updateChatList", root, source, false) end ) end ) This is what i was looking for. Now looking at it, i see what you all mean. It would be easier just to turn it back into a normal function but i have been curious for awhile now if it can be done or not. Thanks all, you satisfied my curiosity. 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