HowTo/Events: Difference between revisions

From GTA Connected
Jump to navigation Jump to search
No edit summary
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 5: Line 5:
An event system is also called the observer pattern.
An event system is also called the observer pattern.


A scripting function can be bound to an event name.<br>
A scripting function can be bound to an event, by using the event name with [[addEventHandler|addEventHandler]] or [[bindEventHandler|bindEventHandler]].<br>
When an event name is invoked, all scripting functions that are bound to that event name are called.
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==
==Example==
'''JS, Server-Side:'''
{{JSCode|1=addEventHandler('onPlayerJoined', function(event,client)
{
    message(client.player.name + ' has joined the server!', COLOUR_GREEN);
});}}


'''Lua, Server-Side:'''
'''Lua, Server-Side:'''


{{LuaCode|1=addEventHandler('onPlayerJoined', function(event,client)
{{LuaCode|1=addEventHandler('onPlayerJoined', function(event,client)
     outputChatBox(client.player.name .. ' has joined the server!', COLOUR_GREEN)
     message(client.player.name .. ' has joined the server!', COLOUR_GREEN)
end)}}
end)}}

Latest revision as of 15:57, 21 April 2021

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)