Difference between revisions of "RegisterEventHandler"

From The Official Visionaire Studio: Adventure Game Engine Wiki
(26 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<b>History</b>: Available since <span style="color:green">v3.7</span> <br/>
+
==registerEventHandler==
MovieVolume & GlobalVolume added to <span style="color:green">v3.8</span>
 
  
Allows the user to check current volume levels of: music, sound fx, speech, movies & global(master)
+
<div class="command-min-version-info">Available since: <span class="command-min-version">v3.6</span></div>
{| class="mw-collapsible mw-collapsed" style="background: #f0f0f0; border: 1px dashed darkgrey" width="100%"
 
! Additional Info
 
|-
 
|
 
These are the numbers used inside of the getVolume(brackets) to check specific volume types! <br/>
 
eMusicVolume (0), eSoundVolume (1), eSpeechVolume (2), eMovieVolume (3) or eGlobalVolume (4)
 
|}
 
  
Syntax:
+
<div class="command-doc">Registers an event handler function for a specific event.</div>
<syntaxhighlight>
 
registerEventHandler('eventName', 'functionName', {integerValue or eventFlagName})
 
</syntaxhighlight>
 
  
Example:
+
Lua Syntax:
 +
<pre class="command-syntax">registerEventHandler(event, eventHandler [, eventFlags])</pre>
 +
===Arguments===
 +
====event====
 +
:'''"string"''' - The event which triggers the event handler to be called. Currently supported events: "mainLoop", "mouseEvent", "keyEvent", "animationStarted", "animationStopped", "textStarted", "textStopped".
 +
====eventHandler====
 +
:'''"string"''' - The name of the lua function which is called when the event occurs.
 +
:
 +
:The function for the events "animationStarted", "animationStopped", "textStarted" and "textStopped" should take exactly one argument which is the affected visionaire object.
 +
:
 +
:There are no arguments for the "mainLoop" event handler.
 +
:
 +
:The function for "mouseEvent" takes two arguments, the first one is the mouse event (see "eventFlags" parameter) and the second argument is the current mouse position (a table containing x- and y-position).
 +
:
 +
:The function for "keyEvent" takes four arguments: eventType, character, keycode and modifiers. Return value must be true or false. If the function returns true, the key event will not be handled by the engine.
 +
:eventType: eEvtKeyDown (key pressed, can be fired multiple times for pressed key), eEvtKeyTextInput (text input, can be fired multiple times for pressed key) and eEvtKeyUp (key released, only fired once).
 +
:character: current text input of pressed key(s). This parameter only contains printable characters.
 +
:keycode: virtual keycode, only valid for eEvtKeyDown and eEvtKeyUp events. Can be used to query special keys like Esc or Shift.
 +
:modifiers: currently active key modifiers (e.g. Ctrl, Shift).
 +
====eventFlags====
 +
:'''{int,...}''' - Can only be specified for "mouseEvent": a list of mouse events where the event handler is called.
 +
:Currently the following mouse events are supported: eEvtMouseMove, eEvtMouseLeftButtonDoubleClick, eEvtMouseLeftButtonDown, eEvtMouseLeftButtonUp, eEvtMouseLeftButtonHold, eEvtMouseLeftButtonHolding, eEvtMouseRightButtonDoubleClick, eEvtMouseRightButtonDown, eEvtMouseRightButtonUp, eEvtMouseMiddleButtonDown, eEvtMouseMiddleButtonUp, eEvtMouseWheelUp and eEvtMouseWheelDown. If no mouse event is specified then the event handler is registered for all mouse events.
 +
===Flags===
 +
===Return Values===
 +
None.
 +
===Examples===
 +
Example 1: mainLoop
 
<syntaxhighlight>
 
<syntaxhighlight>
-- is music volume over 50%?
+
function onMainLoop()
if getVolume(0) > 50 then
+
-- do something
-- do some action!
 
 
end
 
end
  
-- is sound fx volume less than 25%?
+
registerEventHandler("mainLoop", "onMainLoop")
if getVolume(1) < 25 then
+
</syntaxhighlight>
-- do some action!
+
Example 2: mouseEvent
 +
<syntaxhighlight>
 +
function onMouseEvent(eventType, mousePosition)
 +
  if eventType == eEvtMouseWheelUp or eventType == eEvtMouseWheelDown then
 +
    -- mouse wheel was activated, do something
 +
  end
 
end
 
end
  
-- is speech volume more than or equal to 80%?
+
registerEventHandler("mouseEvent", "onMouseEvent", {eEvtMouseMove, eEvtMouseLeftButtonDoubleClick, eEvtMouseLeftButtonDown, eEvtMouseLeftButtonUp, eEvtMouseLeftButtonHold, eEvtMouseLeftButtonHolding, eEvtMouseRightButtonDoubleClick, eEvtMouseRightButtonDown, eEvtMouseRightButtonUp, eEvtMouseMiddleButtonDown, eEvtMouseMiddleButtonUp, eEvtMouseWheelUp, eEvtMouseWheelDown})
if getVolume(2) >= 80 then
+
</syntaxhighlight>
-- do some action!
+
Example 3: keyEvent
end
+
<syntaxhighlight>
 
+
function keyboardHandler(eventType, character, keycode, modifiers)
-- is movie volume at 50%?
+
  if eventType==eEvtKeyUp then
if getVolume(3) == 50 then
+
    print('key up: ' .. keycode)
-- do some action!
+
    -- test for '0' with character parameter
 +
    if character == '0' then print('0 released') end
 +
    -- another option to test '0' key
 +
    if keycode == 48 then print('0 released') end
 +
  elseif eventType==eEvtKeyDown then
 +
    print('key down: ' .. keycode)
 +
  elseif eventType==eEvtKeyTextInput then
 +
    -- this will also show more 'complex' unicode characters when multiple keys are used to generate a single character (e.g. Chinese characters)
 +
    print('input: ' .. character)
 +
  end
 +
  if keycode == eKeyEscape then
 +
    -- event will not be handled by engine. this means that also cutscenes can't be skipped with Escape key
 +
    return true
 +
  end
 +
  return false -- key event will also be handled by engine
 
end
 
end
  
-- is global (master) volume not equal to 50%?
+
registerEventHandler("keyEvent", "keyboardHandler")
if getVolume(4) ~= 50 then
 
-- do some action!
 
end
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
<b><u>Arguments</u></b>
 
 
type: integer <br/>
 
The type of volume to return: eMusicVolume (0), eSoundVolume (1), eSpeechVolume (2), eMovieVolume (3) or eGlobalVolume (4)
 
 
Flags: none
 
 
Return: volume <br/>
 
The queried volume
 
{{i18n|GetVolume}}
 

Revision as of 21:27, 30 September 2014

registerEventHandler

Available since: v3.6
Registers an event handler function for a specific event.

Lua Syntax:

registerEventHandler(event, eventHandler [, eventFlags])

Arguments

event

"string" - The event which triggers the event handler to be called. Currently supported events: "mainLoop", "mouseEvent", "keyEvent", "animationStarted", "animationStopped", "textStarted", "textStopped".

eventHandler

"string" - The name of the lua function which is called when the event occurs.
The function for the events "animationStarted", "animationStopped", "textStarted" and "textStopped" should take exactly one argument which is the affected visionaire object.
There are no arguments for the "mainLoop" event handler.
The function for "mouseEvent" takes two arguments, the first one is the mouse event (see "eventFlags" parameter) and the second argument is the current mouse position (a table containing x- and y-position).
The function for "keyEvent" takes four arguments: eventType, character, keycode and modifiers. Return value must be true or false. If the function returns true, the key event will not be handled by the engine.
eventType: eEvtKeyDown (key pressed, can be fired multiple times for pressed key), eEvtKeyTextInput (text input, can be fired multiple times for pressed key) and eEvtKeyUp (key released, only fired once).
character: current text input of pressed key(s). This parameter only contains printable characters.
keycode: virtual keycode, only valid for eEvtKeyDown and eEvtKeyUp events. Can be used to query special keys like Esc or Shift.
modifiers: currently active key modifiers (e.g. Ctrl, Shift).

eventFlags

{int,...} - Can only be specified for "mouseEvent": a list of mouse events where the event handler is called.
Currently the following mouse events are supported: eEvtMouseMove, eEvtMouseLeftButtonDoubleClick, eEvtMouseLeftButtonDown, eEvtMouseLeftButtonUp, eEvtMouseLeftButtonHold, eEvtMouseLeftButtonHolding, eEvtMouseRightButtonDoubleClick, eEvtMouseRightButtonDown, eEvtMouseRightButtonUp, eEvtMouseMiddleButtonDown, eEvtMouseMiddleButtonUp, eEvtMouseWheelUp and eEvtMouseWheelDown. If no mouse event is specified then the event handler is registered for all mouse events.

Flags

Return Values

None.

Examples

Example 1: mainLoop

function onMainLoop()
-- do something
end

registerEventHandler("mainLoop", "onMainLoop")

Example 2: mouseEvent

function onMouseEvent(eventType, mousePosition)
  if eventType == eEvtMouseWheelUp or eventType == eEvtMouseWheelDown then
    -- mouse wheel was activated, do something
  end
end

registerEventHandler("mouseEvent", "onMouseEvent", {eEvtMouseMove, eEvtMouseLeftButtonDoubleClick, eEvtMouseLeftButtonDown, eEvtMouseLeftButtonUp, eEvtMouseLeftButtonHold, eEvtMouseLeftButtonHolding, eEvtMouseRightButtonDoubleClick, eEvtMouseRightButtonDown, eEvtMouseRightButtonUp, eEvtMouseMiddleButtonDown, eEvtMouseMiddleButtonUp, eEvtMouseWheelUp, eEvtMouseWheelDown})

Example 3: keyEvent

function keyboardHandler(eventType, character, keycode, modifiers)
  if eventType==eEvtKeyUp then
    print('key up: ' .. keycode)
    -- test for '0' with character parameter
    if character == '0' then print('0 released') end
    -- another option to test '0' key
    if keycode == 48 then print('0 released') end
  elseif eventType==eEvtKeyDown then
    print('key down: ' .. keycode)
  elseif eventType==eEvtKeyTextInput then
    -- this will also show more 'complex' unicode characters when multiple keys are used to generate a single character (e.g. Chinese characters)
    print('input: ' .. character)
  end
  if keycode == eKeyEscape then
    -- event will not be handled by engine. this means that also cutscenes can't be skipped with Escape key
    return true
  end
  return false -- key event will also be handled by engine
end

registerEventHandler("keyEvent", "keyboardHandler")