AddEvent: Difference between revisions

From Onset Developer Wiki
(Created page with "{{Info|Function|Server|1.0}} {{FuncDescription|__EDIT_ME__}} {{FuncSyntax|AddEvent(EventName, LuaFunction [, OptionalBindId])}} {{FuncParameters}} {{FuncParam|EventName|__E...")
 
No edit summary
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Info|Function|Server|1.0}}
{{Info|Function|Server & Client|1.0}}


{{FuncDescription|__EDIT_ME__}}
{{FuncDescription|Adds a function as event handler which will be called when a certain event occurs. Multiple functions can be bound to a single event.


{{FuncSyntax|AddEvent(EventName, LuaFunction [, OptionalBindId])}}
You can also specify new event names that do not exist.}}
 
{{FuncSyntax|AddEvent(EventName, LuaFunction)}}


{{FuncParameters}}
{{FuncParameters}}
{{FuncParam|EventName|__EDIT_ME__}}
{{FuncParam|EventName|The name of the event. See [[Template:ServerEvents|Server Events]] or [[Template:ClientEvents|Client Events]] for a list of events.}}
{{FuncParam|LuaFunction |__EDIT_ME__}}
{{FuncParam|LuaFunction |The function which will be called when the event occurs.}}
{{FuncParamOptional|OptionalBindId|__EDIT_ME__}}


{{FuncReturnValue|__EDIT_ME__}}
{{FuncReturnValue|This function returns '''true''' on success.}}


== Example ==
== Example ==
__EDIT_ME__
<syntaxhighlight lang="Lua">
function OnPlayerDeath(player, instigator)
        if instigator == nil then
        AddPlayerChat(player, "You died :(")
        else
            AddPlayerChat(player, "You have been killed by "..GetPlayerName(instigator))
        end
end
AddEvent("OnPlayerDeath", OnPlayerDeath)
</syntaxhighlight>


{{RelatedFunctions}}
{{RelatedFunctions}}
__EDIT_ME__
* [[CallEvent]]
* [[AddEvent]]
* [[CallRemoteEvent]]
* [[BroadcastRemoteEvent]]
* [[AddRemoteEvent]]

Latest revision as of 17:07, 30 December 2020

AddEvent

Type: Function
Context: Server & Client
Introduced: v1.0

Description

Adds a function as event handler which will be called when a certain event occurs. Multiple functions can be bound to a single event.

You can also specify new event names that do not exist.

Syntax

AddEvent(EventName, LuaFunction)

Parameters

  • EventName
    The name of the event. See Server Events or Client Events for a list of events.
  • LuaFunction
    The function which will be called when the event occurs.

Return Value

  • This function returns true on success.

Example

function OnPlayerDeath(player, instigator)
        if instigator == nil then
	        AddPlayerChat(player, "You died :(")
        else
            AddPlayerChat(player, "You have been killed by "..GetPlayerName(instigator))
        end
end
AddEvent("OnPlayerDeath", OnPlayerDeath)

See also