Jump to content

Anonymous Functions


Recommended Posts

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

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

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

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

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