2016-08-23 1 views
0

私は部屋を定義するための簡単なクラスを持っています。最初は私が必要とするすべての部屋を設定しましたが、私の例では3つしか設定しませんでしたが、私はRoomsクラスの正しいインスタンスを参照するために使用する文字列を持っています。たとえば、これは「X10Y10」とすることができます。その文字列を使用して対応するRoomsインスタンスを識別したいが、それらを関連付ける方法はわからない。文字列変数を使用してクラスの対応するインスタンスを識別する

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

既存の回答を受け入れるか、実際の修正が著しく異なる場合は新しい回答を追加することができれば、[[解決済み]](ここでは「何か」ではない)をマークするのではなく、 *受け入れる答えとして*印* –

答えて

2

それはあなたがここで必要なもののように聞こえるがDictionaryです - 値を使用したキーを関連付けコレクション。あなたのケースでは、各文字列キーを別のRoomsインスタンスに関連付けることができ、どのインスタンスにも素早く簡単に(そして効率的に)アクセスできます。ここにあなたのコードは、この変化にどのように見えるかです:あなたは、スクリプトファイルにディレクティブusing System.Collections.Genericを追加する必要があり

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

注意。

キー値には文字列以外のものも使用できます(おそらくそうすべきです)。ここでは、文字列ではなく、これらのX/Y座標にVector2という値を使用する方が意味があると思います。 (だから、roomCollection.Add(new Vector2(10, 10), X10Y10);のようなものがより適切でしょう)

これは役に立ちます。ご質問がある場合はお知らせください。

+0

マジック!あなたは素晴らしいです!、私はこれに多くの時間を費やしてきました。ありがとう、ありがとう!私はそれを試して、それは動作します。 Lee – Ferrari177

+0

@ Ferrari177素晴らしい!本当にうれしく思っています。問題が解決しても問題が解決したら、その横にあるチェックマークをクリックして回答を受け入れることができます。これは、システム内で回答があったことを示します。質問のタイトルを編集する必要はありません。 – Serlite

+0

@ Ferrari177ここでそれを行う方法ですhttp://meta.stackexchange.com/a/5235 – Programmer

関連する問題