我有一個簡單的類來定義房間。最初,我設置了所有我需要的房間,(可能是數百個長列表),雖然在我的例子中我只設置了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; }
}
而不是標記'[[Resolved]]'(這裏不是「一件事物」),如果您可以接受現有的答案,或者如果實際解決方案明顯不同,並將*標記爲接受的答案 –