Dictionary Keys Instead of Variables?

0 favourites
  • 11 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • This is really plaguing me...

    I am currently not setting any local variables in my game because the dictionary is a global storage location in which I can set and retrieve values from any layout. The information is stored in RAM so it moves pretty fast. I have however been warned, "not to use global variables when I don't need to as a general rule", but no one can really tell me why.

    -If I am only using one dictionary,

    -I make sure not to use the same key twice,

    -the dictionary repository stays relatively small (100kb,)

    -I am saving the dictionary to webstorage every 30 seconds or every layout change,

    What is danger I should be wary of?

    Is there something I am not understanding that will cause the inevitable demise of my game if I keep everything as a Dictionary Keys?

  • I think global variable is referring to the variable unit on top of the event sheet, not global storage like dictionary. In contrary, I see a lot of people recommending global variables as the fix of a problem, especially for transferring data across layouts.

    What I suspect the problem is that, if you have a lot of layouts doing their own things and you use global variables to do this, the scope of the variable becomes blurry quickly. Like, a monster in layout 1 has a global health of 10. You hit it and it's reduced to 5. In layout 2 that's supposed to be reset to 10 but its health is going to stay at 5 because of what happened in layout 1.

    Dictionary and array, I believe, are highly encouraged. You can even make multiple dictionaries instead of just one depending on how you want to organise the data.

  • Ok, so I am using the Dictionary to house all my variables and not using any local variables, is there anything wrong with that logic?

  • That's also what I'm doing so I don't suppose there's anything wrong with that. Variables are easier to retrieve via point and click whereas you'd have to type out Dictionary.At everytime you retrieve something from the dictionary, but it won't make a difference.

  • Yeah, dictionary is fantastic because I can pre-type in all my creature stats as JSON into an imported notepad file then tell the dictionary to just pull it all in when the player starts a new game. Also makes a very centralized place to access all the games variables(keys). Just feels really tidy to me.

    When I am working I have the JSON file open next to my event editor next to my chrome test window for maximum productivity.

  • I don't know why anyone told you not to use global variables, there's no problem using them. Local variables as well. Simply use whatever is best suited for the task at hand. For example, I used to use the dictionary object for temporary variables is CC, but since C2 has local variables, I find those more convenient instead.

  • I do remember being told not to use globals in programming outside of C2, such as Java and C++. Forgot what the reason was now though.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yeah it non-Construct2 programmers telling me global variables are bad. That is pretty much what I wanted to confirm is that they were taught that way and that there isnt really a good reason not to as long as your consistent and precise.

    Like dont create a global variable and a local variable with the same name which I dont even think should be a problem in CS2 but in direct language programming it is.

  • Well, the inappropriate using of global variable in JavaScript can lead to memory issues.

    But it doesn't apply to C2 since global variables in C2 are part of the engine and the engine does take care of most memory management issues so you don't have to think about it.

  • Awesome, that is pretty much what I was hoping to hear, thanks!

  • I do remember being told not to use globals in programming outside of C2, such as Java and C++. Forgot what the reason was now though.

    Wasn't it something about taking up too many resources? If all variables are global in C++ code, then that memory will always stay used. Whereas the local variables are created and destroyed as needed.

    Not sure if that is 100% right...I only play a programmer on TV!

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