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

How to avoid singleton

10 posts in this topic

I started a new project and I feel like I'm using singleton for everything.

For instance, I have skills that a character can do. I implemented that as singleton
[source lang="cpp"]class IceSpear;
class FireBall;
class Sprint;
class Teleport;[/source]
And to use one of the skill
[source lang="cpp"]character.doSkill(Fireball::getInstance());[/source]

I know I shouldn't use singletons a lot. How can I avoid this?
0

Share this post


Link to post
Share on other sites
Is there a reason Fireball needs to be a singleton and you can't just do
character.doSkill(new Fireball());
0

Share this post


Link to post
Share on other sites
The reason I decided to use singletons is that there's going to be only one instance of Fireball at all times and rather than calling new and delete, I thought it would be better to just make it singleton

wait.. I just got an idea that forced me not to use singleton.
thanks, anyways. Edited by lride
0

Share this post


Link to post
Share on other sites
yeah... you could just use a const to record the maximum number of fireballs, and a static member variable to record the current number of fireballs, and set the max at 1 and use a factory to instantiate a fireball which checks to see if the maximum number of fireballs has been reached before allocating memory for another instance... (totally off the top of my head, so don't trust me)

The main reason singletons are bad is because they're pretty much like publicly accessible global variables. If joe down the hallway decides that he wants to use your singleton and he mucks around with some internal variable values in his code and then you muck around in some other code which mucks around with internal variables, you're going to run into some dependency issues which will inevitably manifest themselves as bugs which are really hard to track down. Or, you'll develop your code around the assumption that the singleton is always going to be the lone instance, but five weeks later, your boss/customer/good idea fairy comes down and says "yeah... we want more than one fireball." and that's going to cause some serious refactoring of code. If you use singletons, expect to be up to your knees in muck, stumbling around and mumbling "fuck."

Just.... entirely forget what a singleton is. Erase it from memory.
0

Share this post


Link to post
Share on other sites
[quote name='lride' timestamp='1352049360' post='4997226']
The reason I decided to use singletons is that there's going to be only one instance of Fireball at all times and rather than calling new and delete, I thought it would be better to just make it singleton

wait.. I just got an idea that forced me not to use singleton.
thanks, anyways.
[/quote]
When you come up with a solution to a problem you've asked about, please post that solution. Someone in the future may search for something related, find your post, and just see "nevermind, I figured it out", which is frustrating. You're not helping anyone by not posting it. Posting your idea may also spur more discussion that leads to even better ideas.
2

Share this post


Link to post
Share on other sites
[quote name='lride' timestamp='1352048408' post='4997219']I know I shouldn't use singletons a lot. How can I avoid this?[/quote]
You can use a factory, then pass the factory around:

[code]
class Factory {
private:
map<string,Entity*> prototypes;

public:

Entity* create(const string&amp; name) {
if ( prototypes[name] )
return prototypes[name];
}

// Or:
// template<class T>
// T* create() { }
// template<> Fireball* create() { return (Fireball*)prototypes["fireball"] /*or etc...*/; }
};

struct Game {
Factory f;
};

void test( Character&amp; c, Factory&amp; f ) {
c.doSkill(f.create("fireball"));
// or: f.create<Fireball>()
}
[/code]
Or etc...

You can also use [url=http://stackoverflow.com/questions/4157687/using-char-as-a-key-in-stdmap]const char*[/url] over std::string if you need to avoid frequent memory allocations there.. Edited by fastcall22
1

Share this post


Link to post
Share on other sites
<p>[quote name=&#39;Suspense&#39; timestamp=&#39;1352138362&#39; post=&#39;4997648&#39;]<br />
[quote name=&#39;lride&#39; timestamp=&#39;1352049360&#39; post=&#39;4997226&#39;]<br />
The reason I decided to use singletons is that there&#39;s going to be only one instance of Fireball at all times and rather than calling new and delete, I thought it would be better to just make it singleton<br />
<br />
wait.. I just got an idea that forced me not to use singleton.<br />
thanks, anyways.<br />
[/quote]<br />
When you come up with a solution to a problem you&#39;ve asked about, please post that solution.  Someone in the future may search for something related, find your post, and just see &quot;nevermind, I figured it out&quot;, which is frustrating.  You&#39;re not helping anyone by not posting it.  Posting your idea may also spur more discussion that leads to even better ideas.<br />
[/quote]<br />
<br />
I could make fireball of different sizes, but to do that, I should construct multiple instances of fireball each containing different size info</p>
<pre>
[source lang=&quot;cpp&quot;]character.doSkill(new Fireball(3)) //3 is the size[/source]</pre>
Edited by lride
0

Share this post


Link to post
Share on other sites
[quote name='slayemin' timestamp='1352137568' post='4997642']
The main reason singletons are bad is because they're pretty much like publicly accessible global variables.
...
Just.... entirely forget what a singleton is. Erase it from memory.
[/quote]

I often see this statement, but I don't understand it, so please help me understand. That is, I believe I understand the problem of using global variables.

A problem with types is that they are (usually) global. That is, if there is a class defined in a header file, someone can include that header file and instantiate an object from it. This could lead to errors if the class manages a resource that there only should be one instance of. Using the singleton design pattern will guarantee that there can only be one instance.
0

Share this post


Link to post
Share on other sites
Whats wrong with creating a Fireball instance and passing a const ref or pointer?

game.init(){
.....
.....
fireball= new Fireball()
character= new Character();
}

game.iteration(){
.....
.....
character.doSkill( fireball)
}
0

Share this post


Link to post
Share on other sites
If you're okay with pre-loading all the resources associated with all the skills then why not just store them as resources with some kind of reference table and then create a skill class that manages any and all skills by referencing the table?

....

Now that I'm thinking about it, what are you actually talking about? Can you post or explain one of these classes? Are these classes that describe the skill's attributes or level, or are these classes that execute the skill? Edited by Khatharr
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