• 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

Alternative to context objects

4 posts in this topic


I have a "Stage" class which act sort of like a context object and contains every service and module that will be used in the same level. Most services live and die with the stage, but some are reference to upper services in the game. The stage also contains a list of entities who will use the services.

Entities come in vast varieties and each uses different services, so I pass the Stage object in their constructor and let them decide what to use.

Some entities need to load a sprite, which will be cached for later use in the same stage:

Some entities need to check for map collisions:

Some entities need to override camera control:

This works, but every time I add a new service to Stage, every entity needs to be recompiled as they all depend on Stage, which is somehow annoying.

I also have a factory which maintains a map of every entity and its "Key", so I can read and create entities from a file. The factory have to call functions with same signatures, such as
Entity *Create(Stage *stage);
so passing individual service a certain entity need to its constructor don't seems feasible.

I have written several games, and they all end up having a huge class containing everything anyone might need. Is there a better solution to this?

I have looked into the service locator pattern but I'm not sure it is the right solution like this:

Singletons won't work because there might be more than one Stage (split screen 2P, etc...)

Share this post

Link to post
Share on other sites
1) Instead of calling functions send messages. Kind of ugly, but would work

2) Use Components

3) Use mixins

I prefer components myself.
Have a scene class, which contains a list of game objects.
A game object is a transform, a name, a layer and one or more components.
The component class is a generic interface, it has abstract functions to init, update, render, etc...
Subclass the component into things like: script component, camera component, sprite component, renderer component
So, you have a game object and you attach a sprite component to it. The sprite component does whatever needs to be done to load in a sprite (texutre, uv coords, etc...) It will however not render anything. For that you attach a render component.
Each frame you loop trough your cameras, each camera determines which objects are visible to it, then uses the render component to render the visible objects.
Delegate each specific responsibility into it's own component, and if you can have a script component to script your game.

Unity3D's script component lifecycle:

Share this post

Link to post
Share on other sites
I agree that this issue can be addressed with component based design.

I currently favor a design in which components are those parts of entities which are associated with specific engine systems ("services"). They're created (and processed) by the engine systems themselves. And entities are collections of components.

The components generally need to know about a couple of other requisite components types, and often don't even need to know about their own engine system, since the system knows about them.

Note that this still leaves us with entity constructors which know about components, and the engine systems which create them.

This can be resolved by designing an entity factory, with which each engine system registers itself (rather than one which already knows about them), that does data driven entity construction (is given a list of component type IDs and uses the registered systems to generate an entity containing instances of those components).

That should eliminate the need to recompile code for non-dependent entity types when adding new systems. Although I must admit, I'm not sure how much this saves you in terms of re-compiling. On the one hand, there will still be some high level code (AI) which will need to know about a lot of the systems/component types. And on the other hand, adding systems doesn't seem like something which should be happening often enough for this to be an annoyance.

Share this post

Link to post
Share on other sites
I recently solved this problem with what I call a "doubly-opaque pointer" (is there a named pattern or idiom for this?). The idea is based on a service provider object, which uses (abuses) forward declarations, template functions, and the opaque pointer idiom to provide a strongly typed pointer to its users. Only the object that constructs the ServiceManager will need to construct (and/or setup) all of its services. When setup correctly, only the ServiceManager.cpp and the object that sets up the ServiceManager will need to be recompiled if a service is added to the ServiceManager.

Here's a sample code sample:
// ServiceManagerImpl.h
class ResourceManager;
class SceneManager;
class RenderManager;

struct ServiceManagerImpl {
ResourceManager* resman;
SceneManager* sceneman;
RenderManager* renderman;
// ServiceManager.h
struct ServiceManagerImpl;

class ServiceManager {
ServiceManager(ServiceManagerImpl* impl);

template<class ServiceType>
ServiceType* get();

ServiceManagerImpl* impl;
// ServiceManager.cpp
#include "ServiceManager.h"
#include "ServiceManagerImpl.h"

ServiceManager::ServiceManager(ServiceManagerImpl* impl) : impl(impl) {

template<> ResourceManager* ServiceManager::get() { return impl->resman; }
template<> SceneManager* ServiceManager::get() { return impl->sceneman; }
template<> RenderManager* ServiceManager::get() { return impl->renderman; }

And a sample demonstratation:
// Game.cpp
void Game::Setup() {
ServiceManagerImpl* impl = new ServiceManagerImpl;
impl->resman = new ResourceManager;
impl->renderman = new RenderManager;
impl->sceneman = new SceneManager;

theServiceManager = new ServiceManager(impl);

void Game::Dothings() {
for ( int idx = 0; idx < entities.size(); ++idx )
// MonsterEntity.cpp
#include "MonsterEntity.h" // "ServiceManager.h" (implied)
#include "RenderManager.h"

void MonsterEntity::Update(ServiceManager& services){
// PlayerEntity.cpp
#include "PlayerEntity.h" // "ServiceManager.h" (implied)
#include "SceneManager.h"

void PlayerEntity::Update(ServiceManager& services) {

As you can see, a new service can be added to ServiceManagerImpl.h with an accompanying template specialization for ServiceManager::get in ServiceManager.cpp without affecting ServiceManager.h and none of the ServicesManager users need to be recompiled. Only Game.cpp and ServiceManager.cpp need to be recompiled (expensive for Game.cpp, relatively light for ServiceManager.cpp).

I'm not sure how this rates on the OOP-perversion scale, but I think this is pretty neat.

Here's an image illustrating such an approach as well
[url=http://i.imgur.com/rbPhD.png]http://i.imgur.com/rbPhD.png[/url] Edited by fastcall22

Share this post

Link to post
Share on other sites
I like the doubly-opaque pointer, like service locator it relieves the long list of getter method but doesn't require service ID managing or RTTI and will also tell you what's wrong at compile time.

As for components, I think I still need to pass around a context object around for their constructor or update method(The doubly-opaque pointer could also help this) , or use factory for components(which should be shorter than exposing all those services).

I also agreed that the recomplie issue is a bit overrated. I tend to add features one by one to make sure they all work fine. As the game architecture become more stable I shall seldom need to change the context object.

Thank you all for replying.

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