Elseif is really alsoif

So I am liking Defold thus far but…

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…?

Else is a statement that runs only if the previous if statement did not return true. For instance,

Local redundant = 2

If redundant == 2 then
     redundant = redundant - 1
Else
     redundant = redundant + 1
End

This says, if redundant equals 2 then redundant is added to. ANYTHING ELSE (hence the Else statement) runs everything during the else statement.

Elseif is just an if statement that runs if the previous if statement was not met.

/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.

1 Like

could you find a more obscure and confusing illustration?

Oh yes. Though, I suppose could’ve made it more relateable like this:

Function on_input()
     If action_id == hash("text") then
          -- do something
     Else
          -- do something Else
     End
End

I described it the other way, because then I was able to describe what happened.

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!).

Also, as soon as you realise that “update” happens about 60 times per second, life becomes a lot easier.

(“on_input” happens a lot and very quickly as well, assuming you are holding a key, moving a mouse, etc)

Remember if you want to have something happen only once, you can put something like

If self.once== nil then
–something happens once
Self.once = 1
end

(And in some other part of your code, make sure you have self.once=nil again to reset it)

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.

The official Lua documentation has the most in depth explanation of tables: Programming in Lua : 2.5

A quick summary of tables can be found here: Learn Lua in 15 Minutes

3 Likes

Thank you very much!

1 Like