Sign in to follow this  
ConstantlyLost

Confused on Connecting Objects

Recommended Posts

I'm currently working on putting together a text-based maze game, without the use of a multidimensional array. Therefore my problem is how to connect each of my Room objects to each other. I want to use a method add_connection which will create a bidirectional connection between two specified rooms. So I'll also have a function create_maze which will create all my separate Room objects and connects them. But I've come to a mind blank on how to do this connection, I think I'll need to use pointers but not sure. Thanks for any help.

Share this post


Link to post
Share on other sites
Ok, given that this is your first post in the gamedev forums, how about giving some mention about what level you feel comfortable programming at? Do you grok pointers yet, or are you still uncomfortable? Can you write your own classes? Oh, and what language you're using would be nice.

Share this post


Link to post
Share on other sites
If you are familiar with polymorphism, create a base tile class that you derive all your tiles from (floor, wall, stair, door, etc.), and store base pointers to these objects in your array. Your stair and door tile classes can have members describing where you'll warp when you enter them, say a map/room #, and the (x,y) of that map/room (or you can store a stair/door tag (integer) along with the map/room #, and warp to the stair/door of that map that has the same tag).

Without using an array (ie, a 2d array representing a static map) you'll have to have each of your tiles contain a position coordinate. Of course you'll store all your tiles in an array anyway, so you might as well just make a static map, as this will negate the need for coordinate pairs inside your tiles.

Let me know if you don't understand this, and I'll try to either explain better or offer a more simplistic solution.

EDIT: just re-read you last post and noticed you're not comfortable with pointers. In that case, you have two options:

1. Get comfortable with them. They are necessary to writing extensible, reusable code (even though their actual use often leads to opposite effects).

2. Get more comfortable with them. While you could create a single tile class that contained all information for all different kinds of tiles, this is both counter-productive, fragile and error-prone.

What if you wanted to add a new kind of tile, say a lava floor, that did damage to the player if she walks on it. You'd have to modify the one and only tile class to facilitate this new functionality, making sure the clients who use the tile class aren't going to be affected by your changes. You'll also have to recompile every file that references this class. As you project gets bigger, this is no fun.

With pointers, the situation becomes much more manageable, flexible and extensible, simply because you can hold a pointer to a base class that actually points to a derived class. This means working on code that's encapsulated (so if implementations change, the client's code (your engine) won't break because of it) and much more open to extension.

I'd suggest looking into pointers pretty heavily, not only so you know how to use them, but also so you know when not to use them (exposed interfaces spring to mind, as well as ordinary array constructs - use a std::container instead). Try and find a copy of Effective C++, and the free online book Thinking in C++ is an excellent read as well.

[Edited by - stylin on October 23, 2005 6:00:36 PM]

Share this post


Link to post
Share on other sites
I found this:
http://www.aaroncox.net/tutorials/miscellaneous/FunctionPointers.html

and this:
http://www.aaroncox.net/tutorials/miscellaneous/Pointers.html

really useful for understanding pointers.

There are other useful tutorials on their such as linked lists, etc..

Share this post


Link to post
Share on other sites
Ok that somewhat makes sense to me, but I was intending have base classes with derived anyways for different room/tiles. But I first want to get the connection right between the room/tiles.

Let's say I'm at a position which will take me to a new room in the cave.
So declaring each room used would be using the operator new:
Room * room1 = new Room("desc"); (or something)
Room * room2 = new Room("desc");
I'm not worried about deleting the rooms until later.

And if I was then to use the method for conneting the rooms would the call be similiar or such to:
room1->add_connection(direction, room2);
?
Am I making any sense?

I'm trying to get the simplistic basics done before I attempt anything else, as even this is getting me lost. Heh.

Share this post


Link to post
Share on other sites
A room has an array of tiles, and a world has an array of rooms. Such classes could look like:

class room {

private:
std::vector< tile * > map; // array of tiles
std:string tag; // tag for this room (eg. "cellar" or "kitchen")
...
};

class world {

private:
std::vector< room * > rooms; // array of rooms
...
};

Create a new world, then start reading your file. You may have some marker (int or string) that signifies the data that follows is room data. For each room you have on file, you create a new room object, fill in its map with new tile objects as you read them, and then add the room to your world's array.

The tiles themselves (your warp tiles anyway: stairs/doors) will store information regarding the particular room they warp to via the room tag. This kind of connection is done when you create the world (ie., with an editor) rather than when you load it. For a simple text file format, you can manually insert these tags if your map isn't too complicated and you don't already have an editor.

For example, say the first room has a tag of "cellar", and it has a stair tile that should take the player to room "kitchen" that also has a stair tile. You'd then have the stair tile in "cellar" contain a room tag of "kitchen", and a warp tag of 2, for instance. The stair tile in "kitchen" would have a room tag of "cellar" and a warp tag of 2 as well. When the player steps into the stair tile in "cellar", you know you have to display the room "kitchen", and you know that the player will start on the stair tile with warp tag 2.

As for run-time connections, yes, you could put a warp_connection object inside your stair tile, that you'd fill with the above information when you're loading the map.

Share this post


Link to post
Share on other sites
Quote:
Original post by ConstantlyLost
Ok that somewhat makes sense to me, but I was intending have base classes with derived anyways for different room/tiles. But I first want to get the connection right between the room/tiles.

Let's say I'm at a position which will take me to a new room in the cave.
So declaring each room used would be using the operator new:
Room * room1 = new Room("desc"); (or something)
Room * room2 = new Room("desc");
I'm not worried about deleting the rooms until later.

And if I was then to use the method for connecting the rooms would the call be similiar or such to:
room1->add_connection(direction, room2);
?
Am I making any sense?

I'm trying to get the simplistic basics done before I attempt anything else, as even this is getting me lost. Heh.


I would have something like this:

class room
{
std::vector<room *> OtherRooms; // vector of all rooms this room connects to
public:
void connectToRoom(room *R) //method for connecting to other rooms
{
OtherRooms.push_back(R);
}
};

// connects Two rooms to each other
void connectRooms(room *Room1, room *Room2)
{
Room1->connectToRoom(Room2);
Room2->connectToRoom(Room1);
}

// creates a one way passage from room1 to room 2
void connectRooms_OneWay(room *Room1, room *Room2)
{
Room1->connectToRoom(Room2);
}


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