Page 1 of 1

too many variables?

PostPosted: Sun Mar 11, 2012 4:09 pm
by Fojam
how many variables is too many? if you have a lot of variables, will it slow down your game? and do actor variables take up more than regular variables?
I ask because i have a lot of variables in my game, and a large amount of them are actor variables. I am wondering if it would be better to try to thin down the amount of variables by combining variables such as moveLeft and moveRight into just move, where when it is -1 it is left and when it is 1 it is right

Re: too many variables?

PostPosted: Sun Mar 11, 2012 4:34 pm
by phyzix5761
As you suggested, compressing two or more variables into one will save you allocated memory. But what you have to ask yourself is, "Are you already experiencing lag?" Your computer can process a lot. If you are having lag issues first check your collision methods, then your actors (are they out of view and doing nothing?) then finally come back to the variables.

Something that may also be worth seeing is your coding. Check to see if there are redundant statements of if any parts can be consolidated.

Re: too many variables?

PostPosted: Sun Mar 11, 2012 8:19 pm
by skydereign
You won't experience lag if you have too many variables. But, that doesn't mean you should make redundant variables. I can't think of any reason that you should have moveRight and moveLeft, if when one is equal to 1 the other is always equal to 0. Even then you can compress that into a single variable. This kind of optimizing makes your code a lot more manageable. And having less variables doesn't save you allocated space, unless you are using pointers and using dynamic memory allocation.

Re: too many variables?

PostPosted: Sun Mar 11, 2012 8:43 pm
by phyzix5761
From what I've read Static, Auto, and Dynamic Variables are given allocated memory; Pointers as well.

Re: too many variables?

PostPosted: Sun Mar 11, 2012 8:50 pm
by skydereign
I'm talking about the stack versus the heap. The heap is where the user allocates memory via malloc/calloc/realloc. While technically you can say the local variables and the like are allocated out in the stack, usually the term allocated is reserved for the dynamic memory allocation functions (so anything involving the heap).