Alternative to using a "Variable Holder" sprite?

Get help using Construct 2

Post » Sun May 20, 2012 2:38 am

I want to create several variables that I can modify and access from my event sheet. Global variables would be overkill since other event sheets can access them. Local variables don't work because they reset themselves every tick. The only way I've found is to use a variable holder sprite with several instance variables, which feels like a hack and makes the code look weird. Is there some functionality of Construct 2 that I've overlooked, or is this the only way to do it?

Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Sun May 20, 2012 4:01 am

Nah, no real alternative, I usually use global variable for that.
I already suggested 'Layout Variables' last November but, now that I think about it, it would rather be 'Event Sheet Variables' since each layout can share the same event sheet.
Or some kind of 'static local variable' if it's the right term. Local variable that doesn't get reinitialized.

Anyway, that's @Ashley 's call (:
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Sun May 20, 2012 11:03 am

Plugin Array without property global?
B
14
S
6
G
5
Posts: 173
Reputation: 4,742

Post » Sun May 20, 2012 2:47 pm

@Joe7
Yeah you can do that but unless you are super clean and keep track of which index of the array corresponds to which value, it's a pain (:
I always prefer some properly named variables.

Well now... With the Dictionnary plugin maybe... If you're organised such as initializing all the variable you'll use in one place in your event sheet. 'Cause you won't have a list of keys the same way you have a list of global/local variables =)Yann2012-05-20 14:51:11
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Sun May 20, 2012 8:35 pm

Thanks for your responses guys. An array would make it unmanageable by making make the code even harder to read. I don't want to use global variables because by the time I'm done with my game I could potentially end up with like a hundred globals and that just seems wrong to me since I'm not using them globally. I guess it doesn't seem like there is much of an alternative then.

Alright, I'll go with the Variable Holder sprite! It actually makes workflow pretty efficient. I could name it something like 'Vars', nice and short. Then I can refer to my variables like 'Vars.Distortion = 3' and the best part is that when I type Vars in the expression editor I'll get a list of all the variables I could use. So I really don't mind. It's actually faster than trying to remember the name of your global, or having to click System and select a global from a list. It just sucks that these have to be instance variables and not Event Sheet Specific variables. Perhaps if Ashley sees this as a useful feature, he'll go ahead and implement it. Til then, this is what I'll go with.
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Sun May 20, 2012 11:42 pm

Well just one thing then, I wouldn't use a sprite. Almost any object can have variables, but sprite are drawn objects that you must place, so you could potentially delete it by mistake, and it would just be another object to put somewhere on the side of your layout.
Then I would just use a non world object like array, dictionnary, or anything else that can hold instance variables. You can always call it vars (:
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon May 21, 2012 12:19 am

I would agree with Yann, just use global variables for the moment. An 'event sheet variable' would be useful, but I can understand the current setup as it keeps with common programming conventions.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Mon May 21, 2012 4:25 am

Oh, thanks Yann! I didn't realize arrays had instance variables. I'll probably go with that. Too bad I already used a sprite :P
Image
B
10
S
3
G
2
Posts: 196
Reputation: 2,053

Post » Mon May 21, 2012 6:42 am

I'd just use a global -- a "variable holder" sprite is accessible from other event sheets too, so there's really no benefit to that method.
B
24
S
5
G
2
Posts: 104
Reputation: 3,136

Post » Mon May 21, 2012 11:55 am

@jbadams
There's one, if the object isn't global, the variables are reset on start of layout. There is a "reset all global variables" but that's an all or nothing function. I like to keep some data (like score or current level) but reset some other (like enemy counter or some) and even have some different initial values for the same variable in different layouts (like base strength of ennemies or stuff like that)

Also the object is accessible from other event sheets yes, and that's fine, but it's not accessible in a layout where you didn't put the object. Which make the variable holder object truely 'by layout'. So you will just have to be carefull with included event sheet that uses these variables not to include them in event sheets of layout that doesn't have the object.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Next

Return to How do I....?

Who is online

Users browsing this forum: Colorfish, tatogame and 24 guests