Here's the problem I'm having at the moment. I'm defining a class called "room." I want it to contain all the fundamental properties of a room in this text game, such as its description, exits, and so on. The description was easy. The exits are harder.
I want the variable I'm using for the exits to be able to directly relate to their destination. My first idea was to make each room have a string variable called, "N_Exit" (for the North exit of the room) for instance, which I could later assign to be "Room_2".
Let me say it in code, even though it didn't work. I'll cut out the extraneous stuff.
class room
{
Public:
string N_Exit, S_Exit, E_Exit, W_Exit;
} room001, room002, room003;
later on, I define the exits of those particular rooms.room001.N_Exit = "room002"; room002.S_Exit = "room001";See where I'm going here? I'm trying to make it so that when the user inputs that he wants to move north, the game will look at the N_Exit designation for the room he's currently in, and switch the current room designation to the value of N_Exit.
To do this I created another object of class room, and designated it to contain the data of room001 (the starting room).
room Current_Room; Current_Room = room001;It seemed to work, at least, when I input the command to show the room description of Current_Room, it outputted the description of room001. But maybe I should be using a pointer here? I don't quite understand when I should use pointers yet. Should Current_Room just be a pointer?
But I'm having a devil of a time getting the Current_Room to change. This is the sequence I tried when the user inputs the command to travel North from room001 (into room002):
if (usercommand = "n") and (Current_Room.N_Exit != "0")
{
Current_Room = Current_Room.N_Exit;
}
(I'm assigning "0" as the value for the exit if you can't go that way)This seemed a little iffy to me, and indeed, the compiler rejected it. The deal is, the text string for Current_Room.N_Exit is valid text for an existing object of class room, so I thought maybe it would work.
Ultimately, I guess my question boils down to how do I use the contents of a string to designate a class?
The compiler doesn't like it when I mix types. It says Current_Room is a class, and Current_Room.N_Exit is a string, and can't make one of them equal another.
I know this has GOT to be really basic, and all I need is the right syntax, so that's why it's a n00b question. But if I can't figure out the basic stuff, I'm not going to be able to do complex stuff later on. :)


Sign In
Create Account


Back to top









