How many Global Variables is too much?

Discussion and feedback on Construct 2

Post » Sat Aug 02, 2014 12:58 am

I have about 100 global variables (numbers), there's no performance impact. I use GV for saving data as well as for AI triggers or combat.
B
70
S
24
G
19
Posts: 1,757
Reputation: 17,614

Post » Sat Aug 02, 2014 1:08 am

A0Nasser wrote:@SEGamestudio
If you will put a tree in every layout, This will be 100 original tree and the image memory will increase.
If you have 10 trees in one layout, This will consider one tree in the image memory.

I don't think it will be a good idea to have 100 layout.


Well I'm not sure why, but when I duplicated a Layout 35 times, my image memory did not increase.
B
44
S
13
G
7
Posts: 32
Reputation: 5,628

Post » Sat Aug 02, 2014 1:40 am

On the PC, assets are loaded per layout not the entire game.

C2 has very good native memory management.
B
70
S
24
G
19
Posts: 1,757
Reputation: 17,614

Post » Sat Aug 02, 2014 3:22 am

@A0Nasser most export options support memory management so they load only whats on that layout so its very possible to have 100+layouts
B
42
S
17
G
2
Posts: 850
Reputation: 6,209

Post » Sat Aug 02, 2014 10:02 am

The traditional programmers tell you global variables are bad. They do make the code a bit more difficult to handle in some respects but in terms of performance they will have no effect at all.
B
47
S
16
G
9
Posts: 1,097
Reputation: 11,180

Post » Sat Aug 02, 2014 10:25 am

In C2 you don't need to think about such aspects, as @Ashley is the traditional programmer who takes care of all this (I hope he won't argue against it^^). In C2 gloval variables exist to keep data between layouts. So use them as you see fit. If you can manage a thousand globals - then use a thousand.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Sat Aug 02, 2014 7:58 pm

Contrary to (weirdly widespread) belief, number of layouts has NO effect on performance. Number of variables shouldn't either (unless you're up in the millions) but for the case you're talking about, you need to use a data structure such as webstorage or an array, just for convenience purposes.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,679

Post » Sun Aug 03, 2014 9:51 am

Global variables have virtually no performance impact. The only limit on how many you can use is how far you can go before it becomes unmanageable. If you are using loads of variables you might want to switch to an array or dictionary instead, simply because they are more manageable.
Scirra Founder
B
387
S
230
G
87
Posts: 24,249
Reputation: 192,240

Post » Sun Aug 03, 2014 11:58 am

number of layout or number of global variable have no impact on performance
BUT its safe to use lowest number of it to keep your organisation
B
45
S
16
G
8
Posts: 792
Reputation: 8,306

Post » Mon Aug 04, 2014 4:07 am

Depends on what your aiming for.

JS v8 and all of the JIT have gotten pretty good. You could say unlimited. However there is a far better answer and a different way to look at the "how many globals is too many".

The answer is.
"Any more than what disrupts development, readibility and good development practices."

After a certain point you will need to keep better track of these globals where you don't need to. Sounds easy. is easy. but it's still over all lost time.

My personal global count max is 5.
I keep variables in Groups to object them. Groups always define an ES.
I also keep Dictionaries which store globals as an instance variable as Dictionaries are Global by nature.
When I use a true global it's development value. Such as
DebugLog string global. One where I will pop out a value to debug screen without the console window.

as for storing player info such as best time.
Dictionary.Add("BestTime_" & layoutname, timevariable)

and there you go. 1 global dictionary. stores thousands of levels(no need for global vars). And best of all you can also do. WebStorage.Add( "BestTimeSave", Dictionay.asJson ). To save all the data for later. Then use
Dictionary.FromJson( WebStoage.Get( "BestTimeSave"). boom smiple. only a few peice of code lines. and now you can support thousands of levels in only a few lines on a single ES.
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,013

PreviousNext

Return to Construct 2 General

Who is online

Users browsing this forum: Brendan2007 and 7 guests