- Home /
Storing variables temporarily during static functions?
I've heard that using static variables is bad practice, but I'm trying to make a static 'library' where in one function I set a variable, then in the second function I use that variable as a sort of constant and then that's all it'll do, what I have in mind is something like this
//DoMathsGood.js
static var constant : int
static function setConstant(i : int)
{
constant = i;
}
static function doMaths(a : int, e : int)
{
return a*(constant+e)
}
so then in some other script I can just go DoMathsGood.setContant(4) and whatever since "DoMathsGood" won't be attached to a GameObject which is why I'm making it all static, the reason for this is I'm building a File IO Library that I want to be able to call from any script in the game, so would what I have work in theory?
I don't see why this wouldn't work. Why is using static variables bad practice? I would suggest that "constant" should be marked as private so it cannot be modified directly by other scripts.
It's bad practice when it doesn't make sense. It does in that case, so go for it !
First, thanks for editing the code, my computer has a problem with the editing button for some reason. Second since it's not being attached to a GameObject would the 'constant' variable still be initialized?
Answer by senc01a · Apr 02, 2014 at 07:50 AM
Using unprotected global and/or static variables is bad practice for various reasons, including racing conditions when using concurrently (two parts of your code want to access it at the same time) or making your code more coupled ( read more at Global Variables Are Bad.You probably want to encapsulate this in a Singleton, a commonly used pattern to deal with these situations. Singletons in Unity.
Having said that, your code will probably work, and if you do not have multiple threads, there is not a major reason why this might not work. Just keep in mind that the keyword static makes that variable unique across all of your program, so its value will always be shared no matter who calls it.
I really want to avoid using Singletons because from what I've read you have to attach it to a GameObject and I am really trying to make a library that you can just throw into the Plugins folder and start using that being said because it's not being attached to a GameObject will it still initialize the variable in the first place?
I understand. And yes indeed, if you want to use a singleton you need to have a gameobject that this is attached to because of the way Unity is designed. In that case you may want to reconsider that particular piece of functionality that you are implementing. Since your $$anonymous$$ath utility has a state (which you call constant), then this should be an actual class/prototype that the caller can instantiate and object from, given a constant. That way multiple parts of the code can use it independently providing independent constants. This would make your code more independent and modular, and your would not be doing "nasty" things.
Your answer
Follow this Question
Related Questions
Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer
how do I create a static Instance in javascript 2 Answers
Invalid IL code error when calling static function 1 Answer
Using static constants from an outside script 2 Answers
Setting Scroll View Width GUILayout 1 Answer