Which is better?

Just started using Construct 2? Post your questions here

Post » Mon Jul 07, 2014 3:41 am

Which is better? Global variables, local variables, or instance variables? Currently most of the advanced stuff I do with movement and shooting has one or two global variables. Is it better to do that, set up instance variables, or local variables? Based on everything I have read I don't see any of those options making an overall impact on the speed or efficiency of my game. I guess at that point it comes down to ease of use? What are you using or what or your thoughts on the matter?
Founder of HeroicTek
HeroicTek.com
Owner of HeroicDomains
HeroicDomains.com
B
21
S
7
G
1
Posts: 157
Reputation: 2,020

Post » Mon Jul 07, 2014 3:47 am

It's not a matter of which is better. It's a matter of what is appropriate to use in a given context.
I strongly advise you read the manual on Global variables, local variables, and instance variables.
Image
B
75
S
23
G
3
Posts: 673
Reputation: 9,283

Post » Mon Jul 07, 2014 3:58 am

I have. Doesn't really do much for me other than rule out local variables for most things I am doing. I Guess, at least in the context of my game, it doesn't really matter if I do it globally or as an instance variable. Given you didn't hint at there being any real performance benefit between them, I will just keep things the way they are since it currently works and that is what matters in this stage of the game. Thank you.
Founder of HeroicTek
HeroicTek.com
Owner of HeroicDomains
HeroicDomains.com
B
21
S
7
G
1
Posts: 157
Reputation: 2,020

Post » Mon Jul 07, 2014 4:40 am

I'm not commenting on performance, but there there is the obvious difference with instance variables vs global/local, which is that each object instance has its own copy of an instance variable. So if you need a value that can be different for each instance, then an instance variable is your first choice. I'm not ruling out other possibilities for tracking per-instance data, such as an array that might store instance UIDs along with the associated data.

For the other decision, there is a rule of thumb called locality of reference, which says that you should declare something close to where you use it. This would mean using local variables where you need a value only in a particular 'scope', e.g. within a single event and/or sub-events. This is a kind of 'data hiding', which is generally a useful thing and helps to modularise your code. For everything else, there are global variables.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Mon Jul 07, 2014 4:45 am

Excellent information @codah
Founder of HeroicTek
HeroicTek.com
Owner of HeroicDomains
HeroicDomains.com
B
21
S
7
G
1
Posts: 157
Reputation: 2,020


Post » Mon Jul 07, 2014 4:07 pm

reading up is always good and the best source of info, but a quick summary is as follows


local variables: within a single event structure, I often use them for intermediate calculations, etc. they can only be changed within that event and using them means that you don't affect things outside of your event. so it keeps things constrained

global variables: very useful for information that passes between events (on the same sheet as well as between sheets and layouts). they are also useful for long-term storage of information... eg scores, progress. global variables could be used for all cases of local variables but in my humble opinion it is bad practice as you should keep the number of globals to a minimum and use them only when you need them

instance variables: these are variables that are connected to an object and are very useful for picking out a specific object from a group, or for having one object do multiple things depending on the value of an instance variable.. or could be used as the health indicator for a baddie (that was in one of the tutorials). so instance variables do not replace globals or locals, they are different things.

for those not comfortable with instance variables, you could hack your way through global variables but then you are really missing a powerful tool.

R
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

Post » Mon Jul 07, 2014 4:13 pm

Thanks @Lordshiva1948 and @rho, those are great pieces of information. Gives me more to think about.
Founder of HeroicTek
HeroicTek.com
Owner of HeroicDomains
HeroicDomains.com
B
21
S
7
G
1
Posts: 157
Reputation: 2,020

Post » Mon Jul 07, 2014 5:36 pm

i like global varibles because of events passings
B
7
S
1
Posts: 57
Reputation: 484

Post » Tue Jul 08, 2014 6:15 am

beardedeagle you welcome
B
250
S
39
G
15
Posts: 1,953
Reputation: 40,623

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: FartsLolz and 5 guests