Jump to content
  • Advertisement
Sign in to follow this  
chiranjivi

Beginner Q re. objects

This topic is 2830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Writing a simple game. This game has a GameWorld object. The GameWorld object holds a bunch of stuff, such as the Player object, and a vector of items that exist in the game area.


class GameWorld
{
public:

GameWorld();

Player p;
vector<Item> gameworld_items;
};


The Player object also contains a vector of items representing the player's inventory.


class Player
{
public:

Player();

void manage_inventory();

vector<Item> inventory;
}


So, in the course of playing the game, while the Player is running around the gameworld he can pick up items he comes across. This is simple enough: remove the item from the gameworld item vector and put it into the player's inventory vector.

If the player wishes to manage his items, he can view his inventory - I've implemented this as a method of the Player object, which seems logical. The issue is that if the Player wants to drop an item from the inventory menu, I can't see a simple way to pass the item back 'up the chain', so to speak, to the gameworld object that contains the player. I figure I could make it work by passing the gameworld's whole vector of items to the Player every time he looks at his inventory, just in case he wants to drop something, but this seems kinda heavy-handed? I figure there must be a simpler/more elegant way of doing this kind of thing?

Share this post


Link to post
Share on other sites
Advertisement
Hello,

You can add a pointer to the GameWorld object in the Player object. When the dropObject method is called, it remove the objet from the player item vector and put it into the world item vector.

Share this post


Link to post
Share on other sites
You could have a bool for each item that indicates whether or not it has been picked up by the player. By default the bool would be false for all objects, but when the player picks one up it could toggle to true. Dropping the item toggles it back to false again. If you need to work with items in the gameworld that have not been picked up by the player, it would be pretty easy to write a loop that only works with items whose bool is false.

That way you only update the gameworld's item vector when you need to, and you don't have to worry about frequently re-sizing it.

Share this post


Link to post
Share on other sites
The pointer idea sounds good. Then you wouldn't have to copy the gameworld item list, you just reference to it in order to manipulate from the player class.

Share this post


Link to post
Share on other sites
Thanks both guys for your replies!

ericbeg,


Hello,

You can add a pointer to the GameWorld object in the Player object. When the dropObject method is called, it remove the objet from the player item vector and put it into the world item vector.


This sounds like a simple and effective solution. I have to define the Player class before GameWorld, as GameWorld contains a Player, so when I first tried to add this pointer, I obviously got:

error: ISO C++ forbids declaration of `GameWorld' with no type

So in order to remedy this, I tried to forward-declare the GameWorld class...


class GameWorld;

class Player
{
public:

Player();

void manage_inventory();

vector<Item> inventory;

GameWorld* parent_gameworld;
}


The compiler does not like this, and says:

error: invalid use of undefined type `struct GameWorld'

I am at a loss now :huh: How can I make the compiler accept a pointer to GameWorld within the Player object? Apologies for asking what I am sure is a very basic question.

Thanks again for your help.

Share this post


Link to post
Share on other sites
This is indeed the way to go. Maybe you just forgot the semicolon at the end of the class definition.
And have in mind that forward declaration works only with pointers. You migth need to declare the player as a pointer too in the GameWorld class.

Share this post


Link to post
Share on other sites
This is indeed the way to go. Maybe you just forgot the semicolon at the end of the class definition.
And have in mind that forward declaration works only with pointers. You migth need to declare the player as a pointer too in the GameWorld class.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!