(Possible Suggestion) Group Global Variables

Get help using Construct 2

Post » Thu Nov 14, 2013 5:01 am

I'd like to know the best method for grouping a series of global variables together.

To explain better, I'd like to make a game that includes many different variables, grouped into different categories, such as:

Intelligence
     Learning
     Knowledge
     Memory

Physical
     Strength
     Agility
     Speed

The list goes on. As you can see, I currently have 6 as shown above, and would eventually refer to these frequently. I'd like to be able to group them so that I can locate them a lot quicker.

Is the best way to do this to just make a sprite or something, name it the group, and work with instance variables instead? The problem with this method is that I want these 'stats' to remain throughout, so I guess I could make the sprites a global object, but I was hoping there might be an easier way someone's thought of.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
79
S
12
G
7
Posts: 963
Reputation: 10,721

Post » Thu Nov 14, 2013 5:11 am

I have two approaches:

I have an event sheet reserved for globals. Then they are all together in their own file. Since they are globals, I don't have to worry about including it in all my other event sheets, but I can reuse them for different layouts.

However, if I need an object to hold the variables, I've found a size zero array works well (set the Width to 0, leave Height and Depth at 1). I can give it instance variables for the globals, set it to persist, and I don't have to give it a space in the layout.


I might have misunderstood you, though.
B
15
S
5
G
2
Posts: 24
Reputation: 2,054

Post » Thu Nov 14, 2013 5:27 am

@PixelHero

I like the idea of using the array. How do you add persist? You mean just global? You can't add behaviours to arrays.

I've recently started using that first method, and I'd rather use global events as I feel it would make things easier for me in the long run, but without groups, it will turn into a massive list when I come to call them.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
79
S
12
G
7
Posts: 963
Reputation: 10,721

Post » Thu Nov 14, 2013 7:46 am

i know it isn't as organized as groups would be but you could do something like this IN_learning or PHY_Strength
thats what i do and it helps and makes edits to related objects easy because they are all listed in order together
B
42
S
17
G
2
Posts: 850
Reputation: 6,209

Post » Thu Nov 14, 2013 11:15 am

Perhaps you could use some global objects with instance variables to simulate grouping variables. For example a sprite called 'Intelligence' allows you to use Intelligence.Learning, Intelligence.Knowledge and Intelligence.Memory. A sprite called 'Physical' allows you to use Physical.Strength, Physical.Agility, Physical.Speed. Etc...
Scirra Founder
B
395
S
232
G
88
Posts: 24,371
Reputation: 193,772

Post » Thu Nov 14, 2013 3:01 pm

@Ashley - Would a global array be better than a global sprite? I need to add sprites to layouts, and I'm wondering if I will need to have the sprite appear at least once, or every time I want to make a change, will I need the object to appear?
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
79
S
12
G
7
Posts: 963
Reputation: 10,721

Post » Thu Nov 14, 2013 3:14 pm

You don't need, make a new layout (normally the loading screen), leaving it with your sprites inside. One time created in the runtime, the object will remain available for you in every other layouts.

As global objects, they can be placed outside the layout too, and you can disable the collision.
Image
B
107
S
24
G
18
Posts: 1,361
Reputation: 22,770

Post » Thu Nov 14, 2013 3:25 pm

@TELLES0808

Does this mean the loading level (or level with the sprites in) needs to be accessed at least once for the sprites to be originally created?

If that's the case, I won't be able to just open a random level and test it. This would mean that Global Events would be easier, and I could just put up with long lists :)
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
79
S
12
G
7
Posts: 963
Reputation: 10,721

Post » Thu Nov 14, 2013 3:35 pm

You need access the layout with the objects at least one time, to create them in runtime, because who will keep your variables alive is the instance of that object in the runtime.

But don't worry, make invisible objects with 1 pixel of size, set the collision off (or not) and place them inside your start menu layout, or inside your level. One time created, they will remain active in any layout.

Place them outside, or leave them inside, it really don't affect performance...TELLES08082013-11-14 15:37:19
Image
B
107
S
24
G
18
Posts: 1,361
Reputation: 22,770

Post » Thu Nov 14, 2013 3:36 pm

Yeah, just tested this, and I can't change local variables unless the sprite is on the screen.

Also tried using an array instead of a sprite, and it works fine.

Still, if it's possible in the future, I think it would be useful to be able to group global variables for those of us that plan on having dozens and dozens.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
79
S
12
G
7
Posts: 963
Reputation: 10,721

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 41 guests