Use a constant to initialize variables at design time?

0 favourites
  • 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi all, I'd like to use a constant to set a variable's value at design time. For example, say I have a bunch of state names, like:

    STATE_IDLE = "idle"

    STATE_RUN = "run"

    STATE_BADLY_NAMED = "fred"

    and in my character objects I have a variable called "State" that I want to set to one of these at design time. Currently I have to manually enter the State strings into the object properties by typing them in - I can't use the constant. So if I decide to change my constant for STATE_BADLY_NAMED from "fred" to "asdf", I have to remember to go through my objects and manually change all the "fred"s to "asdf"s in their properties too, which makes for errors. I also don't know a way to search through all object properties for a string inside the editor. It can be done in Windows, but it's a bit of a hassle.

    Does anyone know a trick to use constants in object properties, or a way to search object properties for a string from inside Construct?


  • You can create a bunch of events like this:

    If Character.State="STATE_RUN" -> Character set State to STATE_RUN

    Unfortunately, there is no other way to access variables by their string name.


    Also instead of the constant variables, you can use a dictionary:

    Key="STATE_RUN", value="run"

    KEY="STATE_IDLE", value="idle"

    Character Set animation to StatesDictionary.Get(Character.State)

  • Thanks for the suggestion dop2000, but I'm looking for a way to initialize instances of the same object with arbitrary values on an instance-by-instance basis, which can really only be done by setting their properties directly. I think it's time for me to write up a feature request!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • How is it different from what I suggested? You first create a "database" of states either with constants or a dictionary or some other data storage method.

    Then in the layout editor you initialize your instances with state code names - "STATE_RUN", "STATE_IDLE" etc.

    And then in runtime you lookup actual values for each state code in the dictionary or from constants using a bunch of "if" events.


    Or do you mean you want to select values for object's instance variable from the list, rather than type them?

  • Yes, exactly - I want some kind of idiot-proof drop down allowing me to populate variable values in the object properties using constants I've defined. I think in Unity such drop-downs are accomplished with enums, for example, but it's been awhile.

    I can see how your suggestion for setting variables using event sheet logic works for objects that all need to be initialized the same way - I am actually doing that in one case. But I'm referring to a bunch of object instances of the same type that need to be initialized with different values at design time. Another hypothetical example that's hopefully clearer:

    I have a bunch of doors in a building layout, and I want some of them to start locked, others to be open, others to be closed, and they are all based on a single "Door" object. I implement their open/locked/closed states by giving the Door object a string variable called "State", and using "open", "locked", "closed" strings as my state values. In my layout I then select the doors one by one and type whatever state I want into each of them. If I could set those variables using string constants (instead of repeatedly hand-typing them), they'd be less error-prone and easier to rename/refactor later on. Basically the exact same reason constants are used in event sheets, but applied to object properties at design time.

    Anyway this is a pipe dream for now, unless there's a really elegant workaround I'm overlooking.

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