Any plans to include Enum as an instance var type?

0 favourites
  • 8 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • Let's say you had an instance variable called state that can represent three states: HIDING, CHASING, or ATTACKING. Without Enums, the best approach is to use a Number for this variable, and then declare global constants in the following manner: HIDING = 0, CHASING = 1, ATTACKING = 2. Now you can use these constants in your code with full control over them in case things were to change.

    That works fine for one object, but now consider you have 15 different objects with different state values. That's a lot of global constants and can make things very unclear in the code. This issue could be eliminated if we had an Enum instance variable type. With an Enum, every state would be self-contained and it could be easily referenced in the code.

    Is this planned for a future release? If not, why not? Instance Enums would be handy ex. intelligent enemies (hiding, chasing...), characteristics (sharp, smooth), etc.

  • What if we allowed constant instance variables? Then you could have Player.ATTACKING, Player.CHASING as well as Enemy.ATTACKING, Enemy.CHASING etc. without spamming your global variables.

    Alternatively you can just use strings like "attacking", "chasing", "hiding" etc. Javascript takes this approach, and while it's susceptible to typos, it works surprisingly well and has the added convenience you don't need to do anything to define a new enum.

  • Constant instance variables (or 'instance constants'? :p) would be terrific in their own right. Even so, I think traditional 'Enums', for me, excel at 2 important things that make them especially worthwhile compared to other approaches:

    1. Forcing a single choice. An enemy can't hide and attack at the same time. In code, instead of having to specify a priority order, one could simply select 'State.HIDE' from the list and call it good - super clear and less prone to buggy logic - like using a Boolean instead of isTrue and isFalse vars (hopefully no one has ever done that). Plus, the idea of a neat little drop-down in the C2 user interface when testing against an Enum really excites me.

    2. Less prone to typos. True that Javascript enums are just strings, but I guess there's also the possibility of using the Object.freeze method to seal a property into an effective Enum, eliminating the typo issue. I feel like in many cases in JS games, this would be more desirable than using dynamic JS enums. Likewise, in C2 having an Enum in many cases would be preferred to using strings or constants.

    All in all, I think Enums would be an awesome addition to have down the line, but I see why this wouldn't be an immediate priority because there are a handful of alternate approaches.

  • What if we allowed constant instance variables? Then you could have Player.ATTACKING, Player.CHASING as well as Enemy.ATTACKING, Enemy.CHASING etc. without spamming your global variables.

    Alternatively you can just use strings like "attacking", "chasing", "hiding" etc. Javascript takes this approach, and while it's susceptible to typos, it works surprisingly well and has the added convenience you don't need to do anything to define a new enum.

    Hi Ashley.

    Yes you can use strings, but enums would be really nice because you can set them with integer values, and it is nice to code with enums, because it is much easier to read and you make fewer mistakes.

    So I would love to see enums in construct.

    And codecompletion on function names, so you dont make typing mistakes with function names

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I know this topic is old, but I put "enum" in the search and this is the first hit. I searched a few of the other "hits" yet no enum was referenced.

    Just wondering what happened to this idea... either ENUMs or Instance Constants, was/is there another alternative?

    EDIT: just realized this is in C2, but my question is really for C3

  • Are typos really that big of a problem? You'll mostly realize you have one the first time you press on preview.

    And do enumerations really have that much of an advantage for certain things? I would rather stay flexible and just use number variables.

    Booleans are the worst things to use imo, too. Why use a boolean instead of just using "0" and "1", which always allows you to add certain in-between steps as "2" etc.

  • 13spongg13 not sure what you are referring to regarding typos, I didn't mention anything about typos.

    there are many reasons for enums and bools, for me it's all about readability and being able to look at something months later and quickly get up to speed on what I was doing. It's also nice to be able to loop through enums.

    net-informations.com/java/basics/enum.htm

  • jobel

    With typos I was referring to the other comments above, but yeah I can see where enums might be useful.

    I do know what they are, but I think I never really used them actively myself.

    The more options there are, the more people can get comfortable with c2/c3. So it would be a nice addition, but they have probably so much to do already. I would also love a new version for the C2 steam greenlight plug-in.

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