I do not understand how the code in War Battles ‘Player Movement’ can work. As far as I can tell, the elseif is really working as alsoif, which is causing some confusion… It should NOT work as desired by any definition of elseif I can find.
Is it supposed to happen in Lua 5.1? Defold only?
Is it because of where it is at in the code?
If it’s a feature can it be renamed alsoif?
what am I missing…
Is it the way the input is brought into the function (table) - so it is really called more than once sort of simultaneously…?
/giggles
could you find a more obscure and confusing illustration?
I get it now,
function on_input(self, action_id, action)
----if then elseif block
end
it is not the elsif that is confusing, it is the function which iterates for every action, which is explained in the tutorial.
The on_input() function is called every frame for all mapped input that is active. The argument action_id contains the action as set up in the input bindings file. The argument action is a Lua table with details on the input.
It only seems to be acting as alsoif because the function is called for every active input (up to 5 times in this example). There is a sequential order (similar to a for loop) though it almost appears simultaneous.
Within on input, don’t forget you have booleans for action.pressed, and action.released. Those are only triggered once (when the key is pressed and when the key is released). That may be more appropriate for your code. If you had included your code and your desired outcome, you probably would have got more specific advice (in my experience, most programmers are unable to view a beginner’s code without suggesting a few improvements!).
This is not true. on_input() is called once per input event. An input event could be the move of the mouse, the touch of a finger, a press of a game pad button or a key on a keyboard.
This means that if you press two keys on the keyboard simultaneously you get two calls to on_input on the frame when the keys were pressed (action.pressed is true).
If you keep holding down on the keys you’ll get repeated key events as described here: Device input in Defold
There is nothing called alsoif in the Lua programming language. Most programming languages have a syntax for running conditional code, and in most of them it is if-elseif-else (possible with a space between the else and the if).
Ah thanks. Do you have handy a nice explanation of tables? (lazily searching provided link produces a lot of results I will want to read eventually)
alsoif
Well I do not think there is an alsoif in any language, I was just describing how it appeared to be acting, before I had a slightly better understanding of how on_input() behaves. On further reflection it would be hard to find something more useless, but that has never stopped people from trying.