#variablestore
1 messages · Page 1 of 1 (latest)
If I have a left hand gameobject variable
And I need to do something with it from 2 different scripts, I can just access it from the variable store, mainly if I have multiple different scripts needing to access it I can just access the var from the variable store
Instead of referring to the first script that uses the left hand, or having multiple left hand variables
@woven lagoon
Sounds like you just want the singleton pattern tbh
what
Or just a Dictionary<string, GameObject> which is kinda clunky but
I dont want to only store gameobjects in it though
You can make a Dictionary<string, object> but your code is going to be extremely ugly
thats what Ive done
But to add to the dictionary it wants a string for the key and I want the key to be the same as the variable
So many casts and no ability to refactor
what
You're basically throwing away all compile time safety and any ability to understand what your code is doing.
can you stop just saying "what"
idk wtf that means
If you already have the variable then what do you need the Dictionary for
really?
the key is used to lookup the value
Elaborate
to store it
wdym "store" it
all objects are stored in memory
as long as they exist
you don't need to "store" them elsewhere
like have all the variables in one place
one completely unmanageable place
wdym
Just get the variable with the variables name
and add the variable the same way
what if there's two objects with the same name
then what are you doing
having two objects?
You don't even know what type is in the store, so every time you have to get it you need to perform a cast. If you ever refactor that code you have to find all the places where it's used by name and change the types you've casted, it's a nightmare
ill probably add something to take care of that
true
i figured the value would tell it what kind of variable it is
the dictionary value
What's better?
void Awake() {
GlobalObjectStore["LeftHand"] = this;
}
// elsewhere
GameObject leftHand = (Hand)GlobalObjectStore["LeftHand"];```
Or just:
```cs
public static Hand LeftHand;
void Awake() {
LeftHand = this;
}
// elsewhere
Hand leftHand = Hands.LeftHand;```
notice... we have type safety and compile safety in the bottom one
practically, if you ever find yourself using object outside of serialization, you've likely done something wrong.
i dont understand how that compromises safety and compile safety
Also, wouldn't that be:
(Hand)GlobalObjectStore.Store["LeftHand"];
yes
in the first example if i spell "LeftHand" wrong in either place, I won't have any idea until the game is running and not working
and i'll have to investigate what happened
Because if the type is wrong you get a compiler error. If the "left hand" has changed to be renamed or whatever, you get a compiler error
I dont spell things wrong
in the second example if I spell it wrong anywhere, the compiler will tell you instantly
I dont
Well if you never make mistakes, then sure
I've never met such a superhero before though
I do make mistakes, just not spelling mistakes
If you:
- use the wrong type in the cast
- use the wrong name
- miscapitalize
the compiler won't help you.
If you want to click on the variable name and see where it's defined, you can't do it with your approach at all. With the second approach, you can, and you get the handy "n references" in VS, etc...
there's just... so much more to help you along when you don't actively try to fight against the type system
I just don't see any benefit you gain from doing it
Only ever having to reference one script
If anyone else saw code like that they would be overcome with violence and a need to destroy
lmao
how is that a benefit? That tightly couples everything in your game to one thing
we have different definitions of those things I guess
one big unorganized pile
And wait a century for everything to be sorted?
I don't even actually know what you mean by sorting this thing anyway lol
For no reason. Dictionaries are unsorted
no im saying it wouldnt matter that its unorganized, just use the find thing
Imagine if a hospital stored medical records, employment records, utility bills, contractor contracts etc all in the same file. Instead of saying patients["John"] you now try to do everything["John"]
but how do you know if you're going to get back a patient record
or an employee record
or a guy who installed the light bulbs?
by not having two variables named the same thing
SO now I do everything["Patient John"]
how is that any better than patients["John"]
except that now I lose type safety
so its neat
and you only have to reference everything instead of multiple different things
This isn't the way man
but
try it
see how it works for you
maybe you'll love it
Dictionary<string, object>
you're also allocating constantly
every time you set or access something you have to allocate a string
and that is a lot of garbage
Not to mention yes a dictionary lookup is fast but still slower than direct references
it's also proportional to the length of the string key...
also who is responsible for putting objects into the giant dictionary?
And maybe more importantly
who is responsible for removing them?
How do you not end up with 1000 orphaned references to destroyed objects
Anyway, if you really do want to do this absolute travesty of an idea, my answer to how you would handle names would be something like this:
public void Set<T>(T obj, bool force = false) where T : UnityEngine.Object => Set(obj.name, obj, force);
public void Set(string key, object obj, bool force = false)
{
if (!force)
{
if (_store.TryGetValue(key, out var other))
throw new ArgumentException($"Key \"{key}\" already exists in the store, with the value \"{other}\".");
_store.Add(key, obj);
}
else
{
_store[key] = obj;
}
}```
Where UnityEngine.Object types names are automated, but for any other type you must specify a key. You could do the same thing for `Get`.
Again, this is awful for so many reasons though, probably even more than we've already mentioned