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

Alternatives to singletons for data manager?

31 posts in this topic

One of the clever, but more contraversial alternatives I've found for sharing data between classes is to use inheritance/static fields. Let's say you're programming a 3D editor of some sort, and a list of vertexes in your model has to be shared between many classes. The options in this thread include simply passing the list as a parameter, setting up a singleton, or setting up a container class that gets passed at runtime.

With my inheritance method, you set up an abstract class that contains protected static fields for shared information - such as the list of vertexes that has to be shared. Any class that needs access to those vertexes would inherit the abstract class.


I know you've put a lot of thought into your way of avoiding globals, but your editor example is a good example for passing references. At first many may think, I have this model editor and it can only do one model at a time. I've made a model editor and made the same assumption. Sure if that's true, just use a global or singleton rather than a more complex organization just to avoid using globals.

But really, will an editor only store ONE model? Maybe this editor allows you to load a character model, but it can also load attachments like weapons. The editor can manipulate the character and the attachments. It seemed like only one model was needed, but after adding more functionality, now you need more than one. So when you open a dialog or tool in the editor, the dialog or tool will need to be passed a reference of what it will manipulate.
0

Share this post


Link to post
Share on other sites

A basic philosophy I sort of follow is that code should be (in order):

  1. As optimized as necessary.
  2. Simple
  3. Transparent

Singletons usually fail on the transparency case, but they're worth using if the alternative has huge code complexity costs.  Dependency injection is fine for a manager type of thing, but despite what I used to think, there are cases where globals/singletons have use.  Logging is the canonical example.  For example, I have a fractal library with deeply nested trees of noise/fractal modules.  If I want to enable logging on  those modules, I can do one of three things:

  • Pass a logger as a depdency to every module (nearly my entire library -- 30+ classes).
  • pass a parent reference to propagate a logging message to the root, which handles logging itself (isn't this just isomorphic to throwing an exception and catching at root?)
  • just call a stinking logger global.

Dependenccy injection is "ugly" by some metrics, and it's simply too ugly for this use case.  Ergo, I use a singleton.

0

Share this post


Link to post
Share on other sites

One of the clever, but more contraversial alternatives I've found for sharing data between classes is to use inheritance/static fields. Let's say you're programming a 3D editor of some sort, and a list of vertexes in your model has to be shared between many classes. The options in this thread include simply passing the list as a parameter, setting up a singleton, or setting up a container class that gets passed at runtime.

With my inheritance method, you set up an abstract class that contains protected static fields for shared information - such as the list of vertexes that has to be shared. Any class that needs access to those vertexes would inherit the abstract class.


I know you've put a lot of thought into your way of avoiding globals, but your editor example is a good example for passing references. At first many may think, I have this model editor and it can only do one model at a time. I've made a model editor and made the same assumption. Sure if that's true, just use a global or singleton rather than a more complex organization just to avoid using globals.

But really, will an editor only store ONE model? Maybe this editor allows you to load a character model, but it can also load attachments like weapons. The editor can manipulate the character and the attachments. It seemed like only one model was needed, but after adding more functionality, now you need more than one. So when you open a dialog or tool in the editor, the dialog or tool will need to be passed a reference of what it will manipulate.

I'll admit my example really wasn't that great, but inheritance based global access isn't around to replace something like a class full of references.

 

An infinitely better example would be an static-inheritance class giving access to something like the graphics device. On one of my older projects, the "sprite" class is typically accessed very far down the call stack. From the entrypoint of the program, you have to go through a few layers of gamestate logic, then UI logic, then all the way at the bottom is the sprite class. The sprite class needs access the graphics device, or whatever class is handling the graphics device. Instead of passing down a reference to the graphics device down a dozen layers of call stack, the Sprite class would inherit from a static class that contains the graphics device.

 

This way, access to the graphics device has to be explicitly defined through the use of inheritance, and any usage of the graphics device can be found by just looking at all the child classes that inherit from it. In addition, it's one less piece of trash floating around the global namespace.

 

I'll admit it's hacky as hell, but I'd rather have an ugly hack than restructure 10k lines of UI logic.

0

Share this post


Link to post
Share on other sites

A basic philosophy I sort of follow is that code should be (in order):


  • As optimized as necessary.
  • Simple
  • Transparent
Singletons usually fail on the transparency case, but they're worth using if the alternative has huge code complexity costs.


a global instance of a normal class is less complex than a singleton and more flexible.

Singletons are just a global with a "there can be only one" restriction and some makeup, If you don't absolutely need to restrict the number of instances to 1, just use a global(put it in a sane namespace), using singletons because "globals are evil" is silly since a singleton is a global. (and globals are only really evil if their state can be modified globally, stateless functions, constants, etc are perfectly fine to allow global access to). Edited by SimonForsman
1

Share this post


Link to post
Share on other sites

An infinitely better example would be an static-inheritance class giving access to something like the graphics device. On one of my older projects, the "sprite" class is typically accessed very far down the call stack. From the entrypoint of the program, you have to go through a few layers of gamestate logic, then UI logic, then all the way at the bottom is the sprite class. The sprite class needs access the graphics device, or whatever class is handling the graphics device. Instead of passing down a reference to the graphics device down a dozen layers of call stack, the Sprite class would inherit from a static class that contains the graphics device.

 

This appears to be more a flaw of your overall design. Normally, the further down you go, the less you information you need - and the less things you have to pass on. Sure there is exceptions to this, but if you really need to acces the graphics device down so far that you have to pass it down several layers without using it, there is something horribly flawed in the first place. Your classes probably have too much responsilibity, aren't encapsulated well enough, etc... . I remember the same thing from one of my first projects, where I had to pass the DX9-device around EVERYWHERE. But this was, also, a design flaw per se and not a inconvieniente thing solved by inheriting from a class that grants access to a graphics device.

 

Seriously though, this inheritance-thingy is not much better than a global or singleton. In fact, it is even worse, because it create a relationship that has no purpose other than to save passing a parameter. Inheritance should always represent an "is a" relation. Whats that in your case? "sprite" is a "convienient static data container for a g.d."? That sounds about wrong.

 

I'll admit it's hacky as hell, but I'd rather have an ugly hack than restructure 10k lines of UI logic.

 

Well, you better would - otherwise you'll eventually reach a point where there is no back and forth, where the code overall becomes so ugly and hacked together that you can't develope it without insane effort, and where refactoring is virtuall impossible, forcing you to start over. But after all, thats up to you - just saying that the earlier you start to rely on hacks (namely things that you yourself call "hacky") the more messed up your code gets...

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