TriggerClientEvent: Difference between revisions
No edit summary |
|||
Line 16: | Line 16: | ||
===Required Arguments=== | ===Required Arguments=== | ||
*'''name:''' The name of the event to trigger client side. You should register this event with [[addEvent]] and add at least one event handler using [[addEventHandler]]. | *'''name:''' The name of the event to trigger client side. You should register this event with [[addEvent]] and add at least one event handler using [[addEventHandler]]. | ||
*'''theElement:''' The element that is the | *'''theElement:''' The element that is the [[Event system#Event handlers|source]] of the event. This could be another player, or if this isn't relevant, use the root element. | ||
===Optional Arguments=== | ===Optional Arguments=== | ||
{{OptionalArg}} | {{OptionalArg}} | ||
*'''triggerFor:''' The event will be triggered on all [[ | *'''triggerFor:''' The event will be triggered on all [[player]]s that are children of the specified element. By default this is the root element, and hence the event is triggered on all elements. If you specify a single player it will just be triggered for that player. | ||
*'''arguments...:''' A list of arguments to trigger with the event. You can pass any lua data type (except functions). You can also pass [[element]]s. | *'''arguments...:''' A list of arguments to trigger with the event. You can pass any lua data type (except functions). You can also pass [[element]]s. | ||
Line 27: | Line 27: | ||
==Example== | ==Example== | ||
===Example 1=== | |||
This example shows how you can pass a simple "Hello World" message from the server to the all the clients using an event. | |||
<section name="Client" class="client" show="true"> | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function greetingHandler ( message ) | function greetingHandler ( message ) | ||
outputChatBox ( "The server says: " .. message ) | outputChatBox ( "The server says: " .. message ) | ||
end | end | ||
addEvent("onGreeting", true) | addEvent( "onGreeting", true ) | ||
addEventHandler("onGreeting", getRootElement(), greetingHandler) | addEventHandler( "onGreeting", getRootElement(), greetingHandler ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
<section name="Server" class="server" show="true"> | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function greetingCommand ( source, commandName ) | function greetingCommand ( source, commandName ) | ||
Line 45: | Line 46: | ||
addCommandHandler ( "greet", greetingCommand ) | addCommandHandler ( "greet", greetingCommand ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
When the command "greet" is executed (by typing it in the server console or the player's console), the server's ''greetingCommand'' function is called. This triggers the client side event ''onGreeting'' with the string ''"Hello World!"''. This event is then handled by the ''greetingHandler'' function client 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 server's ''greetingCommand'' function is called. This triggers the client side event ''onGreeting'' with the string ''"Hello World!"''. This event is then handled by the ''greetingHandler'' function client side which then displays the message. | ||
===Example 2=== | |||
This example shows how you can pass a simple "Hello World" message from the server to '''a single''' client using an event. | |||
<section name="Client" class="client" show="true"> | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function greetingHandler ( message ) | function greetingHandler ( message ) | ||
outputChatBox ( "The server says: " .. message ) | outputChatBox ( "The server says: " .. message ) | ||
end | end | ||
addEvent("onGreeting", true) | addEvent( "onGreeting", true ) | ||
addEventHandler("onGreeting", getRootElement(), greetingHandler) | addEventHandler( "onGreeting", getRootElement(), greetingHandler ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
<section name="Server" class="server" show="true"> | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function greetingCommandOne ( | function greetingCommandOne ( playerSource, commandName, playerName ) | ||
if playerName then | if playerName then | ||
local | local thePlayer = getPlayerFromNick ( playerName ) | ||
if | if thePlayer then | ||
triggerClientEvent ( | triggerClientEvent ( thePlayer, "onGreeting", getRootElement(), "Hello World!" ) | ||
else | else | ||
-- invalid player name specified | -- invalid player name specified | ||
Line 75: | Line 78: | ||
addCommandHandler ( "greet_one", greetingCommandOne ) | addCommandHandler ( "greet_one", greetingCommandOne ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
This works like the first example except an extra '' | This works like the first example except an extra ''thePlayer'' argument is specified for triggerClientEvent. | ||
==See Also== | ==See Also== | ||
{{Event functions}} | {{Event functions}} |
Revision as of 20:23, 14 June 2008
This function triggers an event previously registered on a client. This is the primary means of passing information between the server and the client. Clients have a similar triggerServerEvent function that can do the reverse. You can treat this function as if it was an asynchronous function call, using triggerServerEvent to pass back any returned information if necessary.
Almost any data types can be passed as expected, including elements and complex nested tables. Non-element MTA data types like xmlNodes or resource pointers will not be able to be passed as they do not necessarily have a valid representation on the client.
Events are sent reliably, so clients 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 triggerClientEvent ( [element triggerFor=getRootElement()], string name, element theElement, [arguments...] )
Required Arguments
- name: The name of the event to trigger client 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
NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.
- triggerFor: The event will be triggered on all players that are children of the specified element. By default this is the root element, and hence the event is triggered on all elements. If you specify a single player it will just be triggered for that player.
- 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
Example 1
This example shows how you can pass a simple "Hello World" message from the server to the all the clients using an event.
function greetingHandler ( message ) outputChatBox ( "The server says: " .. message ) end addEvent( "onGreeting", true ) addEventHandler( "onGreeting", getRootElement(), greetingHandler )
function greetingCommand ( source, commandName ) triggerClientEvent ( "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 server's greetingCommand function is called. This triggers the client side event onGreeting with the string "Hello World!". This event is then handled by the greetingHandler function client side which then displays the message.
Example 2
This example shows how you can pass a simple "Hello World" message from the server to a single client using an event.
function greetingHandler ( message ) outputChatBox ( "The server says: " .. message ) end addEvent( "onGreeting", true ) addEventHandler( "onGreeting", getRootElement(), greetingHandler )
function greetingCommandOne ( playerSource, commandName, playerName ) if playerName then local thePlayer = getPlayerFromNick ( playerName ) if thePlayer then triggerClientEvent ( thePlayer, "onGreeting", getRootElement(), "Hello World!" ) else -- invalid player name specified end else -- No player name specified end end addCommandHandler ( "greet_one", greetingCommandOne )
This works like the first example except an extra thePlayer argument is specified for triggerClientEvent.
See Also
- addEvent
- addEventHandler
- cancelEvent
- cancelLatentEvent
- getEventHandlers
- getLatentEventHandles
- getLatentEventStatus
- removeEventHandler
- triggerEvent
- wasEventCancelled