Dictionary Keys Instead of Variables?

Discuss game development design and post your game ideas

Post » Fri Apr 05, 2013 2:21 pm

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?
B
6
S
3
G
1
Posts: 102
Reputation: 1,379

Post » Fri Apr 05, 2013 4:15 pm

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.rosareven2013-04-05 16:16:14
B
19
S
6
G
1
Posts: 116
Reputation: 2,730

Post » Fri Apr 05, 2013 4:18 pm

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?
B
6
S
3
G
1
Posts: 102
Reputation: 1,379

Post » Fri Apr 05, 2013 4:31 pm

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.
B
19
S
6
G
1
Posts: 116
Reputation: 2,730

Post » Fri Apr 05, 2013 4:39 pm

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.
B
6
S
3
G
1
Posts: 102
Reputation: 1,379

Post » Fri Apr 05, 2013 5:04 pm

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.
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Fri Apr 05, 2013 6:28 pm

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.
B
19
S
6
G
1
Posts: 116
Reputation: 2,730

Post » Fri Apr 05, 2013 7:12 pm

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.
B
6
S
3
G
1
Posts: 102
Reputation: 1,379

Post » Sat Apr 06, 2013 12:06 am

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.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Sat Apr 06, 2013 3:33 am

Awesome, that is pretty much what I was hoping to hear, thanks!
B
6
S
3
G
1
Posts: 102
Reputation: 1,379

Next

Return to Game Development, Design & Ideas

Who is online

Users browsing this forum: No registered users and 2 guests