Dynamically Allocating Objects, and Making Your Program Know How Many

Started by
3 comments, last by Bombario 19 years, 5 months ago
Here's the rundown: I'm writing a text RPG engine in C++, and I have different classes such as CTown, CMonster, CItem, etc. I want to be able to just make a bunch of CMonster objects, CTown objects, etc, and have the program make the world out of that. Each class has just a few general attributes, since I'm not very good at programming yet. I want to be able to just give each Monster (or Town, or Item) object an ID # and have the program be able to list ALL of them. Would this just be a search function? I'm wanting the engine itself to do this; I know I could just make the game code itself do this, but then what's the point of the engine? So my friend, who's more advanced than I, was telling me I had to do something with virtual functions or whatnot. I didn't quite understand all he was telling me. The problems I'm facing are: - How to let the player know all of the towns they can go to and where they are. - How to just have to specify item IDs for a shopowner, and he'll automatically have those items in stock. I don't know how else to explain it, other than the fact that I don't know what the above problems would entail doing to implement into the engine. Ask questions if you don't understand; I'll gladly answer them for whatever help I can get.
"Somebody should make a game about pirating video games. That would be interesting."~Chandler
Advertisement
Come on...could someone at least point me in the direction of a tutorial? I don't even know what I'm looking for.
"Somebody should make a game about pirating video games. That would be interesting."~Chandler
I'm not really seeing a problem here. Basically, you want a list of monsters/items/towns/etc in your game, which are matched to an ID (or a key). There are a lot of ways you could do this. You could use a vector to store objects of a particular class, and include an id within the class itself. You could use a linked list. You could set up hash table, which is sort of like an array, except you access elements with their key (or ID) instead of their index number. You just tell the engine what to look for, and write the game accordingly.

Or did I miss you question/point?
What seems to me that you want to do here, is having a hash table. When you create a Town, you'd want to add it to the table, with a given ID. So, here's a first pointer:

std::map<std::string, CTown *> m_townList;

If it does n't make sense to you, go and read up on STL maps.

I did n't quite get what you meant by the second requirement, but if my guess is correct, you want all shopowners to contain a list of items. So, here goes...

typedef std::list<CItem *> ListOfItemsType;

and then in your shopowner class, have a member variable:

ListOfItemsType m_itemsList;

You'd probably want to provide some helper functions for adding, deleting and querying objects in your ShopOwner class. Go and read up <once again> on STL.

As for those virtual functions stuff. What your friend is trying to tell you probably is to have a CItem Base class from which maybe all your other objects would derive from. If you don't understand what I mean by this, you might want to go and really brush up on your C++ skills first.

There are probably more than one ways to do whatever it is you are trying to implement, this is just one of them I can think of, off the top of my head, based on the information you have given so far...

I hope this helps...
All of that points me in the right direction. Thanks, guys.
"Somebody should make a game about pirating video games. That would be interesting."~Chandler

This topic is closed to new replies.

Advertisement