Sign in to follow this  
dillscon

using the same class reference more than once

Recommended Posts

dillscon    142
I just created a method that loads all the room content into memory at game startup. Again this is a text based game where you explore through a house, so no graphics at all. ------------- Language: C# OBJECTIVE: Use the same class reference over and over again instead of creating 4 refrences to be prepared for the max amount of doors ------------- i have something like this..
public class room
{
   public string name;
   public int id;
   public linkedlist<door> doorList = new linkedlist<door>();
}

public class door
{
   public int destRoomID;
   public int curRoomID;
   public int wall;
   public bool locked = false;
}


/* Now I'm wondering the best way to do this.  Every room may load anywhere between 1 to 4 doors.  I've created a function in the room class that can add the door to the doorList.  Heres an example of what I want to do, just tell me if its a bad idea, and if theres a better way to do it.
*/

// Add Room Content

public room GetRoomContent(int id)
{
   room r1 = new room();
   door d1 = new door();
   switch(id)
   {
      case 1:   // Room ID #1
         d1.locked = true;
         d1.wall = 1;
         d1.curRoomID = 1;
         d1.destRoomID = 2;
         r1.AddDoor(d1);

         // now create another door
         d1 = null;       // Not sure if this is good or bad....                             
                          // just want to clear the door info
         d1.locked = false;
         d1.wall = 2;
         d1.curRoomID = 1;
         d1.destRoomID = 3;
         d1.AddDoor(d1);

         break;

         default: 
            break;
   }
}


I typed all this in here, so if you see any typoes or anything like that this isnt my actual code. I shortened it up a bit on here just to show the gist of what I'm trying to do.

Share this post


Link to post
Share on other sites
SilverCoder    325
hi,

e.g. create a static RoomManager class which stores the room instances.


public static RoomManager
{

static Dictionary<int, room> m_Rooms = new Dictionary<int, room>();

public static int Addroom( room roomInstance )
{
static roomId = 0;
m_rooms[++roomId] = roomInstance;
}

public static room GetRoom( int roomId )
{
return m_Rooms[roomId];
}

}


then somewhere in your code write


room = RoomManager.GetRoom( 1 );

Share this post


Link to post
Share on other sites
dillscon    142
Well that certainly is a better way of doing a lot of things. I didnt know anything about Dictionary<> before that post. I'm glad you posted that.

My GetRoomContent(int id) method is only used at startup. I've hard coded all my content right now because i've been too lazy to make a file system to do it. Its on my todo list though.

I'm wondering if how im adding my doors is the right/best way to do it.

I think before I finish I'm going to have to use the Dictionary<> though. Right now I loop through a LinkedList<> to get my room info after all the content has been added.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this