Tuesday, October 7, 2014

Construct 2: if-then-elseif-else statement

Do you have an idea how to implement IF-THEN-ELSEIF-ELSE statement with Construct 2? At least I had to think for a while how to do it. It seems that this basic structure is not so straightforward to implement in C2.

I was creating a very simple game where boxes were moving from left to right. See example below:


The color of each box is chosen randomly, so it required the usage of random() function and if-then-else statement. There are probably several ways to do this, but I wanted to make this as simple as possible inside one single event and action box. 

Normally I would create a if-then-elseif-else structure like this to select color for the box (pseudo-code just for an example):

if randomNumber == 0
      set colorOfBox = "red"
else if randomNumber == 1
      set colorOfBox = "yellow"
else if randomNumber == 2
      set colorOfBox = "green"
else
      set colorOfBox = "blue"
end if

But when using Construct 2 things were bit more complicated for me: tool is event based and I don't have deep knowledge about the possibilities of it. So I decided to find out a solution that fits for me.

In my implementation the output of random(0,4) function was stored to randomNumber variable and the color of the next box to be created was chosen according to this table:
value  color
  0    red
  1    yellow
  2    green
  3    blue

If you want to use if-then-else statement in the parameter field of some action box (like Animation field inside Set animation action), the syntax to be used is like this:
condition ? if_true : if_false

For example, the return value of statement is set to "true" if a<10. Otherwise return value is "false":
a<10 ? "true" : "false"

As you might notice, the statement above implements just the regular if-then-else structure. But how to make the "else-if" part, if such is required? My answer is to use nested statements, so if-then-else structures are chained like in following figure:


Interesting part is surrounded with red box. There are total of three chained if-then-else statements, which will select the color of the box according to the value of randomNumber variable (see the table above). The else branch of each if statement will be executed if the condition is false. True condition will return the value from "true" branch. I tried to clarify this a bit with the following figure:


Figure shows how the randomNumber value is checked in the if-then-elseif-else statement. Different stages of statement are marked with different colors.

If the value of randomNumber is 0, the color of the box will be red. If value is something else (not 0), execution moves to second stage and will check if the value is 1. If that is true, then the color of the box will be yellow. If value is still something else than 0 or 1, the third statement checks whether the value is 2. if that is true, box color is green. Any other value will make next box to be blue (This is so-called "else" -branch, that covers all condifions that have not been met in previous stage).

Everything is done inside one event, so my target was achieved with this solution. There are probably more clever ways to do this, so please let me know if you know one. :)

-Jussi

2 comments:

  1. Thanks, i was really looking for this... am still rather confused though.

    ReplyDelete
    Replies
    1. Yes, I believe this can be very confusing. C2 is really nice tool as far as you don't need to write any custom code. It took me a while to figure out how if-then-else statement can be done in C2. Still I am not too sure if I am doing it a correct way, but anyways it works. :)

      Delete