Notice: Failed to invoke Pygments: [Called from MediaWiki\SyntaxHighlight\SyntaxHighlight::highlightInner in /home/pi/www/gtac/wiki/wiki/extensions/SyntaxHighlight_GeSHi/includes/SyntaxHighlight.php at line 334] in /home/pi/www/gtac/wiki/wiki/includes/debug/MWDebug.php on line 507

Notice: Failed to invoke Pygments: [Called from MediaWiki\SyntaxHighlight\SyntaxHighlight::highlightInner in /home/pi/www/gtac/wiki/wiki/extensions/SyntaxHighlight_GeSHi/includes/SyntaxHighlight.php at line 334] in /home/pi/www/gtac/wiki/wiki/includes/debug/MWDebug.php on line 507
How to use Events - GTA Connected

How to use Events

Revision as of 15:56, 21 April 2021 by Mex (talk | contribs)

Summary

Events are used to run your code when something specific occurs.
An event system is also called the observer pattern.

A scripting function can be bound to an event, by using the event name with addEventHandler or bindEventHandler.
When an event is invoked, all scripting functions that are bound to that event are called.

Event names are not case-sensitive, OnPlayerJoined is the same as onPlayerJoined.

Example

JS, Server-Side:

addEventHandler('onPlayerJoined', function(event,client)
{
    message(client.player.name + ' has joined the server!', COLOUR_GREEN);
})

Lua, Server-Side:

addEventHandler('onPlayerJoined', function(event,client)
    message(client.player.name .. ' has joined the server!', COLOUR_GREEN)
end)