TriggerServerEvent: Difference between revisions
Black Dragon (talk | contribs) mNo edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Client function}} | |||
This function triggers an event on servers. This is the primary means of passing information between the client and the server. Servers have a similar [[triggerClientEvent]] function that can do the reverse. You can treat this function as if it was an asynchronous function call, using [[triggerClientEvent]] to pass back any returned information if necessary. | This function triggers an event on servers. This is the primary means of passing information between the client and the server. Servers have a similar [[triggerClientEvent]] function that can do the reverse. You can treat this function as if it was an asynchronous function call, using [[triggerClientEvent]] to pass back any returned information if necessary. | ||
Line 28: | Line 29: | ||
'''Server:''' | '''Server:''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function greetingHandler ( message ) | function greetingHandler ( message ) | ||
outputChatBox ( "The client says: " .. message ) | outputChatBox ( "The client says: " .. message ) | ||
end | end | ||
addEvent("onGreeting") | |||
addEventHandler("onGreeting", getRootElement(), greetingHandler) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''Client:''' | '''Client:''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function greetingCommand ( source, commandName ) | function greetingCommand ( source, commandName ) | ||
triggerServerEvent ( "onGreeting", getRootElement(), "Hello World!" ) | triggerServerEvent ( "onGreeting", getRootElement(), "Hello World!" ) | ||
end | end | ||
addCommandHandler ( "greet", greetingCommand ) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
When the command "greet" is executed (by typing it in the server console or the player's console), the clients ''greetingCommand'' function is called. This triggers the server-side event ''onGreeting'' with the string ''"Hello World!"''. This event is then handled by the ''greetingHandler'' function server-side which then displays the message. | When the command "greet" is executed (by typing it in the server console or the player's console), the clients ''greetingCommand'' function is called. This triggers the server-side event ''onGreeting'' with the string ''"Hello World!"''. This event is then handled by the ''greetingHandler'' function server-side which then displays the message. |
Revision as of 17:20, 15 August 2007
This function triggers an event on servers. This is the primary means of passing information between the client and the server. Servers have a similar triggerClientEvent function that can do the reverse. You can treat this function as if it was an asynchronous function call, using triggerClientEvent to pass back any returned information if necessary.
Almost any data types can be passed as expected, including elements and complex nested tables. Special data types like xmlNodes will not be able to be passed as they do not necessarily have a valid representation on the client.
Events are sent reliably, so the server will receive them, but there may be (but shouldn't be) a significant delay before they are received. You should take this into account when using them.
Keep in mind the bandwidth issues when using events - don't pass a large list of arguments unless you really need to. It is marginally more efficient to pass one large event than two smaller ones.
Syntax
bool triggerServerEvent ( string event, element theElement, [arguments...] )
Required Arguments
- event: The name of the event to trigger server-side. You should register this event with addEvent and add at least one event handler using addEventHandler.
- theElement: The element that is the source of the event. This could be another player, or if this isn't relevant, use the root element.
Optional Arguments
- arguments...: A list of arguments to trigger with the event. You can pass any lua data type (except functions). You can also pass elements.
Returns
Returns true if the event trigger has been sent, false if invalid arguments were specified.
Example
This example shows how you can pass a simple "Hello World" message from the client to the server using an event.
Server:
function greetingHandler ( message ) outputChatBox ( "The client says: " .. message ) end addEvent("onGreeting") addEventHandler("onGreeting", getRootElement(), greetingHandler)
Client:
function greetingCommand ( source, commandName ) triggerServerEvent ( "onGreeting", getRootElement(), "Hello World!" ) end addCommandHandler ( "greet", greetingCommand )
When the command "greet" is executed (by typing it in the server console or the player's console), the clients greetingCommand function is called. This triggers the server-side event onGreeting with the string "Hello World!". This event is then handled by the greetingHandler function server-side which then displays the message.