Unity "static" variable?
I want to write a static function that will take a variable name I pass it, and check if that variable exists (the type is consistent), and if not, create it, and if so, will use that value.
So like DoSomething(string variableName, int variableSize)
And if I say DoSomething(apples, 2)
It'll look and see if int apples exists, and if not instantiate a int apples = 2.
And if this function runs again (in the same session) with the same passed name of "apples", it'll check again, and if int apples exist (it should) it'll just use that variable.
Is this where static comes in? Not sure how this works. Thanks in advance!
So like DoSomething(string variableName, int variableSize)
And if I say DoSomething(apples, 2)
It'll look and see if int apples exists, and if not instantiate a int apples = 2.
And if this function runs again (in the same session) with the same passed name of "apples", it'll check again, and if int apples exist (it should) it'll just use that variable.
Is this where static comes in? Not sure how this works. Thanks in advance!
Comments
I'm not 100% sure of what you want to use it for, but from the description I think what you are looking for is a dictionary data structure. It can be static or non static depending on how you want to use it, though I would recommend you try to stay away from statics.
http://csharp.net-informations.com/collection/dictionary.htm
Unity's Player prefs works in a very similar way. You might even want to use that if you want the values to persist between sessions.
https://answers.unity.com/questions/1325056/how-to-use-playerprefs-2.html
So I run this function, pass it "apple". It'll check if the int variable with the name "apple" exists, if it does, it uses it. If it doesn't, it creates it. That's it really.
How do I pass a string into the function, and query if that name exists as a variable name?
https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx
In the documentation the "key" part is whatever you want to call your variables. "apples" in your example. The value would be the value that is associated with the key.
Maybe you should explain a little more about what it is you want to do though. Rereading your initial post you mention that you want it to look to see if "int apples" exist, which is checking the name and type. This is not something directly/easily done with dictionaries.
So what I'm trying to do is to create a function that I can call once, checks if an variable of int type by that name exists. If yes, use that variable. If not, create that variable, then use it.
More detailed description of my function:
void ControlledRandom (string varName, int varRange)
with the varName it stores a list of varRange number of elements, and runs a Random.Range call against it, and removes an element from the list whenever I call it. And this ensures a controlled random which will eventually calls every element in the range of random instead of maybe getting ten zeros in a row.
Object Pooling - Unity
Essentially you want to do something like that, but instead of an object you contain a Dictionary of <string, int> or whatever and when you access something you can either create it or remove it.
Is that correct?
@Rigormortis yes - I want to do all that from one function without having to setup the whole thing first - I already have that functionality, I can already do that, but I want to do that without having to setup a variable first manually.
But ok, I understand what you are aiming to do now. A dictionary will probably be your best bet. If you want to access it from anywhere in the project you are going to have to do some boiler plate code do provide all the functionality you want though.
As an example, you will probably need 2 methods for getting and adding values to the dictionary.
Your dictionary declaration will then look something like this:
There are quite a few ways that you could go about this though, so you'll have to do some reading/experimenting to see what works best. Again though, I would recommend you stay away from using statics. They have their place, but often times create unnecessary problems.
And then from anywhere (any other class you have) you can call ValueHelper.DoSomething ("Apple", 2);
Or, @Tuism, if you have a finite list of items that you want to track (i.e. apple, peach, etc.), then I highly recommend using an enum for the key. String (as in using "apple") are prone to error (typo's and such).
If I instantiate 3 of this class, this is the output:
As a side note to everyone, not sure if this is true in Unity, but in C# you dont have to call
You can simply just do:
This will create the entry, or overwrite it if it exists.
You can add properties or methods to it at run-time.
This is part of C# 4.0 and up.