How do you handle Booleans in Function parameters?

0 favourites
  • 5 posts
  • What's the best way to set a boolean in a Function call based on a variable value?

    I can only see the "tick box" in the Function call dialog, which is only set by the programmer.

    My current solution is a pair of if/else statements for each boolean:

    • If sprite.boolean, call Function with the boolean tick-box ticked.
    • Else call Function with the boolean tick-box unticked.

    Now I'm thinking all the Booleans parameters should just be Numbers-but doesn't that defeat the purpose of having Boolean parameters at all?

  • Not sure what you're asking? When you call the function you select the parameter to send through the function, in this case whether a boolean is true or false. Then within the function you do something based on the boolean value.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Sorry, what I'm asking is how people deal with calling functions with boolean parameters when the boolean value isn't known.

    Ideally boolean parameters wouldn't be a tick-box, but a value field, like string and number parameters are. That way you could use a variable value at runtime. Instead it's hard coded by the programmer.

    E.g. Say function PrepareMeal has three booleans: eatsMeat, eatsVeg, eatsFish. And say the customer object has the same three boolean variables, but each customer has a different combination of true/false. You'd ideally pass through the customer objects' boolean values into a single function call, like "PrepareMeal(cust.eatsMeat,cust.eatsVeg,cust.eatsFish)", but functions don't accept variables for booleans.

    So it seems like you either have two options:

    1. Use a bunch of if/else statements calling the function with different combinations of booleans.

    2. Use a Number parameter instead of a boolean, which can accept a variable.

    Option 2 seems best, it just felt weird to not use boolean parameters for boolean variables.

    So I thought I'd ask if there was a better approach to this before converting all these if/else groups to functions using numbers instead.

  • From what you're describing it's probably better to pass through the UID of the instance into the function then within the function you can compare the bools and return a value. So the if/else comparisons trying to determine some final value would be inside the function.

  • Right, that makes good sense. And certainly minimizes the amount of if/else comparisons. Thanks very much!

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