- Home /
Will making all variable static reduces the game performance?
Hi, i'm a beginner ,so..... simple answers :D
First of all, i came up with an idea which is to make a whole lot of variables to be static for maybe health purposes or attack counts, mana, etc... so that i can use these variables in other scripts like AI
For example, when the player health is low, i want something like wolf or zombie to search for the player since the player is obviously wounded and blood is spilling out. Thus, i can use the static variable i have declared.
Maybe 1 variable is not much of a impact but what if i declared a whole bunch of them and use them a lot?
Thanks in advance.
Little speed stuff isn't even worth worrying about. It's like stopping to pick up a penny when you're late for a job interview.
Think of it as a "which can I program better" decision. If static player vars make more sense to you, you'll write better, faster code with fewer errors.
But, doing it one way for the monsters, and another for the player might be confusing. And the monsters can't have statics for their stats. Using all member vars might help you write better, faster code.
It's like stopping to pick up a penny when you're late for a job interview. You made my day!
this forum must ave like button hahahaha stopping to pick up a penny when you're late for a job interview hahaha
Answer by Bilelmnasser · Jun 07, 2014 at 11:24 AM
in C# : Method of class (function): A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction, which also checks for a null object references. However, most of the time the performance difference (static or not) between the two is not significant. Members of class (variables): in term of RAM memory i think static members is better for memory purpose (once the static data member has been defined, it exists even if no objects of its class have been created. Static data members are initialized and destroyed exactly like non-local objects) and check this test between static method and non static method call speed at this page:
Answer by Jeff-Kesselman · Jun 08, 2014 at 06:42 PM
So, people have answered your question. But let me give you a suggestion.
DON'T
static variables are global variables. Global variables are a leading cause of bugs in computer programs. The more there are, the more likely you are to have a conflict somewhere.
You are going to make yourself a lot of heartache if this program is of any kind of size and you go that route...