• 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 RaelTungsten · Aug 26, 2021 at 02:38 AM · procedural generationnaming

Is it possible to create a variable with a name that is a string?

Hello, I've searched the internet for days and I still haven't found an answer. The problem is that I want to make many different lists (potentially hundreds), but I want to create them each with a name that fits their categories. The reason I am doing t$$anonymous$$s is because I am developing a game with procedural generation, in w$$anonymous$$ch their will be rooms with different exits, sizes, and types.

listName = ""; if (exitUp) { listName += "N";

              }
              if (exitDown)
              {
                  listName += "S";
                  
              }
              if (exitLeft)
              {
                  listName += "E";
                  
              }
              if (exitRight)
              {
                  listName += "W";
                  
              }
              // The above is the exit denotation
 
              listName += sizeOfRoom.x;
              listName += ",";
              listName += sizeOfRoom.y;
              // The above adds the x value, a comma, and a y value of the room
              listName += roomType;
 
              // The above add the roomType text
 

For example, if I was looking at a room with exits up and down, a size of 8 by 4, and a type of StandardRoom, then listName would equal the string "NS8,4StandardRoom". If I could create a List with that name, and add all rooms that fit those parameters into it, I could then search for a List with that name when I try to spawn a specific room with those qualities.

In short, if I could somehow change the text of the string into the title of a list, that would be very beneficial.

NOTE: I have looked into unity dictionaries, and I want to avoid those if possible. The problem with them is that it becomes incredibly hard to view dictionary entries, and displaying them in the inspector is a nightmare. A list, meanw$$anonymous$$le, can be shown and modified in the inspector.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Eno-Khaon · Aug 26, 2021 at 04:17 AM

Based on another point of reference related to t$$anonymous$$s question, not only is t$$anonymous$$s challenging to do, if it's even possible at all through Reflection, it's also a pretty unproductive idea regardless.

In my personal tests, a Dictionary<> only offers actual performance improvements on a pretty large scale (1000+, if not more entries) compared to simple searches through a List<>, so you probably don't even need to worry about potential performance implications if you keep the checks simple enough.

With simple checks in mind, you could consider creating a class that tracks a List<> of Rooms, coupled with a string for a name. For changes made through script, you can then check all your groups and find one with a matc$$anonymous$$ng name:

 // Minimal example, including base class tracking groups of rooms
 // as well as the groups themselves
 public class MainClass
 {
     public List<RoomGroup> groups;
     
     public void AddGroup(string groupName, Room roomToAdd)
     {
         for(int i = 0; i < groups.Count; i++)
         {
             if(groups[i].name == groupName)
             {
                 groups[i].rooms.Add(roomToAdd);
                 return;
             }
         }
         // If you make it here, the group didn't exist
         groups.Add(new RoomGroup(groupName, roomToAdd));
     }
 }
 
 public class RoomGroup
 {
     public string name;
     public List<Room> rooms;
     
     public RoomGroup(string name, Room initialRoom)
     {
         t$$anonymous$$s.name = name;
         rooms = new List<Room>();
         rooms.Add(initialRoom);
     }
 }
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 RaelTungsten · Aug 26, 2021 at 03:08 PM 0
Share

Thanks, this is exactly what I was looking for. I was using a big list of dictionary entries, but this should work far better.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

127 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 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

Naming array elements 1 Answer

When using ECS, what does "dstManager" stand for? 1 Answer

Reading data from xml. 1 Answer

2d Solar System procedural generation 1 Answer

Adding rivers to procedural generated 2D map 1 Answer


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