• 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
3DModelerMan

Cocos2d-x architecture

5 posts in this topic

If singletons are generally considered bad, then why is Cocos2d-x considered to be so good? I've been using Cocos2d-x and I like it. But I've noticed that there are lots of singleton classes.
0

Share this post


Link to post
Share on other sites
Singletons are a design choice. There are many good reasons not to use them. There are also a few good reasons to use them, especially if they meet several well-established criteria.

It is a design tradeoff, and programmers must decide between these tradeoffs continually. Time vs space is one of the most common: you can store results in a cache or recompute.

They made design choices, and they seem to work for that library.
1

Share this post


Link to post
Share on other sites
More to the point, Cocos2d-x had most of the choices made for them. Cocos2d-x is C++ port of the popular Objective-C library Cocos2D, which itself is a port of a Python based library of the same name.

To a certain degree, trying to emulate an existing API is going to greatly restrict your design choices.

Another thing to keep in mind, many of the biggest flaws with singletons apply to C++ only ( especially in regards to threading ).

Also, be careful when listening to people that say blanket statements like "don't ever use X". Things are so rarely this black and white. Design purists are often so quick to discount other factors like ease of use or ease of learning. While a singleton may have it's downsides, it is easy to implement and easy to grok, both important factors.
2

Share this post


Link to post
Share on other sites
Serapth has a good answer, and I'd like to add that Singletons in C++ really have two cutting edges to them -- firstly are the implementation issues like threading, and secondly are the design issues it can promote. One of the design issues that you encounter with singletons is their global availability, which promotes poor separation of responsibilities and causes systems to become more tightly coupled (a bad thing), but simply having a singleton doesn't require you to break these design principles, you can still make good design decisions and face fewer of the consequences.

For people who are not informed enough about good design, or who are not responsible enough to hold themselves to good design over convenience, its usually better to forego the singleton so that bad decisions are not so easy to make.

In other languages, Singletons are not nearly so bad, or mean something else entirely (I've heard the term used in some languages to describe static classes).

You should *always* be wary of singletons in C++. You should *almost always* prefer other solutions in C++ when you have a choice. You should *never* give in to the convenient "solutions" that Singleton seems to provide in C++, because it nearly always goes directly against good design principles.
1

Share this post


Link to post
Share on other sites
Well what I was thinking is that it would be usefull for utility classes, like event managers. That way anything that implements the IEventListener can register in it's constructor, and unregister for events in it's destructor (as an example). And it wouldn't have to depend on other classes (like a GameManager or App class) to gain access to the event manager.
0

Share this post


Link to post
Share on other sites
What if you want to have two event managers? You might think now that you would never want more than one, but subscribing to singleton locks you into an early decision, and by not passing in the event manager through a proper chain of command, you have no easy way to later say that some class of events uses one manager, and another class of events uses another. What you're really saying when you say "It's great that no one has to tell me what manager I can use" is "I don't want to have to think about who tells who what." -- That's basically shirking your responsibility as a programmer.

Another example -- The convenience argument has been used as a justification for error logging singletons. Proponents would say "Aha! Logging! I just need one, and I need unfettered access from anywhere!" Well, what if you do want just one, but you want a different one tomorrow? Or if you want a different one based on a user setting? Singleton complicates that kind of scenario, it'd be far easier to just instantiate one class or the other and pass it down the chain as needed.

Finally, with proper design, you shouldn't be passing in a million different parameters through a dozen-plus layers of code. It just doesn't happen with proper design. Good design passes only what's needed, just as far as needed -- usually a parameter or two, if any, through 2-3 layers. Anything more than that and you should worry that it's a code-smell. A singleton implementation never reveals this kind of smell, even though all of its ill effects are still there.
1

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