I have a question regarding the caching of variables I hope someone with a bit more understanding could clear up for me.
Let’s say I am writing a class in C#. Within the update method of this class I need to have access to a variable, lets say a bool in this case. Now if that bool is going to have the potential, to be changed and read, each frame. Would it make any different if I declare it within my method, or as a variable of my class?
From what I understand the first option would be better, because in the second option you would be allocating space for the value each and every frame. Where as with the first option, it would have the space allocated once and then would remain allocated while that class is loaded.
In this example, when the variable is just a bool, it makes no real difference. Because the bool is a simple value object, putting it in the Update method means it will be created on the stack, which is fast and probably shouldn’t be worried about. If instead of a bool, it was a class that took a long time to create itself, and you were creating it by using “new Whatever()” within Update, then it would matter more, and keeping it as a class member would definitely be better. Using “new” generally means it’s going on the heap and that it will create garbage that needs to be garbage collected.
In general with C#, I think it’s best to always put the declaration of a variable at the smallest scope possible, so in this case, within the Update method, since it’s not used anywhere else. I’d only go against that if there was a really noticeable performance gain to be had by putting it in an outer scope… for big classes with slow initialization there is; for simple values like bools and ints, there is not.
Unless you have a reason to store “theBool” and use it elsewhere, then no need to have it. As another aside, it’s probably actually better to have theBool be only in your method if that’s the only place it gets used. This allows the compiler to perform additional optimizations because it knows the limit of that variables scope, and when Update is called, that bool just gets push onto and later popped off the stack. It’s fast and efficient and I wouldn’t worry about redeclaring it every call to Update()
Well it’s not a matter of scoping it to update if that’s the only place it’s used.
The bool could be ‘stateful’ while only used in update.
example:
public class Example : Monobehaviour
{
public float speed = 1f;
public bool flag;
void Update()
{
//toggle active flag on button press
if(Input.GetButtonDown("Action"))
this.flag = !this.flag;
bool btemp = this.flag && this.transform.position.y < 100f; //suprefluous but here for examples sake
if(btemp)
{
//while active do stuff
this.transform.position += Vector3.up * this.speed * Time.delatTime;
}
}
}
‘flag’ is only ever used in Update, but it needs to be declared outside of update because it’s stateful.
Variables aught to be class members if they describe the state of the class. In my example 'Example’s can be 'flag’d or not. This property of Example describes it. It’s part of its state.
Where as ‘btemp’ does not describe the state of Example, it’s not a property of it. It’s a calculation based on other properties of ‘Example’ (named if it’s flagged and it’s y position is below 100).
Of course btemp is superfluous, just like Dustin Horne points out about ‘theBool’, we could just stick the test inside the if statement:
Yes, but that state carries no meaning if it’s re-established every Update cycle. However, another consideration is… how often do you need to update that state? If the method that updates the bool is somewhat expensive, you could store the bool as a class level field and only updated it in FixedUpdate, while still doing the if check in Update. This makes the bool still carry meaning but reduces the frequency at which it’s update.
Yes, in the OP’s example, it isn’t stateful and carries no meaning.
I was just demonstrating an example for when one would declare a variable as a class level member.
I was just showing that the guideline previously stated that if it’s used only in a single function, you only need to declare it in that function, isn’t a clear enough guideline.