- Home /
How to optimize code with one time use booleans
Hi, so I've written a class which may or may not need to have certain components re initialized or updated in the various built-in unity MonoBehaviour functions. On an object by object basis, I may want the components updated every update, only once, or just every time the object is enabled (Start, Update, and OnEnable).
I will NEVER have need to reassign what functions go where once the program starts, but it would be nice to be able to set which function my methods would get called from in the inspector. (So if there's some mystic magical way to manipulate preprocessor directives from the inspector on an object by object basis that I don't know about, please let me know!)
If I want to shuffle around where the different functions are being called, is there any way to do it in a SINGLE CLASS without a long if-else chain or switch statement existing inside Start, Update, and OnEnable?
If there's not, I'll just write a variety of wrapper classes that handle the updating in different combinations of Start, Update, and OnEnable, but I wanted to see if someone could show me a different (and cleaner) way.
I'm not entirely sure what you're asking or trying to do. Can you give me an example? $$anonymous$$aybe a little bit of pseudo code?
What I'm getting at is that I have a set of booleans and enumerations that deter$$anonymous$$e how the object should act, but all I'm doing is setting them in the inspector: once the game starts I don't need to be checking the values every update, nor do I want to.
Let's say I have an enumeration that is supposed to specify whether I should execute a function in Start, Update, or OnEnable, but only one of those. How can I accomplish this without doing this:
void Start()
{
if(updatePolicy == START_ONLY)
{
CustomFunction();
}
}
void Update()
{
if(updatePolicy == UDATE_ONLY)
{
CustomFunction();
}
}
void OnEnable()
{
if(updatePolicy == ENABLE_ONLY)
{
CustomFunction();
}
}
void CustomFunction()
{
// . . .
}
I don't even need to define Update or OnEnable if the updatePolicy is START_ONLY. The only way I can conceive of splitting these up is to write different classes wrapper that only implement one of the functions, and letting the user select which wrapper class to apply to their object in the inspector. I'm hoping I can find some alternate solution here.
I'm looking into something related to reflection at the moment. I'll let you know if it would be possible to use it for your case.
Answer by ThePunisher · Nov 15, 2013 at 11:21 PM
So after swimming through members in all the base classes of Monobehavior (through reflection). I was unable to find anything that could potentially store the definable methods like Awake, Start, OnEnable, OnDestroy, etc. This could mean that they use their own built-in SendMessage functionality to call your Awake, Start, Update, and so on, through reflection. In other words, you can't change which of those methods exist during run time (based on my knowledge).
So to answer your question, I don't think you can do what you're wanting in a clever way, although I am not 100% sure. You're best bet will probably be creating the different types of classes that have only the required methods defined.
Alright, well thanks for trying. Hopefully someone else can provide some clever solution we haven't thought of, but I guess I'll just go with different wrapper classes for now.
You could tell me your overall goal for this kind of framework and perhaps I can provide some suggestions or alternatives altogether.
The overall goal was just efficiency / not needlessly checking booleans when I don't need to. For all the other situations I'm already using a series of delegates that are hooked up in the start function based on what the user specifies in the inspector. $$anonymous$$y problem only arose because this was more about frequency of update vs. what the updates do. I thought about using coroutines to specify intervals of updates, but that still doesn't really handle the OnEnable case. At this point, the best solution still seems to be my original plan: just make a couple separate wrapper classes.
If I don't get an alternative within the next couple days, I'll mark your answer as correct and be done with it.
Sorry I wasn't clear, I meant the purpose of the framework. What does it do? Or what is it used for?