TriggerClientEvent

From Multi Theft Auto: Wiki
Revision as of 00:26, 15 June 2007 by EAi (talk | contribs)
Jump to navigation Jump to search

This function triggers an event on clients. 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. 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 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 cannot pass tables or any MTA special data types except elements.

Returns

Returns true if the event trigger has been sent, false if invalid arguments were specified.

Example

This example does...

--This line does...
blabhalbalhb --abababa
--This line does this...
mooo

See Also