Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oskar-s · Dec 30, 2020 at 02:02 PM · booleanreflectionreflectionsclass object

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;
     }

  







Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oskar-s · Dec 30, 2020 at 03:38 PM 0
Share

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.

avatar image
1

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 },
     ...
 }


Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oskar-s · Dec 30, 2020 at 09:07 PM 0
Share

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.

avatar image sacredgeometry oskar-s · Dec 30, 2020 at 10:10 PM 0
Share

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.

avatar image sacredgeometry oskar-s · Dec 30, 2020 at 10:17 PM 0
Share

Let me write you something an example of how I would probably do it.

avatar image sacredgeometry oskar-s · Dec 30, 2020 at 11:47 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weird lighting box caused by reflection probe 0 Answers

Self-Illum Fresnel Reflective Bumped Specular issue 0 Answers

Reflection probes only reflecting 1 side 3 Answers

Assembly.Load on iOS 1 Answer

How to Access the "play button" of a AnimatiorWindow from script editor 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges