0 Favourites

Boolean or Number?

  • Hello constructors,

    When you using variables to define the object states, which one you prefer to use, boolean or number? It seems number can do what boolean can do and it's more flexible to extend in the future. For boolean I need to add more variable, for number I can just set A=0, A=1, A=2... its just not that intuitive and needs more comments.

  • Boolean is usually easier to read and makes projects easier to manage, I use them if applicable. I also prefer strings over number states: "walking", "running", "crawling" is 100x easier to read than 1,2,3- describing states.

  • Good suggestion for strings. since I got a lot of boolean variables and stop using some of them after the change, I'm thinking about using string instead.

  • i'm happy using numbers for yes/no variables. but in some circumstances boolean is all you. if something is either one thing or another, and it's never going to change than why not use boolean.

  • Look at it this way; when you try to feed the boolean a number or a string, it throws an error. That's one way to prevent nasty bugs.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • vee41 - does C2 minify/obfuscate strings on export?

    "Walking", "Running", "Crawling" may be 100x easier to read, but they're 100x less efficient when doing variable tests (i.e. if state="walking" rather than if state=2)

    In most languages, this is exactly why you'd use enums...

  • vee41 - does C2 minify/obfuscate strings on export?

    "Walking", "Running", "Crawling" may be 100x easier to read, but they're 100x less efficient when doing variable tests (i.e. if state="walking" rather than if state=2)

    In most languages, this is exactly why you'd use enums...

    @tanoshimi

    I highly doubt there is anything relevant performance in how you do comparisons. To me it's about usability and managing your code. :) The way Ashley has described C2 performance, it should not really matter how you handle simple comparisons as performance bottlenecks are always somewhere else.

  • Use global and local variables for naming things.

    For instance your enemy might have a variable called "state" or such, and then you create some global/local variables called:

    idle = 0

    run = 1

    follow = 2

    This way you can just refer to names rather than numbers in equations so that your math is easy to read and adjust.

  • The performance of comparing strings vs. numbers is irrelevant. Despite that the best solution is probably alspal's! If you mark the variables const then that guarantees they cannot be changed, and also prevents them appearing in the 'Set variable' dropdowns.

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