Alternative to using a "Variable Holder" sprite?

0 favourites
  • 11 posts
From the Asset Store
Template for an alternative to falling shapes, fully documented in comments and video
  • 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?

  • 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 (:

  • Plugin Array without property global?

  • 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 =)

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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 (:

  • 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.

  • 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

  • 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.

  • 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.

  • I'd go with a Dictionary object. Then you have two options: use the instance variables C2 gives to every object, or use the string-based Dictionary storage (which might be useful for dynamic things).

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