- Home /
Static vs Non-static functions and variables
I am quite new to this programming scene and try to figure out from examples but some things bother me about static and non-static things that I can't find answer online. 1. Should I reference object in script and call public functions or just set them static and call them static way (wich is faster) 2. Why cant I comminacte normal functions and static functions and when to use static functions and variables
I tried to search thoese answers on google and either i dont understand them or can't fight good explanations. Any kind of help would be good.
Thanks.
Answer by Enum · Jun 04, 2015 at 09:10 PM
I think you should read a guide about object oriented programming in general. To understand all those concepts of classes/instances, static/non-static, inheritence and polymorphism is really worth the effort. But I will try to give you an example (that is not related to game programming on purpose):
Let's consider a class representing an emperor penguin: (Be aware that I might violate naming conventions, as C# is a new language for me, too)
class EmperorPenguin {
static string conservationStatus = "Near Threatened";
float weight = 15;
static bool doesItLike(Food f) {
return f.isFish();
}
void eat(Food f) {
if(EmperorPenguin.doesItLike(f)) {
weight += f.getWeight();
}
}
}
The variable conservationStatus is static, because it is a property of the species. Not one emperor penguin is Near Threatened but the "community" of emperor penguins. Weight is an instance variable because it is per individual: Two penguins might not share the same weight.
For the functions it's the same: All penguins eat the same food (without respect to personal preferrences like in human beings), so the method that checks whether a penguin likes a particular piece of food can be static. Eat() should increase the weight of the specific penguin eating the food, so this method must not be static.
You can see that the non-static method can call the static one, as it can be accessed from everywhere by the class-name. But the static function may not call instance methods or variables, as it belongs to all penguins, not to a single individual.
To call those non-static methods, you need to create an instance: Taking the "construction plan" (the class) and create a concrete object from it:
EmperorPenguin p = new EmperorPenguin();
Food f = new Food("fish", 0.5f);
p.eat(f);
print(p.weight);
//You can create a second penguin with different properties:
EmperorPenguin p2 = new EmperorPenguin();
Food f = new Food("fish", 0.75f);
p2.eat(f);
print(p2.weight);
//Possible without any instance:
print(EmperorPenguin.conservationStatus);
When it comes to method, you could actually use one or the other. But it is conventional to use instance when the object is modified, else use static. Like in Vector3 class, Distance use the parameter but does not affect them so static, Normalize modifies the instance so non-static.
So far I used quite alot of public static variables mostly for easier communication between scripts (for instance if its game wide variable or cant reference script) but for functions (public static) i find it easier to use public static bool to call functions like that.
maybe iam doing quite alot of things wrong here but my games are not much complex for now so i cant really see fps drops but i try to optimize best as i can
Static or not has no big impact on performance. It's a matter of program$$anonymous$$g reusable or extendability. Program$$anonymous$$g is not all about performance. Calling one variable every frame is not worth thinking about performance. $$anonymous$$aybe think about performance where (nested) loops are.
I renew my advice to read about (good) program$$anonymous$$g in general, as you will learn why static (and making everything public) is often not really appropriate. BUT in simple programs it is not worth bothering, like it is not worth bothering about performance when calling/creating a single variable.
I never really learned C#.. I learned Java wich is quite similar. $$anonymous$$aybe someone else has a good hint?
IFs that you write by hand, that are not in loops will not really affect performance... Simple IFs or additions will always take only a few nanoseconds or less (quite complicated what happens in a processor, but that are maximum numbers). When you ask a List with 100 entries, if it contains a specific object, this will need up to 100 IFs and 100 calls to an "equals" method. This might take, lets say, 600 nanoseconds. Such points, where you operate on large datastructures, are those points where you can slowly(!) start thinking about performance. Otherwise don't bother your $$anonymous$$d with performance. Start with it, when you really have FPS problems.
A long method in general is considered not well programmed (you will learn such things by time and will experience why it's bad). If you have a method, that you can read like text, because it calls many methods with speaking names and uses variables with speaking names, then it is nice. (Of course you will at some point always need to write the IFs in the called methods.)
For more read in tutorials, here in unity answers and maybe another general program$$anonymous$$g forum. $$anonymous$$aybe even try to answer simple questions.