Difference between revisions of "Global Command: registerEventHandler"
(Created page with "Registering an event handler allows you to run custom functions on specific engine events. {| class="ts" |- | style="width:15%" | Related functions | Global Command: unregi...") |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
== Syntax == | == Syntax == | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | registerEventHandler( | + | registerEventHandler(eventType, handler [, flags]) |
</syntaxhighlight> | </syntaxhighlight> | ||
Line 23: | Line 23: | ||
! colspan="2" | Description | ! colspan="2" | Description | ||
|- | |- | ||
− | | rowspan="9" | | + | | rowspan="9" | eventType |
| rowspan="9" | string | | rowspan="9" | string | ||
| "actionArea" | | "actionArea" | ||
| Event fires when a character enters or leaves an action area. | | Event fires when a character enters or leaves an action area. | ||
− | | rowspan="9" style="width:15%" | The type of event which triggers the event handler to be called | + | | rowspan="9" style="width:15%" | The type of event which triggers the event handler to be called. |
|- | |- | ||
| "animationStarted" | | "animationStarted" | ||
Line 45: | Line 45: | ||
|- | |- | ||
| "mouseEvent" | | "mouseEvent" | ||
− | | Event fires when the user uses the mouse. | + | | Event fires when the user uses the mouse or fingers/gestures on touch devices. |
|- | |- | ||
| "textStarted" | | "textStarted" | ||
Line 61: | Line 61: | ||
| table of integers | | table of integers | ||
| ''see the list of mouse events in the table below'' | | ''see the list of mouse events in the table below'' | ||
− | | colspan="2" | A list of mouse events upon which the event handler is called. Only applicable if | + | | colspan="2" | A list of mouse events upon which the event handler is called. Only applicable if eventType = "mouseEvent". If no mouse event is specified, the event handler is registered for all mouse events. |
|} | |} | ||
Line 119: | Line 119: | ||
|- | |- | ||
| keycode (int) | | keycode (int) | ||
− | | | + | | Keycode of the pressed key (only valid for "eEvtKeyDown" and "eEvtKeyUp" events). Refer to the [[Keycodes_%26_Modifiers|list of keycodes]]. |
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| modifiers (int) | | modifiers (int) | ||
− | | The currently active key modifiers (only valid for "eEvtKeyDown" and "eEvtKeyUp" events) | + | | The currently active key modifiers (only valid for "eEvtKeyDown" and "eEvtKeyUp" events). Refer to the [[Keycodes_%26_Modifiers#Modifiers|list of modifiers]]. |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| "mainLoop" | | "mainLoop" | ||
Line 148: | Line 128: | ||
| colspan="2" | | | colspan="2" | | ||
|- | |- | ||
− | | rowspan=" | + | | rowspan="5" | "mouseEvent" |
| eventType (int) | | eventType (int) | ||
| colspan="2" | The mouse event which triggered the function. Supported values: | | colspan="2" | The mouse event which triggered the function. Supported values: | ||
Line 170: | Line 150: | ||
* eEvtTouchMove (18) | * eEvtTouchMove (18) | ||
|- | |- | ||
− | | | + | | position (t_point) |
− | | colspan="2" | A table (associative array) containing the elements '''x (int)''' and '''y (int)'''. | + | | colspan="2" | The mouse position or the normalized center of the gesture (for "eEvtMultiGesture" and "eEvtDollarGesture" events), respectively. A table (associative array) containing the elements '''x (int)''' and '''y (int)'''. |
+ | |- | ||
+ | | fingers1 (int) | ||
+ | | colspan="2" | Only valid for "eEvtMultiGesture" and "eEvtDollarGesture" events: | ||
+ | * for "eEvtMultiGesture": the amount that the fingers pinched during the motion | ||
+ | * for "eEvtDollarGesture": the difference between the gesture template and the actual performed gesture (the lower the number, the better the match) | ||
+ | |- | ||
+ | | fingers2 (int) | ||
+ | | colspan="2" | Only valid for "eEvtMultiGesture" and "eEvtDollarGesture" events: | ||
+ | * for "eEvtMultiGesture": the amount that the fingers rotated during the motion, in radians | ||
+ | * for "eEvtDollarGesture": the number of fingers used in the gesture | ||
+ | |- | ||
+ | | fingers3 (int) | ||
+ | | colspan="2" | Only valid for "eEvtMultiGesture" and "eEvtDollarGesture" events: | ||
+ | * for "eEvtMultiGesture": the number of fingers used in the gesture | ||
+ | * for "eEvtDollarGesture": the unique id of the closest gesture to the performed stroke | ||
|- | |- | ||
| "textStarted" | | "textStarted" | ||
Line 203: | Line 198: | ||
'''Example 2:''' Register an event handler function for mouse events which is called when the user uses the scroll wheel. | '''Example 2:''' Register an event handler function for mouse events which is called when the user uses the scroll wheel. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | function onMouseEvent(eventType, | + | function onMouseEvent(eventType, position) |
if eventType == eEvtMouseWheelUp then | if eventType == eEvtMouseWheelUp then | ||
print("User scrolled up.") | print("User scrolled up.") | ||
Line 245: | Line 240: | ||
'''Example 4:''' Register an event handler function which gets executed after loading a savegame (be aware that this is only an example; the "read_ini()" function doesn't exist unless you created it). | '''Example 4:''' Register an event handler function which gets executed after loading a savegame (be aware that this is only an example; the "read_ini()" function doesn't exist unless you created it). | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | function onEngineEvent(event, path) | + | function onEngineEvent(event, path) |
− | if event == "LoadingSavegameSuccess" then | + | if event == "LoadingSavegameSuccess" then |
read_ini() | read_ini() | ||
− | end | + | end |
− | end | + | end |
+ | |||
registerEventHandler("engineEvent", "onEngineEvent") | registerEventHandler("engineEvent", "onEngineEvent") | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 11:09, 13 September 2023
Registering an event handler allows you to run custom functions on specific engine events.
Related functions | unregisterEventHandler |
Syntax
registerEventHandler(eventType, handler [, flags])
Parameters
Parameter | Type | Supported values | Description | |
---|---|---|---|---|
eventType | string | "actionArea" | Event fires when a character enters or leaves an action area. | The type of event which triggers the event handler to be called. |
"animationStarted" | Event fires when an animation gets started. | |||
"animationStopped" | Event fires when an animation gets stopped. | |||
"engineEvent" | Event fires on certain engine events. | |||
"keyEvent" | Event fires when the user presses a key on the keyboard or gamepad. | |||
"mainLoop" | Event fires continuously about 60 times per second. | |||
"mouseEvent" | Event fires when the user uses the mouse or fingers/gestures on touch devices. | |||
"textStarted" | Event fires when a text gets displayed. | |||
"textStopped" | Event fires when a text display ends. | |||
handler | string | The name of the function which is called when the event occurs. See the table below for the arguments the function takes. | ||
flags | table of integers | see the list of mouse events in the table below | A list of mouse events upon which the event handler is called. Only applicable if eventType = "mouseEvent". If no mouse event is specified, the event handler is registered for all mouse events. |
Event handler function
Depending on the event type, the event handler function takes different arguments.
Event type | Arguments | Description | |
---|---|---|---|
"actionArea" | movement (str) | The event which triggered the function. Supported values:
| |
actionArea (TVisObj) | The action area that triggered the event. | ||
character (TVisObj) | The character that triggered the event. | ||
"animationStarted" | animation (TVisObj) | The animation which gets started. | |
"animationStopped" | animation (TVisObj) | The animation which gets stopped. | |
"engineEvent" | event (str) | The engine event which triggered the function. Supported values:
| |
path (str) | The full path to the loaded savegame file.
Example (Windows): "C:\Users\USERNAME\AppData\Local/DEVELOPER/GAME/Savegames/savegame00.dat" | ||
"keyEvent" | eventType (int) | The key event which triggered the function. Supported values:
|
The function must return a boolean. If true the key event will not be handled by the engine. |
character (str) | The text input of the pressed key. This parameter only contains printable characters. | ||
keycode (int) | Keycode of the pressed key (only valid for "eEvtKeyDown" and "eEvtKeyUp" events). Refer to the list of keycodes. | ||
modifiers (int) | The currently active key modifiers (only valid for "eEvtKeyDown" and "eEvtKeyUp" events). Refer to the list of modifiers. | ||
"mainLoop" | none | ||
"mouseEvent" | eventType (int) | The mouse event which triggered the function. Supported values:
| |
position (t_point) | The mouse position or the normalized center of the gesture (for "eEvtMultiGesture" and "eEvtDollarGesture" events), respectively. A table (associative array) containing the elements x (int) and y (int). | ||
fingers1 (int) | Only valid for "eEvtMultiGesture" and "eEvtDollarGesture" events:
| ||
fingers2 (int) | Only valid for "eEvtMultiGesture" and "eEvtDollarGesture" events:
| ||
fingers3 (int) | Only valid for "eEvtMultiGesture" and "eEvtDollarGesture" events:
| ||
"textStarted" | text (TVisObj) | The text object which gets displayed. | |
"textStopped" | text (TVisObj) | The text object which gets stopped displaying. |
Return values
none
Examples
Example 1: Register an event handler function for the main loop, printing a text to the log file continuously.
function onMainLoop()
print("I am still alive!")
end
registerEventHandler("mainLoop", "onMainLoop")
Example 2: Register an event handler function for mouse events which is called when the user uses the scroll wheel.
function onMouseEvent(eventType, position)
if eventType == eEvtMouseWheelUp then
print("User scrolled up.")
elseif eventType == eEvtMouseWheelDown then
print("User scrolled down.")
end
end
registerEventHandler("mouseEvent", "onMouseEvent", {eEvtMouseWheelUp, eEvtMouseWheelDown})
Example 3: Register an event handler function for key events.
function keyboardHandler(eventType, character, keycode, modifiers)
-- Two different ways of checking for the 0 key (by "character" or by "keycode" argument)
if eventType == eEvtKeyDown then
if character == "0" then
print("0 pressed")
end
elseif eventType == eEvtKeyUp then
if keycode == 48 then
print("0 released")
end
end
-- disable the "Esc" key (cutscenes can't be skipped)
if keycode == eKeyEscape then
-- return true to prevent the engine from handling the key event
return true
end
-- return false to let the engine handle the key event, too
return false
end
registerEventHandler("keyEvent", "keyboardHandler")
Example 4: Register an event handler function which gets executed after loading a savegame (be aware that this is only an example; the "read_ini()" function doesn't exist unless you created it).
function onEngineEvent(event, path)
if event == "LoadingSavegameSuccess" then
read_ini()
end
end
registerEventHandler("engineEvent", "onEngineEvent")
Example 5: Register an event handler function for the "textStarted" event. It keeps the user from skipping texts accidentally.
function delaySkipText(text)
if not text.Background then
-- Prevent text skipping
game.AlwaysAllowSkipText = false
-- Allow skipping again after 300ms have passed; create a non-skippable cutscene for
-- that period of time by hiding the cursor, if no cutscene is currently active
if game.HideCursor == false and game.CutsceneAction:isEmpty() then
game.HideCursor = true
setDelay(300, function() game.AlwaysAllowSkipText = true; game.HideCursor = false end)
else
setDelay(300, function() game.AlwaysAllowSkipText = true end)
end
end
end
registerEventHandler("textStarted", "delaySkipText")