Hi I have a question with regards to storing and retrieving a players location in a game. At the moment I'm working on a text based dungeon game, and one of the things I am having trouble implementing is, a way to move between rooms in the game world (I.E. typing "GO NORTH" and having the player move to the north room). It seems to me that pointers would be an Ideal method of doing this, as all I would have to do is just create a pointer called current_location or some such, and update it every time the player moves to another room in the map. (I already have the directions and a room class done, I just need a way to "link them together" so to speak.)
please note that this is pseudocode:

class player
{
	int HP;
	int Dex;
	int Intel;
	int Str;
	int Wis;
	bool HAS_MAGIC;
	int current_room;
        *current_loc;

and when the user decides to type in a direction (Say "North", for instance):

if (direction==North){
player.current_room = room.array[1];// now the player goes north
*current_loc = player.current_room;}

This is, in a nutshell, what I want to do. I have it all planned out on paper, but I'm turning to these forums because you guys know your stuff and I would appreciate the help. So if you have any ideas/brainstorms/better methods, feel free to fire away!
*note that my direction system consists solely of enums, as I felt a parser would be a little too difficult to implement.

Thanks for your help!

Recommended Answers

All 3 Replies

I'll presume the app is 2d based for easier explaining and thus, you have a map grid of say 10x10. The starting point would be irrelevant.

Make 'current_loc' a pointer to 2 integers or make 2 integers of x and y. I'll take the first example. I'll use 'xxxx_bounds' as an example of boundary checking which could be replaced by anything you want.

if(NORTH){
   if(current_loc.y - 1 > north_bounds)
      current_loc.y -= 1;
}
else if(SOUTH){
   if(current_loc.y + 1 < south_bounds)
      current_loc.y += 1;
}
else if(EAST){
   if(current_loc.x + 1 < east_bounds)
      current_loc.x += 1;
}
else if(WEST){
   if(current_loc.x - 1 > west_bounds)
      current_loc.x -= 1;
}

If you decide to have a grid which is not a rectangular shape but instead irregular, make a 2d array for each potential position on the map. Each array item should contain booleans for north/south/east/west which you can set and thus create a map through the 2d world.

Hope this helps :)

I don't understand why current_loc even needs to be a pointer? Why not just have each player have a member object of your location enum? Maybe I missed something?

I don't understand why current_loc even needs to be a pointer? Why not just have each player have a member object of your location enum? Maybe I missed something?

I had assumed that the use of pointers would be ideal for this, as they store the address of an objects location (thus when the player changes room, if I want to query the location for something such as a random ambush, it would be a matter of just checking the current_loc pointer. Apologies if this post seems vague,I am just a novice interested in writing some games to get a good handle on OOP concepts and the c++ language as a whole.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.