Never use variables in C2, only dictionaries

Discussion and feedback on Construct 2

Post » Tue Feb 23, 2016 10:16 am

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.
B
45
S
11
G
4
Posts: 517
Reputation: 7,398

Post » Tue Feb 23, 2016 10:23 am

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

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
296
S
119
G
96
Posts: 7,332
Reputation: 71,306

Post » Tue Feb 23, 2016 10:46 am

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.
B
94
S
33
G
118
Posts: 5,394
Reputation: 75,849

Post » Tue Feb 23, 2016 10:50 am

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?
B
45
S
11
G
4
Posts: 517
Reputation: 7,398

Post » Tue Feb 23, 2016 1:05 pm

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.
Image
B
82
S
30
G
12
Posts: 517
Reputation: 12,763

Post » Tue Feb 23, 2016 1:10 pm

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.
B
32
S
7
G
4
Posts: 400
Reputation: 4,595

Post » Tue Feb 23, 2016 2:18 pm

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?
B
34
S
8
G
3
Posts: 169
Reputation: 3,305

Post » Tue Feb 23, 2016 2:41 pm

@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.
B
45
S
11
G
4
Posts: 517
Reputation: 7,398

Post » Tue Feb 23, 2016 3:41 pm

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.
Image ImageImage
B
172
S
50
G
183
Posts: 8,442
Reputation: 115,603

Post » Tue Feb 23, 2016 3:49 pm

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.
Image
B
82
S
30
G
12
Posts: 517
Reputation: 12,763

Next

Return to Construct 2 General

Who is online

Users browsing this forum: Palnix777 and 7 guests