Community Forum

Key binding - problem with conversion from keyEvent to actionBinding

Forum Overview >> Scripting

CategoryScripting
Created14.03.2021 20:24


tomsche 14.03.2021 20:24
Hi,

the key bindings are causing me a lot of trouble.

This is working:
function MyMod:keyEvent(unicode, sym, modifier, isDown)
if bitAND(modifier, Input.MOD_CTRL) > 0
and bitAND(modifier, Input.MOD_ALT) > 0
and Input.isKeyPressed(Input.KEY_0) then
MyMod.myFunction();
end;
end;

function MyMod:myFunction()
print("Hooray! It works.");
end;

But now, I want to convert this to use "action" and "inputBinding". I can't figure out, how to use this in the game.

I added these lines to modDesc.xml:
<l10n>
<text name="input_CALL_MY_FUNCTION">
<en>Key Binding Test</en>
</text>
</l10n>

<actions>
<action name="CALL_MY_FUNCTION" category="PLAYER" axisType="HALF" />
</actions>

<inputBinding>
<actionBinding action="CALL_MY_FUNCTION">
<binding device="KB_MOUSE_DEFAULT" input="KEY_lctrl KEY_lalt KEY_0" />
</actionBinding>
</inputBinding>

A new entry is shown in the options menu. If I press ctrl+alt+0 in the game, nothing happens. I suppose, I have to add g_inputBinding:registerActionEvent somewhere in my lua script. But where?

This is my script:
---------------------------
--
-- MyMod
-- version 1.0.0.0
--

MyMod = {};

function MyMod:loadMap(name)
end;

function MyMod:keyEvent(unicode, sym, modifier, isDown)
-- if bitAND(modifier, Input.MOD_CTRL) > 0
-- and bitAND(modifier, Input.MOD_ALT) > 0
-- and Input.isKeyPressed(Input.KEY_0) then
-- MyMod.myFunction();
-- end;
end;

function MyMod:update(dt)
end;

function MyMod:draw()
end;

function MyMod:deleteMap()
end;

function MyMod:mouseEvent(posX, posY, isDown, isUp, button)
end;

function MyMod:registerEvents(vehicleType)
print("MyMod - registerEvents")
end;

function MyMod:registerActionEvents()
print("MyMod - registerActionEvents")
end;

function MyMod:myFunction()
print("MyMod - Hooray! It works.");
end;

addModEventListener(MyMod);
---------------------------


Who knows, what is missing?

Bilbo Beutlin (BBeutlin) 17.03.2021 05:14
Yes - actionEvents must be registered first, before they get evaluated. And for a continous loop a fired action must be registered again.
In a specialization this is quite easy via the 'SpecializationUtil.registerEventListener()'.
For a general script you must do this manually.

Setup a function where all necessary actionEvents are registered. Execute it at least once.
Example:
-----
function myMod:registerActionEvents()
local ok, eventId;
ok, eventId = InputBinding.registerActionEvent(g_inputBinding, InputAction.ACTION1, self, myMod.execACTION1, false, true, false, true);
if ok then g_inputBinding.events[eventId].displayIsVisible = true; end;
ok, eventId = InputBinding.registerActionEvent(g_inputBinding, InputAction.ACTION2, self, myMod.execACTION2, false, true, false, true);
if ok then g_inputBinding.events[eventId].displayIsVisible = true; end;
...
end
-----

There are several ways to re-register a fired action depending on your demands. You may use raiseActive() within the executed function. Or register a certain actionEvent from another function.
Quite comfortable to get a continous loop is appending to a system function.
Example:
-----
function myMod:loadMap(name)
FSBaseMission.registerActionEvents = Utils.appendedFunction(FSBaseMission.registerActionEvents, myMod.registerActionEvents);
end;
-----


Note: Log in to post. Create a new account here.