• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Plethora

What's the best way to do this?

2 posts in this topic

I posted a topic a little while ago in which the responses made me learn about and focus on the Dependency Inversion principle.  I've set about using interfaces rather than straight inheritance and I definitely see the benefit to a lot of the changes I've been making to my codebase, there are several things that already work much better than they did before.

 

So that said, there's one bit of my code that I'm having a bit of trouble refactoring.  I'll try to explain it as concisely as possible, but there's a bit of complexity here:

 

Original Implementation:

I had a parent class called Drawable and several types inherited from it including the relevant ones for this discussion called Tile and Enemy.  I then had a class called Battle_Map and another called Graphic_Arranger.  This all worked together like so:

class Drawable
{
private:
    point position; //point is just a simple struct the has two ints, x and y.

public:
    Drawable();
    point getPos();
};

class Tile : public Drawable
{
    //Does stuff, not relevant here
};

class Enemy : public Drawable
{
    //Does stuff, not relavent here
{;

class Battle_Map
{
private:
    std::vector<Enemy> enemies;
    std::vector<Tile> tiles;

public:
    std::vector<Drawable> getDrawables();
};

//Implementation of getDrawables()
std::vector<Drawable> Battle_Map::getDrawables()
{
    std::vector<Drawable> toDraw;
    toDraw.insert(toDraw.end(), enemies.begin(), enemies.end());
    toDraw.insert(toDraw.end(), tiles.begin(), tiles.end());
    return toDraw;
}


 

Graphic_Arranger had access to Battle_Map, and would then call getDrawables().  It then did stuff like sort the objects and arrange them such that the graphics engine could draw everything properly.

 

Now, this implementation worked, but it had drawbacks.  For one thing it was making copies of everything, and I would be passing vectors full of stuff around at every iteration.  For another, as my program grew, using a straight parent class like Drawable was bound to get frustrating especially when multiple inheritance (which I would have needed) came into play.

 

SOOOOOOO, for my fancy new implementation, I created the interface class iDrawable.  It has two virtual methods called getPos() and getKeyID().  Tile and Enemy now derive from it, and they have their own versions of those methods.  Ok, so far so good, no problems.

 

Well, now I can't have a std::vector<iDrawable> as its an abstract class.  I'll need to use pointers to get the same effect.  I'm fairly inexperienced with the use of c++ smart pointers, so I thought this would be a good place to learn, but I'm having issues.  Rather than get into the specifics I think it would be more productive to ask the following questions:

 

1)  What should the return type be for Battle_Map::getDrawables()?  I first thought that I would have to use a shared_ptr seeing as I am in some sense moving ownership around, but maybe that's not true?  

 

2)  What if I wanted to return a pointer to a vector full of pointers... that seems overly complicated, but is it bad?  Would I want to do it?

 

3)  If I have a std::vector<std::shared_ptr<iDrawable>> object, and I copy it... would it contain addresses to the objects as I would want it to?  Or would it contain useless gibberish pointing to uninitialized memory?

 

I'm sure there is a simple solution here, but I've reached a point where the more I think about it, the more I confuse myself.  I'm gonna put up this post and step away for a bit, lol.

 

0

Share this post


Link to post
Share on other sites
Don't make enemies inherit from a drawable object, rather you should have the enemy use a drawable object.

Make the enemy a subclass of an entity object, that other entity objects can then inherit from.

Use smart pointers in your vectors, the standard library has shared pointer you can use (shared_ptr). This way objects don't have to be copied everywhere.
class Entity
{
    std::shared_ptr<Drawable> drawable;
public:
    Entity(const std::shared_ptr<Drawable>& drawable);
};

class Enemy : Entity
{
public:
};

Now that you have separated your drawable class from your entity class you can change how an enemy is drawn simply by passing it a different drawable. Whenever the enemy moves or changes in any other way, it updates its corresponding drawable object. This may seem like extra work but in the end it is a much better design and will let your code be much more flexible and your project can continue to grow without coming apart at the seams. Edited by HappyCoder
1

Share this post


Link to post
Share on other sites

First off, I want to +1 your answer, but its pretty damned cool that your rep is currently 666, so I'll do it later.  smile.png  (EDIT:  You tipped over to 667, rep points awarded!) 

 

 

Whenever the enemy moves or changes in any other way, it updates its corresponding drawable object.

 

But to the point at hand... I'm resisting moving to a component-entity approach.  Believe me when I say I understand the theory behind it and its likely that whenever I start my next project (or revamp this one with greater ambitions in mind) I will use it, but for now that's off the table.  Besides, in my current implementation, your quoted statement is already true.  iDrawable is directly linked with the Enemy object (or Tile, or Player Character, or Minion, etc.).  It has access to a few small bits of data (namely, position, keyNumber, and a bool isAnimated).  Neither iDrawable, nor my now deprecated Drawable class did anything graphical directly, they were merely interfaces that would grant my Graphics class the information needed to draw the appropriate sprites in the appropriate places.  Again, your approach is very possibly better, but its a completely different paradigm.  Unless I'm wrong, moving in that direction would likely mean starting from scratch. 

 

On the other hand, std::shared_ptr is definitely the way to go.  I'm curious though, most everything I read says to use std::unique_ptr whenever possible, but it seems to me that unique_ptr is severely limited in its usefullness.  I know that shared_ptr has a bit of overhead with it, but is there more than just that reason why I should try to use unique_ptr when possible?

Edited by Plethora
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0