- Home /
i have a String with the VALUE "abc" and want to set a bool with the NAME "abc" to true based on the string value
Hey guys!
I wont paste in my code cause its in to many different classes so i will try to simplify my problem and give an example like this:
So i have a Class "Enemy" This class has a string type. From this i have made a few Enemys like "Wizard : Enemy" with type "wizard", "Goblin : Enemy" with type "goblin", "Random Dude : Enemy" with type "random dude" and many more. Each of those can exist only once and will not spawn again from my random enemy spawner until this one copy exists. The Spawner has some booleans wizard, goblin, randomDude and many more. The spawner spawns the object from a list of Enemys. This one changes every time.
So now i need a function to take the type and the equivalant bool and to check if it already is spawned.
I tried to do this with a Reflection but i have heard that reflections are slow so i am looking for a better solution.
Here is the Method that i wanted to use to "convert" the string name into a bool. I dont really want a Switch cause there will be many enemys.
public bool isSingleCopySpawned(string type)
{
string typeActive = type + "Active";
print(typeActive);
bool boolActive = (bool)this.GetType().GetField(typeActive).GetValue(this);
print(boolActive);
return boolActive;
}
Answer by Roger_0123 · Dec 30, 2020 at 03:26 PM
Hi! You should use a Dictionary (aka Map) Look here Your dictionary will be of type <String, bool>
. You can use the methods Add(String key, bool value), and ContainsKey(String key)
Okay, thank you. Thjis looks promising. I was looking at dictionarys for a while now but im not really sure how i should use them in my usecase.
So i would make a dictionary with string type and bool if its spawned or not. and change it directly inside the dictionary. so i would always check the dictionary if i need to know if its spawned? But then when i spawn it how to replace the value inside the dictionairy? Im new to dictionaries and the web link you gave me is kind of confusing to me.
Answer by sacredgeometry · Dec 30, 2020 at 08:42 PM
Im not sure a dictionary is a good solution but I am also not sure what the problem is.
You have a spawner and it spawns objects from a list of enemies, the spawned enemy changes every time.
So why not just create the enemies once and just recycle the instances using object pooling? That way there will only ever be as many as you have ever created. You wont hit any garbage collection performance spikes in deallocating them All you need to do is pick a random enemy from the pool and put it into the game when you want.
If you do that and access them by using a string then I guess you could put them in a dictionary but it wouldnt be a string bool dict it would probably be better as a string GameObject one.
i.e.
Dictionary<string, GameObject> Enemies = new Dictionary<string, GameObject>
{
{ "Wizard", Wizard },
...
}
Okay, thats a good idea. But i have also a lot of enemies that can spawn multiple times. So i have some "singleCopy"-Enemies and some"multipleCopy"-Enemies. Thats why your system would be difficult for my game. Or maybe i oversee something.
Well thats a different problem to what you said but you could still do it the way I said by having a Dictionary with a similar type signature to this Dictionary<string, List<GameObject>>
I wouldn't use this design for that though I would probably use something a little more sensible. But then I probably wouldnt try to address types with magic strings either.
Let me write you something an example of how I would probably do it.
Sorry I got distracted.
Here is my pool implementation:
https://github.com/eviathan/SpawnPool/
It should give you some ideas on possible ways to implement this. $$anonymous$$y implementation is general so you could just take it and use and it should be fit for purpose.