Never use variables in C2, only dictionaries

0 favourites
  • 14 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • If you want to build something meaningfull in C2, never use variables, they are good for really simple stuff, if you rely on them heavily, you will be punished hard somewhere during the project, especially for using local variables.

    Most of the time because of lack of pointers and runtime declaration. I really feel sorry, that C2 variables lack this flexibility.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • This statement is very strange, as not accurate.

    At times, global variables are all you need for specific mechanics. I've used them in most of my videos in the C2 Academy and it makes sense, more than using a dictionary.

    At other times, local variables are just perfect when nested in a function or a specific block of sub-events and are all which is required. Dictionaries would actually make less sense at that moment.

    Unless you could add some specific case of use, I disagree and counter-advice what this topic is saying.

    Also I've always preferred arrays to dictionary anyway.

    So in the end, my advice would be use what you feel more comfortable with and what makes sense in your project/mechanic.

    I agree that it's probably best to use a data structure rather than dozens of global variables for some data usage, but again, that's not necessary what the OP seems to hint at.

  • Global and local variables are some of the best features of c2 in my opinion. Dictionaries have their uses but I seldom need to use them.

  • well, may be I was a bit emotional about that, depends on a project, but for something RPG-ish with lots of objects, stats etc - variables are no go.

    Variables in C2 are good for functional stuff, like storing some value intermediately, make some calculations and pass it forward. Everything which involves any type of persistence and dependencies is very inflexible when using variables, especially local once.

    May be I had the wrong mind set about variables? Should they be only used for this kind of stuff?

  • I manage realtime gameplay, cameras, UI, animation, etc with globals, I use dictionary to save settings and states, so it's easier to store and read again and do some for each when checking multiple objects states.

  • I think dictionary is very useful especially for things you have to save. I use dictionary for the things I need to save; save as JSON to LocalStorage, load from LocalStorage, so I have only 1 key. On the other hand, I use variables for what I don't need to save.

  • Just a comment and a question here--I use static variables sometimes, I set them so their scope overlaps a set of functions in a group and access them through function calls. The save and load function seems to save and load these just fine, kind of like accessing a variable with a get and set method.

    I can then include the file like an event file and access the variables via get and set and my functions still have them in scope while the rest of the routines in the new event file don't.

    Is this a common practice? If I used a global dictionary I guess I could do the same thing with get and set, but then all my variables would have global scope.

    Does save always save all static variables?

  • winkr7 as long as it works for you, I think it's fine to use this approach

    I think by using dictionary you define the scope of keys by the way you write access get/set logic.

    Yes, the keyword here is STATE, everything which is state related, which should be persistent and have dependencies, better implemented with dictionaries.

  • How do you pick using a dictionary without putting it in a container?

    BTW, everything can have a variable, even dictionaries.

    Also you can store strings in variables, which can act like an array using tokenat, and a few other expressions.

  • Yeah, that's one way to do it, but it's a nightmare to read in debug, I usually use strings with token to read data, but not maintain it in realtime.

  • Yeah, picking is another useful variable use case

  • I'm going to chime in that while dicitonary's and variables are mostly interchangeable in terms of function, variables are autofilled when entering expressions and dictionary keys are not. Instance variables are also logically tied to their objects for picking purposes. Local variables are automatically clean up after themselves. All of these together save me immeasurable amounts of time and brainpower (which unfortunately is always in limited supply).

    As for storing data in any significant quantity, I always resort to arrays as they are much more flexible in what you can do with them.

  • I use variables and arrays and I usually keep a word document to remind me what each do aside from the normal UI comment.

    This is because having a single document telling me all the available variables and arrays I have created and their existing usage is pretty helpful to avoid redundancy which in turn reduce the amount of work when you maintain and improve your project.

    I don't use dictionary since array is more useful to me due to official support and their functionality and speed is nearly similar.

  • I have external file with data to use with ajax and tokens. Having that kind of linear format of data works best for me. With external arrays there is so much to think about in terms of layout that I just can't be asked.

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