Multiple comparisons

0 favourites
  • 7 posts
From the Asset Store
See how your Logical Resolution looks on different devices
  • Hi

    I want to compare the value of a variable several times and do something different for each value, but I don't want to use the if statement repeatedly. Is there a formula for its average?

  • Can you give an example?

    There are some expressions you can use. For example:

    set result to chooseindex(var, "foo", "bar", "baz")

    set result to (var=0 ? "foo" : var=1 ? "bar" : "baz")

  • Example:

    if(a==1)

    {

    somthings//////////////////

    }

    if(a==2)

    {

    somthings//////////////////

    }

    if(a==3)

    {

    somthings//////////////////

    }

    if(a==4)

    {

    somthings//////////////////

    }

    if(a==5)

    {

    somthings//////////////////

    }

    ...

  • any body?

  • Can you give an example of "somthings//////////////////"?

    Some things will be possible to optimize, but you need to be more specific.

  • For example, for each value of variable 'a', a special subject should be created and different variables should be set.

    Of course, I mean how to reduce the 'if' statement and use an alternative statement. Like the 'switch case' command in 'Game Maker'.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 'Switch case' won't be much shorter than a bunch of 'ifs'.

    Like I said, you can use chooseindex or ternary operators to create objects by name:

    Create (a=1 ? "Zombie" : a=2 ? "Goblin" : a=3 ? "Troll" : "Skeleton")

    If you need to assign an instance variable you will have to add all these objects to a family and pick last created instance:

    Pick Last Created MonstersFamily

    MonstersFamily set health to (a=1 ? 5 : a=2 ? 10 : a=3 ? 50 : 3)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)