• 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
rockstar8577

Design question

14 posts in this topic

Is it smart to design classes for a game using design composition? I think its called composition. The methods used on this page [url="http://gameprogrammingpatterns.com/component.html"]http://gameprogrammi.../component.html[/url]
0

Share this post


Link to post
Share on other sites
Is it overall a better choice? Does it have it's pro's con's? Is it better for only some games? Just trying to get an idea about composition.
0

Share this post


Link to post
Share on other sites
All design is a matter of tradeoffs. If your game would make good use of it then it's a good idea. If the game won't, then it might not be.
0

Share this post


Link to post
Share on other sites
Ohhh wow, haha more than i thought. And if i understood it correctly you were saying the pros for inheritance was when it was being used for polymorphism?

Also would it be a bad idea to mix composition with inheritance? It seems like in there should be a base object for everything just incase you ever do need to group them. Edited by rockstar8577
0

Share this post


Link to post
Share on other sites
Inheritance almost always uses composition. The inherited class has to be composed (composition) of [i]something[/i], unless it's just functions.

Also, usually the derived class adds more than just the inherited class' variables, so any derived class is almost always inheriting [i]and[/i]? composing (again: unless it's just adding functions).

Example:
[code]//This class is composition only, even though other classes inherit it.
class BaseClass
{
//Composition:
std::string stuff;
int moreStuff;
MyStruct otherStuff;
};

//This class has inheritance AND composition.
class DerivedA : public BaseClass
{
//Composition:
std::string additionalStuff;
};

//Functions don't typically count as composition, so this class is inheritance only.
class DerivedB : public BaseClass
{
void myFunc();
};[/code]

In the code above, BaseClass is only using composition, but not inheritance. DerivedA is using both composition [i]and[/i] inheritance. DerivedB is only using inheritance (since it doesn't add any member variables).

This class uses [i]neither[/i] inheritance nor composition:
[code]class ClassWithFunctionsOnly
{
void myFunc();
void myOtherFunc();
};[/code]
(Arguably, that class would be better off as functions within a namespace instead of a class - but it could legitimately be an abstract base class if one or more functions were virtual)

Yes, the primary pro of inheritance is polymorphism. The cons really start showing up when people go inheritance-crazy (defaulting to inheritance instead of defaulting to composition), and end up creating dozens upon dozens of abstract classes that aren't really needed by their program.
Abstract classes are classes that cannot be used themselves but define the common interface for other classes to use, for polymorphism. One of the goals of programmers is to make most of your code re-usable. This is good. [i]However[/i], in the quest to make code re-usable, many programmers get carried away with creating [i]overly [/i]generic solutions that aren't really needed, and don't bring them closer to completing their project. [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

In the big picture, programming happens in layers. Each layer of code helps hone down [i]from the generic[/i] (libraries, engines, and frameworks) to the specific (your project). If the code you are writing is [i]too[/i] generic, it'll just be another layer of code between the libraries and your end-goal, without actually carrying you further towards that goal. When abstract programming goes too far, you end up just creating another framework ontop of whatever you are already using, [i]thinking[/i] you are making your game, but really just making the unneeded framework that you'll eventually actually build your game upon... if you ever complete it, now that you wasted so much time accomplishing the nothing-framework that [i]looks like[/i] something impressive. [img]http://public.gamedev.net//public/style_emoticons/default/dry.png[/img]

There's an important balance between creating generic code that can be reused in other projects and creating specific code that actually furthers your current project. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img] Edited by Servant of the Lord
2

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1349809916' post='4988440']
That article is talking about Component Based Design, not Composition
[/quote]
That'll teach me to actually read rather than giving a quick skim. Thanks for taking the time to give a more detailed explanation! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
Heheh, I had to read the article twice to be sure. [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]
There was a little ambiguity with the writing as the author semi-mixes the descriptions of both together.
0

Share this post


Link to post
Share on other sites
So would this be a good way for drawing many objects?

[CODE]
class Drawable
{
int draw()
{
//Code
}
}
int main()
{
List<Drawable> items;

for (int index = 0; index < items.size(); ++index)
{
items.at(index).draw();
}
}
[/CODE]
0

Share this post


Link to post
Share on other sites
Yep. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

However, "Drawable" sounds like an abstract base class used in inheritance.
If it is a base class, for polymorphism to work you have to use either pointers or references. So your list would be [b]List<Drawable*>[/b], and you'd need to use new and delete to manage the dynamic memory, or preferably: [b]List<some_smart_ptr<Drawable>> [/b]to not have to worry about the memory AND enable polymorphism.

If it isn't an abstract base class, a better name would be "Item" (I'm assuming it's an item, since your list says 'items'). If it's not an item, perhaps "Object" or "GameObject" would make more sense.

Another hint, unrelated to the current topic:
When iterating over an array, and if you're confident that you are staying within 0 to size-1, you can use items[index] instead of items.at(index). The at() function checks bounds (but you already know you are within bounds) and throws an exception if you are out of bounds. The subscript [] operator doesn't do any bounds checking, and so has unpredictable results when you go out of bounds, but is faster when iterating over a container since you already know you are within bounds.
([size=2]And if you are using C++11, there's an even better way which I won't get into here[/size])
0

Share this post


Link to post
Share on other sites
This was just an example i could think off of the top of my head.

I actually forgot that you can do that for lists and vectors. Also I would enjoy the C++11 way, incase i do move to that standard.
0

Share this post


Link to post
Share on other sites
In C++11 instead of a normal for-loop:
[code]//Normal for-loop with indexing:
for(size_t index = 0; index < container.size(); index++)
{
DoSomething( container[index] );
}

//Normal for-loop with iterators:
for(Container<Type>::iterator it = container.begin();
it != container.end(); it++)
{
DoSomething( *it );
}
[/code]

First, C++11 includes the keyword 'auto' which greatly reduces the amount of typing, and makes the code cleaner, and makes the code easier to change:
[code]//Normal for-loop with iterators AND using C++11 'auto' keyword:
for(auto it = container.begin(); it != container.end(); it++)
{
DoSomething( *it );
}
[/code]
Or (with std::begin() and std::end() also working for non-container arrays):
[code]for(auto it = std::begin(container); it != std::end(container); it++)
{
DoSomething( *it );
}[/code]

Second, C++11 also introduces a new version of for() loop for when you know you are iterating from begin() to end() without change, and this further reduces the amount of typing, nicely avoids any (visible) iterator dereferencing, and guarantees to the compiler (and anyone reading the code) that you aren't altering the iterator within the for() loop.

This new for-loop we call 'range-for', and it looks like this:
[code]for(const auto &element : container)
{
DoSomething( element );
}[/code]

So in your example:
[code]some_kind_of_container< some_kind_of_smart_ptr<Drawable> > items;

for(const auto &item : items)
{
item->draw();
}[/code]
1

Share this post


Link to post
Share on other sites
Yes, exactly. Except C++ doesn't have a for-each loop. Microsoft had it as a non-standard extension in Visual Studio that wasn't portable.
for-range [i]is[/i] standard C++, and every compiler that claims to support C++ must support it from now on.

Visual Studio had "for each()", Boost had FOR_EACH() macro, Qt had foreach() macro. Now we have a single consistent and properly portable for-each. "[i]for(element-type : container)[/i]"
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