Community Forum

Getting SYM Code from input Binding

Forum Overview >> Farming Simulator 19

CategoryFarming Simulator 19
Created03.09.2020 13:16


Harag Harag (Harag) 03.09.2020 13:16
HI

Fairly new to modding.

I've got a :keyEvent(unicode, sym, modifier, isDown) - working, it fires every time the user presses a key. which is great.

However, I don't want to hard code the SYM codes into the script

e.g. if sym==97 then.

What I would like to do is get the symbol codes from the modDesc.xml file that have been setup (and possibly altered by the user).

e.g.
<actionBinding action="MoveLeft">
<binding device="KB_MOUSE_DEFAULT" input="KEY_lctrl KEY_left"/>
</actionBinding>

I've worked out I can do the following in the keyEvent:

local action - InputAction.MoveLeft

if sym == action.??? and modifier == action.??? then ....

but I don't know how to test that the sym / modifier is the same as the keys they have defined in "MoveLeft"

Any help on this would be much appreciated.

Bilbo Beutlin (BBeutlin) 03.09.2020 20:45
If you want to use actionBindings, the keyEvent() is the wrong approach.
Instead use the new method with 'registerActionEvent()' where you can assign specific inputAction directly to your LUA functions.
You'll find many examples in the official docs, but also in many script mods around.

Harag Harag (Harag) 03.09.2020 21:42
Hi Bilbo.

Many thanks for that. I'm trying to change a mod I have that uses the action binding and the "registerActionEvent()" you mentioned

>>objectName:actionCallback(actionName, keyStatus, callbackState, isAnalog, isMouse, deviceCategory)

when I was testing it, the function was only called once when I pressed the key, but I need it to call twice, once on down and once on key up... In the keyEvent() there is a "isDown" parameter which I wanted to use. so when the user presses the key down I can set a flag, then in the update if the flag is set do stuff. then when they release the key the event is called again and I can unset the flag.

Using the action I can't seem to reproduce this.

print("--- actionCallback: ".. tostring(actionName).. " --- keyStatus:".. tostring(keyStatus).. " --- ")



Bilbo Beutlin (BBeutlin) 04.09.2020 01:29
The actionEvent is meant for 'one-shot'. If you want it in a loop, you must re-register it.
There are several ways for that, depending on kind of script. Main difference is between vehicle specialization or general script. See examples in docs and mods.

However a key actionEvent is fired only on a defined actionBinding, usually on key-down. For key-up you must define a separate actionBinding and actionEvent.

Harag Harag (Harag) 04.09.2020 11:35
Hi.

Thanks for that, that helped a lot. I was looking at the below code

local _, eventName = self:addActionEvent(self.actionEvents, InputAction[actionName], self, myObject.actionCallback, p1, p2, p3, p4, nil);

but I couldn't find what all the parameters of the addActionEvent were. The original code was just passing the variables p1 to p4 with true/false values... Anyway after some playing around I worked them out and got this:

local _, eventName = self:addActionEvent(self.actionEvents, InputAction[actionName], self, myObject.actionCallback, triggerKeyUp, triggerKeyDown, triggerAlways, isActive, nil);

So by changing the p3 to true (triggerAlways) it was firing the actionCallBack while holding the key down.

Now my next problem...

It's TOO FAST, so I need the deltaTime / game time -so I can do a calculation between frames so when the method fires I can put code in there to only run say every 500ms.

How do I get the deltatime? -- I know it's passed through to certain methods (update, draw etc.) by the engine. but is there a way just to get the value?

Bilbo Beutlin (BBeutlin) 04.09.2020 12:35
The lua engine uses often (float) 'g_currentMission.time' - but I don't know about precision.

In 'update' functions you can use 'dt' (delta ticks in ms) which gives the time since last call. See in docs 'update..(dt)'.

Harag Harag (Harag) 04.09.2020 13:00
Thanks. I'll take a look at the g_currentMission.time you mentioned. However, I've done a little bit of "monogame" messing around in the past so I know that the update / draw functions pass the dt, (guessing it's the same for this engine) so had a look at the update and yes as you said the dt is passed...

So I've stored that in a variable on the object and use that... Great. I've now got it working and it only fires the code every 100ms within the action callback.


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