Global variable or instance variable

Get help using Construct 2

Post » Sat Dec 28, 2013 3:19 pm

Curious as to what is the better practice for storing variables like Experience and Level. My game has a single Hero, and right now I am storing Experience and Level on the Hero, but wondering if I should instead store it as a global variable? Just wondering what the recommendations would be and why?
B
17
S
3
Posts: 33
Reputation: 1,666

Post » Sat Dec 28, 2013 5:29 pm

If you know you will only have one sprite ever that uses variables with those names, then I guess making them global wouldn't be a problem, however...I think it's always best to use instance variables 99% of the time when dealing with sprites. One reason would be that you won't later have to convert variables from global to instance when you run into a conflict on down the road, so it could save you a lot of time.

Another reason for me is that it makes more sense to me when it reads like English. I could forget what global variable goes with what. But if something says Hero.Experience, it's easier to remember and understand.

I do use "global" variables, but they are still instance variables. For everyone using the Free Edition, each global variable takes away from your 100 allowed events. So I created an array called Global. I don't use it as an array, but simply a place to put variables I want to use as global. So, they read as Global.Level, Global.CurrentScore, Global.NumberOfBalloons, etc. It is so helpful to read the word 'Global' before the variable, that I have continued doing this even after buying Construct 2.

I only have 4 true global variables in my game, and they are called True, False, Player, and Computer. These never change their value and allow me to use all Number variables throughout the game, so I can use "boolean" variables in equations.sgtwombatstudios2013-12-28 17:32:49
B
11
S
2
G
1
Posts: 64
Reputation: 1,077

Post » Sat Dec 28, 2013 8:17 pm

In general, if your project is going to get large I'd highly recommend that you use static local variables within groups as opposed to global variables. The global variable drop-down gets very cluttered very quickly if you are overusing them.

My player's stats are stored this way, and I access them using getter functions.
B
7
S
1
Posts: 45
Reputation: 1,315


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 24 guests