Jump to content
  • Advertisement
Sign in to follow this  
rpg gamer 17

Finally Attention

This topic is 3263 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

This issue has been drilling it's way into my head for quite some time now. I am going to create a game with people. Naturally these people will have to reproduce. That's the problem. The objects(people) are handled by the object manager which runs the functions of the polymorphized class the objects are public too. Did that make sence? basically this(pseudo code):
class PObject
{
public:
 virtual void DoStuff(void) {};
};
class People : public PObject
{
public:
 void DoStuff(void)
 {
  //doing stuff
 }
};
class ObjectManager
{
private:
 vector<PObject*> ObjectList;
public:
 void Init(void)
 {
  ObjectList.push_back(new People);
 }
 void Start(void)
 {
  //Might be wrong:
  ObjectList.front()->DoStuff();
 }
};

I know I'm going to have to set male and female objects as well as the need to have a partner(after proper housing and a decent job). How would I go about creating a new instance of the object and adding it to the object manager? I know push_back but where would I add it to the list(ObjectManager, People, ect)?

Share this post


Link to post
Share on other sites
Advertisement
There's one thing you can do to simplify this - make a system that registers an entity with a #define and accesses it with a single function. The idea behind this - #define builds a static class with a constructor that registers a function pointer+name(+maybe some additional data) pair/group of objects on the entity list so you could just look it up and call the function that would return you a properly created custom object that is derived from the base object/entity.

Factories are a really lame thing to do for things like this because you have to recompile/edit a lot of code whenever you add an entity, and that happens quite often.

Share this post


Link to post
Share on other sites
Quote:

Factories are a really lame thing to do for things like this because you have to recompile/edit a lot of code whenever you add an entity, and that happens quite often.

This is only the case for very poorly designed, non-generic factory implementations. One of the goals of the factory pattern is to reduce this implementation (and maintainence) effort.

Quote:

I know I'm going to have to set male and female objects as well as the need to have a partner(after proper housing and a decent job).

How would I go about creating a new instance of the object and adding it to the object manager?
I know push_back but where would I add it to the list(ObjectManager, People, ect)?

Please explain your problem better. Your example is a bit too abstract and your description a bit too vague. It sounds like you're planning on using inheritance to implement a different class for male and female people? This would be a bad idea, it'd be better to make gender a property of the Person type.

It also sounds like your trying to trying to derive everything from one base type (PObject) and store all instances helter-skelter in one big list. This is often disadvantageous because not everything should be treated the same. However, for things that can be, simplying storing them as a vector of base-class-pointers like you're doing is fine.

You also seem to have questions about creating new instances of subtypes. A factory (google for "factory pattern C++") is a typical way this is handled. Despite his comments disparaging factories, snake5 has essentially describes a crude generic factory (although there exist more advanced, less ugly-#define-hack ways of implementing this in C++). But you're going to have to provide more information about what you want to do w.r.t to creating objects.

Your ObjectManager example code is basically useless as a means of imparting your intent, the method names don't seem to match what is actually going on, and you haven't told us much about what you want them to do or what you envision the interface to be, et cetera. Please provide more details.

Share this post


Link to post
Share on other sites
Quote:
This is only the case for very poorly designed, non-generic factory implementations.

Oh, maybe I've a bit misunderstood the definition of that factory pattern and maybe even the pattern I described is the same factory pattern, just implemented differently.. :D
EDIT: Oh, right, really. :)
Quote:
less ugly-#define-hack ways of implementing this in C++

Could you please tell me about one of those ways? I'd like to know those ways because they might be useful to me in the future (for other games that require entity handling like this).

Share this post


Link to post
Share on other sites
It seems I have a knack for not being able to explain myself. >.<

The people class(later named person)
Will keep track of the persons job, housing, genger, income and etc.
When the job, housing, and income are suitable they will look for another person of the opposite genger and move in together. Their children will have traits similar to the parents. If their mother or father was a farmer they will be more happy working at a farm than at a logging camp(unless one of them worked there as well).

The example I gave was created in about 2 minutes do to the location I was at.
It was to show how I am using polymorphism and inheritance to handle the objects and that the objects are pushed onto a vector list.

Basically I'm asking how I could create an object and add it to the list from within the object. The factory pattern does make sence, but I don't know how I would get the parent of the newly created object and set stats according. Unless I set the list with-in the factory and made changes on creation.

http://login2win.blogspot.com/2008/05/c-factory-pattern.html
Was the tutorial I looked at.

Quote from jpetrie:
not everything should be treated the same

This comment made me rethink several features of my game.
I have several systems that can be used as I'm using them. It's the manner of when they should be used that's the problem. Even though my game will be top down and the graphics will be limited to colored blocks, I've been thinking about a 'layer' system. Such as if a person is behind a building draw the building first. As of now I iterate through the list drawing all items on the screen regardless of there locations. The layer system would iterate through the list handling/drawing layer one items on the screen and proceed to layer two and etc. In order to accomplish this I'll have to create several lists. One for grid & people(layer one), buildings & plants(layer 2), User Interface(Layer 4).

There lies the possiblity of slowing the game trying to iterate through the lists and handle the processing and rendering functions. Any insight on this would be appreciated.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!