AddEvent: Difference between revisions

From Onset Developer Wiki
No edit summary
No edit summary
Line 13: Line 13:
== Example ==
== Example ==
<syntaxhighlight lang="Lua">
<syntaxhighlight lang="Lua">
AddEvent("Event Name", function()
function OnPlayerDeath(player, instigator)
    -- code here
        if instigator == nil then
end)
        AddPlayerChat(player, "You died :(")
        else
            AddPlayerChat(player, "You have been killed by "..GetPlayerName(instigator))
        end
end
AddEvent("OnPlayerDeath", OnPlayerDeath)
</syntaxhighlight>
</syntaxhighlight>


{{RelatedFunctions}}
{{RelatedFunctions}}
{{Template:EventFunctions}}
{{Template:EventFunctions}}

Revision as of 14:22, 31 December 2019

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.

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 has no specific return value.

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

Template:EventFunctions