Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Event (type): Difference between revisions

From FiguraMC
No edit summary
Style guide
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Distinguish|Events}}
The event type is used for events such as <code>events.TICK</code>. To create an event function, use one of the following.
The event type is used for events such as <code>events.TICK</code>. To create an event function, use one of the following.


<syntaxHighlight lang="lua">
<syntaxHighlight lang="lua"> local tick = 0
function events[EVENT]()
function events.WORLD_TICK()
  tick = tick + 1
end
end
-- OR
-- OR
events[EVENT]:register(function()
local tick = 0
end) -- optionally as a name argument to allow removal
events.WORLD_TICK:register(function()
  tick = tick + 1
end, "TICK_COUNTER")
</syntaxHighlight>
</syntaxHighlight>


Line 15: Line 20:
! | Description
! | Description
|-
|-
| remove
| [[#remove|remove]]
| Takes in a [https://lua.org/manual/5.1/manual.html#5.4 string] with the name of the event, and removes it, stopping it from being called
| Takes in a {{type|string}} with the name of the event, and removes it, stopping it from being called
|-
|-
| clear
| [[#clear|clear]]
| Clears the event of all methods
| Clears the event of all methods
|-
|-
| register
| [[#register|register]]
| Takes in a [https://www.lua.org/manual/5.1/manual.html#2.5.9 function] to run when the event is fired, with an optional name argument ([https://lua.org/manual/5.1/manual.html#5.4 string])
| Takes in a {{type|function}} to run when the event is fired, with an optional name argument ({{type|string}})
|-
|-
| getRegisteredCount
| [[#getRegisteredCount|getRegisteredCount]]
| Returns the amount of functions registered, with an optional name argument ([https://lua.org/manual/5.1/manual.html#5.4 string])
| Returns the amount of functions registered, with an optional name argument ({{type|string}})
|}
|}
== Fields ==
 
remove:
==== remove ====
# name : [https://lua.org/manual/5.1/manual.html#5.4 string]
----
register:
Removes a function from an event, returning how many were removed
# function : [https://www.lua.org/manual/5.1/manual.html#2.5.9 function]
 
# name : [https://lua.org/manual/5.1/manual.html#5.4 string] (optional)
Overloads:
getRegisteredCount:
{| class="wikitable"
# name : [https://lua.org/manual/5.1/manual.html#5.4 string] (optional)
|-
! Arguments
! Return
|-
| name: {{Type|string}}
| {{Type|integer}}
|-
| function: {{Type|function}}
| {{Type|integer}}
|}
<syntaxHighlight lang="lua">
local tick = 0
function tickThing()
  tick = tick + 1
  print("tick " .. tick)
  if tick > 5 then
    events.TICK:remove("thing:)
  end
end
events.TICK:register(tickThing, "tickThing")
</syntaxHighlight>
 
==== clear ====
----
Similar to remove, but removes every event
 
Overloads:
{| class="wikitable"
|-
! Arguments
! Return
|-
| none
| {{Type|nil}}
|}
<syntaxHighlight lang="lua">
-- Example function to clear all events
function clearEvents()
  for _, v in pairs(events:getEvents()) do
    v:clear()
  end
end
</syntaxHighlight>
 
==== register ====
Registers a function to an event
 
Overloads:
{| class="wikitable"
|-
! Arguments
! Return
|-
| func: {{Type|function}}, name: {{Type|string}}
| {{Type|Event}}
|-
| func: {{Type|function}}
| {{Type|Event}}
|}
<syntaxHighlight lang="lua">
local fps = 0
events.RENDER:register(function()
  fps = client.getFPS()
end)
</syntaxHighlight>
 
==== getRegisteredCount ====
Gets the number of registered functions in an event, optionally filtering by name
 
Overloads:
{| class="wikitable"
|-
! Arguments
! Return
|-
| name: {{Type|Integer}}
| {{Type|Event}}
|-
| none
| {{Type|Integer}}
|}
<syntaxHighlight lang="lua">
local fps = 0
events.RENDER:register(function()
  fps = client.getFPS()
end)
</syntaxHighlight>
 
[[Category:Types]]

Latest revision as of 20:21, 5 October 2024

Not to be confused with Events.

The event type is used for events such as events.TICK. To create an event function, use one of the following.

 local tick = 0
function events.WORLD_TICK()
  tick = tick + 1
end
-- OR
local tick = 0
events.WORLD_TICK:register(function()
  tick = tick + 1
end, "TICK_COUNTER")

Methods

Method Description
remove Takes in a string with the name of the event, and removes it, stopping it from being called
clear Clears the event of all methods
register Takes in a function to run when the event is fired, with an optional name argument (string)
getRegisteredCount Returns the amount of functions registered, with an optional name argument (string)

remove


Removes a function from an event, returning how many were removed

Overloads:

Arguments Return
name: string integer
function: function integer
local tick = 0
function tickThing()
  tick = tick + 1
  print("tick " .. tick)
  if tick > 5 then
    events.TICK:remove("thing:)
  end
end
events.TICK:register(tickThing, "tickThing")

clear


Similar to remove, but removes every event

Overloads:

Arguments Return
none nil
-- Example function to clear all events
function clearEvents()
  for _, v in pairs(events:getEvents()) do
    v:clear()
  end
end

register

Registers a function to an event

Overloads:

Arguments Return
func: function, name: string Event
func: function Event
local fps = 0
events.RENDER:register(function()
  fps = client.getFPS()
end)

getRegisteredCount

Gets the number of registered functions in an event, optionally filtering by name

Overloads:

Arguments Return
name: Integer Event
none Integer
local fps = 0
events.RENDER:register(function()
  fps = client.getFPS()
end)