Ferrari177
Ferrari177

Reputation: 21

Use string variable to identify corresponding instance of a class

I have a simple class to define rooms. Initially I set up all the rooms I need, (Could be hundreds in a long list), though in my example I set up just 3. Then I have a string that I will use to reference the right instance of Rooms class. For instance, this could be "X10Y10". I want to use that string to identify the corresponding Rooms instance, but don't know how to associate them.

    void Start () {

        Rooms X10Y10 = new Rooms();
        X10Y10.Name = "The Great Room";
        X10Y10.RoomMonsters = 10;
        X10Y10.Ref = "001";

        Rooms X11Y10 = new Rooms();
        X11Y10.Name = "Smoking room";
        X11Y10.RoomMonsters = 2;
        X11Y10.Ref = "002";

        Rooms X12Y10 = new Rooms();
        X12Y10.Name = "Hunting Room";
        X12Y10.RoomMonsters = 7;
        X12Y10.Ref = "003";



        // Don't Know the room Ref until runtime, during game.
        // Want to get the room instance properties of one of the rooms eg.

        string RoomAtRuntime = "X11Y10";   // dont know this until game is running


        // fix following lines
        print(RoomAtRuntime.RoomMonster);   // would return 2
        print(RoomAtRuntime.Name);   //      would return Smoking room
}

public class Rooms
{
    public string Ref { get; set; }
    public string Name { get; set; }
    public int RoomMonsters { get; set; }
}

Upvotes: 0

Views: 56

Answers (1)

Serlite
Serlite

Reputation: 12258

It sounds like what you need here is a Dictionary - a collection which associates Keys with Values. In your case, you can associate each string key with a different Rooms instance, making it easy (and efficient) to quickly access any instance. Here's what your code might look like with this change:

// Declare and initialize dictionary before using it
private Dictionary<string, Rooms> roomCollection = new Dictionary<string, Rooms>();

void Start () {
    // After you instantiate each room, add it to the dictionary with the corresponding key
    Rooms X10Y10 = new Rooms();
    X10Y10.Name = "The Great Room";
    X10Y10.RoomMonsters = 10;
    X10Y10.Ref = "001";
    roomCollection.Add("X10Y10", X10Y10);

    Rooms X11Y10 = new Rooms();
    X11Y10.Name = "Smoking room";
    X11Y10.RoomMonsters = 2;
    X11Y10.Ref = "002";
    roomCollection.Add("X11Y10", X11Y10);

    Rooms X12Y10 = new Rooms();
    X12Y10.Name = "Hunting Room";
    X12Y10.RoomMonsters = 7;
    X12Y10.Ref = "003";
    roomCollection.Add("X12Y10", X12Y10);
    // The rooms should now all be stored in the dictionary as key-value pairs

    string RoomAtRuntime = "X11Y10";

    // Now we can access any room by its given string key
    print(roomCollection[RoomAtRuntime].RoomMonster);
    print(roomCollection[RoomAtRuntime].Name);
}

Note that you may need to add the directive using System.Collections.Generic to your script file.

You can (and probably should) also use something other than a string for your key value. Here, I think it'd make more sense to use a Vector2 value for these X/Y coordinates, rather than strings. (So, something like roomCollection.Add(new Vector2(10, 10), X10Y10); would be more appropriate.)

Hope this helps! Let me know if you have any questions.

Upvotes: 2

Related Questions