Jump to content
  • Advertisement
Sign in to follow this  
Brokenhope

C++ Classes, Hierarchy, Friends Chart

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

I have been creating a game in C++ (using GDI+)

 

I have read tutorials and examples but cannot figure out where to even start, and or, how this works. Currently, everything has redundant code.

 

Attached is a chart I came up with, I am wondering how it looks, and how I can at least get this connected. From there on, I can add the code.

 

Thanks.

 

 

 

Share this post


Link to post
Share on other sites
Advertisement
"Move" and "Type" seems weird to me, classes are normally objects, things that live in the game. Those two probably don't really exist (or if they do it's yet to be seen whether you really need an object for it, I think).

In general, if you don't know how to go, your thing is too complex. Cut stuff away from it to simplify. You can always add new things later, or move things around.


I would suggest to start much simpler, eg just a map and an player that moves on it.

Share this post


Link to post
Share on other sites

Sorry. I don't quite understand the tree. Do all of the classes inherit from Map? And is there a class Type that inherits from Item? I think what you mean is that class Type has a script as an attribute, Item has a type as an attribute, and that a Map has a group of moving objects, again as an attribute.

 

The general idea of class inheritance is that classes that inherit from another class have similar functionalities and attributes with the class they inherit from. Class inheritance is an IS-A relationship, like an Enemy is a Character, and a Character is a MovingObject. An attribute is a HAS-A relationship, like an Item has a Type, and a Type has a Script.

 

Like Alberth suggested, I think you need to start with something simpler first. Make the map first, make sure it renders the tiles well. Then give a player that moves in it. You'll gain more understanding as you go, and you can develop it further once you get the hang of things.

Share this post


Link to post
Share on other sites

Thanks for replies.

 

That is actually how far I got.

 

TL;DR

 

Maps perfect - as well as info

Player can be moved

Text Styles and everything done

Full spritesheet

 

I am wanting to half rewrite the code now that I want to add items and enemies.

 

I probably misunderstood inheritance or got it backwards.

 

But keeping it simple is what I am going to do. There is just one thing now.

 

Here

I have 3 types of "objs" Player, Enemy, Items
 

Each contains a vector.

 

I want one loop to iterate a single vector. How can I relate all 3 in a way I can add to a single vector?

Share this post


Link to post
Share on other sites
Look in terms of processes and systems. Identify what things stick around, and how those things communicate with each other.

Break down all the responsibilities. Keep breaking them down, and make a long list of each individual responsibility. When you can no longer break down the responsibilities, that is a good list to make classes from. Every class should have only a single responsibility. Communications patterns between them should be figured out to help you identify and remove dependencies.

What you describe in your graph is about general relationships. It is a start, but it has nothing to do with inheritance as it exists in computer code. Inheritance is an implementation detail on how to implement a bunch of items that are perfectly interchangeable but still different.

Also, this is not the time to figure out what lives in various data structures. That comes several steps later, after you have decomposed the things into a single responsibility and worked out communications patterns.

Share this post


Link to post
Share on other sites

Here

I have 3 types of "objs" Player, Enemy, Items
 

Each contains a vector.

 

I want one loop to iterate a single vector. How can I relate all 3 in a way I can add to a single vector?

You can contain objects of those classes in a single vector of the base class of those classes, like std::vector<Move>.

Share this post


Link to post
Share on other sites


You can contain objects of those classes in a single vector of the base class of those classes, like std::vector<Move>.

 

No you can't! That will cause slicing.

 

If you need polymorphism, you need to use pointers. So,

std::vector<Move*>, or preferably std::vector<std::unique_ptr<Move>>.

Share this post


Link to post
Share on other sites

No you can't! That will cause slicing.

 

If you need polymorphism, you need to use pointers. So,

std::vector<Move*>, or preferably std::vector<std::unique_ptr<Move>>.

 

Oopsies. My bad. I wrote that example without thinking. mellow.png

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!