Is there any difference between 1 object with all global variables vs. many objects with a few global variables each?

flyinian

Member
Pretty much what the title says.

1 object with 1000 global variables initialized on game start

VS.

100 objects with 10 global variables initialized on game start.

Any difference?
 
Any difference?
Yes. You have created 100 objects rather than 1. :p
With regards to global variables, it does not matter how you create/initialise these as they do not belong to the object that initialises them - I remember YYG explaining that in the background there is basically an internal object called "global" which holds all global variables (which is why you do global.<variable> when accessing them as this is just the same as accessing any other object).
 

flyinian

Member
Yes. You have created 100 objects rather than 1. :p
With regards to global variables, it does not matter how you create/initialise these as they do not belong to the object that initialises them - I remember YYG explaining that in the background there is basically an internal object called "global" which holds all global variables (which is why you do global.<variable> when accessing them as this is just the same as accessing any other object).
I meant more performance wise. Instead of 1 object with 1000 global variables, its 100 objects with 10 global variables that has to be processed on game start.. For me, this would be used for organization reasons.
 

kburkhart84

Firehammer Games
I meant more performance wise. Instead of 1 object with 1000 global variables, its 100 objects with 10 global variables that has to be processed on game start.. For me, this would be used for organization reasons.
Yes, it IS about performance. Every object instance you create will give a slight hit on performance. They each have all the standard data that instances have, code is run on each of them, even if you don't have events attached. And then you have the creation of the global variables. In the end, it WILL be faster to have a single object create 1000 global variables, because you are only creating the overhead of one object.

That said, the changes coming to 2.3 include putting global variables directly in scripts, so you won't even have to have an object run code creating them anymore.
 

flyinian

Member
Yes, it IS about performance. Every object instance you create will give a slight hit on performance. They each have all the standard data that instances have, code is run on each of them, even if you don't have events attached. And then you have the creation of the global variables. In the end, it WILL be faster to have a single object create 1000 global variables, because you are only creating the overhead of one object.

That said, the changes coming to 2.3 include putting global variables directly in scripts, so you won't even have to have an object run code creating them anymore.
Thanks for the clarification.
 
Top