I wanted to write a simple resource to add some commands for adding weapons and spawning vehicles etc (first resource i create). I was looking in the [system]\chat resource to see how to do it and now i’m very confused.
My understanding is this:
When something is typed in the chat window, the following callback is made in the client script:
RegisterNUICallback('chatResult', function(data, cb)
chatInputActive = false
SetNuiFocus(false)
if not data.canceled then
local id = PlayerId()
--deprecated
local r, g, b = 0, 0x99, 255
if data.message:sub(1, 1) == '/' then
ExecuteCommand(data.message:sub(2))
else
TriggerServerEvent('_chat:messageEntered', GetPlayerName(id), { r, g, b }, data.message)
end
end
cb('ok')
end)
After this the server runs the following event handler:
AddEventHandler('_chat:messageEntered', function(author, color, message)
if not message or not author then
return
end
TriggerEvent('chatMessage', source, author, message)
if not WasEventCanceled() then
TriggerClientEvent('chatMessage', -1, author, { 255, 255, 255 }, message)
end
print(author .. ': ' .. message)
end)
This triggers a chatMessage server event and also a chatMessage client event.
Based on this i now have a couple of questions:
-
How does cancellation of events work? I would have thought that if i cancel an event it would stop further eventhandlers from being called on it, but the use of WasEventCancelled() suggests that an event handler will be called even if the event is cancelled?
-
The server triggers a server chatMEssage event, but no such event is registered by the server, is there only a need to register events that gets called between server and client?
-
In the client code (cl_chat.lua) chatResult callback, if the first character is a ‘/’, it doesnt trigger a _chat:messageEntered event but instead calls an ExecuteCommand function on the input. What does this do/where can i find it? And does that mean that nothing automatically gets sent to the server if i start a message with a ‘/’?
-
I’m curious how event handlers handle different arguments. Am i correct in thinking that if i trigger a particular event with some arguments, any eventhandlers that match those arguments will get triggered, and if i later trigger the same event with different arguements it will trigger different handlers? As in, when triggering event handlers it pattern matches the argument types?
Thanks for any help on this!