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

Static Classes in c++

13 posts in this topic

I've been passing an GUIDManager around in my project to where ever it's been needed, this was causing some problems. I was wondering if making the whole class static would cause any issues? Before I start correcting all reference to this class, I want to make sure that I won't come into some issue in the future.

namespace Pro{
	typedef unsigned int GUID; 
		class GUIDLookup{   
			static unsigned int allocatedBitCount;
			static std::unordered_map<std::string, GUID> nameMapping;
			static std::stack<unsigned int> releasedGUIDs;
			static GUID newGUID();
		public: 
			 
			static GUID newGUID(const std::string& name);
			static void releaseGUID(const std::string& name);
			static void releaseGUID(GUID);
			static GUID getGUIDFromName(const std::string& name);
			static std::string getName(GUID _id);
		}; 
}

 

Edited by Joshhua5
0

Share this post


Link to post
Share on other sites
What is your real problem you are trying to solve? You don't want to pass an object around?

There are reasons to mark a function as static, usually when you have a reason not to need the this pointer.

There are times it is a solution to problems, but with the very brief description, I don't think that is the answer. I think the deeper problem is the "manager" class not having a single responsibility. Most "manager" classes are a symptom of a design flaw.

In this case your "manager" looks like a name/id lookup. Is there more to the class?
1

Share this post


Link to post
Share on other sites

I was cleaning up my code and thought one way would be to stop passing around the GUIDManager.

  

You're right the it is a name/id lookup.

 

I've also decided to change the name to GUIDLookup

Edited by Joshhua5
0

Share this post


Link to post
Share on other sites
Looking over your other posts on trying to get Lua objects to work with game objects, I'm guessing this "manager" class actually belongs as a part of a larger class. It probably belongs as part of a resource cache system. A resource cache can load and unload objects if needed, find the objects if they exist, and potentially (depending on how you want to do it) load up a proxy while you wait for the resources to load. One facet of a resource cache is to translate names into object pointers.

When you try to name a class, the name "manager" is usually a clear marker that you aren't sure about the classes responsibilities. Sometimes the class is really a subset of another class responsibility, sometimes it is a collection of too many responsibilities.

Of course, if this bit of code has nothing to do with those earlier discussions then it could be part of a totally different system, but the concern of a generic "manager" rather than a specific purpose still remains.
0

Share this post


Link to post
Share on other sites

Well as of right now, Lua will refer to an object via it's name, but before Lua performs a required task, it'll get the ID of the name and use when it operates on the C++ code.

 

The only problem is that the functions that Lua calls at times require the GUIDLookup. All functions that Lua calls must be static, which means I either have to pass the GUIDLookup via Lua's stack or have the GUIDLookup static.

Edited by Joshhua5
0

Share this post


Link to post
Share on other sites
There is no such thing as a "static class". You can make member functions static but these will only be able to access static member variables. To make the "whole" class accessible from anywhere in the program use the singleton pattern http://en.wikipedia.org/wiki/Singleton_pattern : You add one static method only to return the instance.

Use this sparingly! Too many of these is a sign of a bad class structure really ( as is the use of the word "manager" in class-names ;) though there are always exceptions if you have a valid reason!
0

Share this post


Link to post
Share on other sites

I'm trying to work on my class structure and naming, what's why I keep refactoring my project :P

 

What would you call my example up above? because it doesn't fit into a Singleton.

0

Share this post


Link to post
Share on other sites
I've heard the term Monostate used for similar things.

Personally I don't like singleton or monostate implementations much. If you have some code that needs GUID mapping lookup, just build a lightweight mapping wrapper that you can use as a component (i.e. member variable) of any object that needs that lookup mapping.


For your Lua code, I'd suggest setting up the mapping in your Lua context wrapper (assuming you did it right and you have one!) and then just use a thunk (standalone function that can find your Lua data, including the GUID mappings, and do whatever). Edited by ApochPiQ
2

Share this post


Link to post
Share on other sites

I'm trying to work on my class structure and naming, what's why I keep refactoring my project :P
 
What would you call my example up above? because it doesn't fit into a Singleton.


I see no reason why this class would not work as a singleton. In fact, having the word manager in the name almost indicates it is a singleton, which is why it makes people sense there's some problem as you should have as little as possible of these.
0

Share this post


Link to post
Share on other sites

It was just according to the Singleton pattern there's a static function that returns a instance of the class, which is then called upon.

Instead of mine which is calling static functions of the class.

0

Share this post


Link to post
Share on other sites

It was just according to the Singleton pattern there's a static function that returns a instance of the class, which is then called upon.
Instead of mine which is calling static functions of the class.

 
Yes, that's a choice. You can go both ways.
 
1) Make all functions + members static
2) Make only the "GetInstance" method static and use the instance it returns to call the non-static member functions. The point is that you can call "GetInstance()" on the class without an instance, thus no need to pass the instance around.
 
Here's your class definition changed to be a Singleton:
namespace Pro
{
	typedef unsigned int GUID; 

	class GUIDLookup 
	{   
	private:

		GUIDLookup(); // constructor is private
		static GUIDLookup* mInstance; // private instance


	public: 
					 
		static GUIDLookup* GetInstance()
		{
			if(!mInstance) mInstance = new GUIDLookup();
			return mInstance;
		}

		// all other non-static public methods
		// can be called after getting the instance with the method above
		GUID newGUID(const std::string& name);
		void releaseGUID(const std::string& name);
		void releaseGUID(GUID);
		GUID getGUIDFromName(const std::string& name);
		std::string getName(GUID _id);

	}; 

}
Edit: Oh and you'll have to intialize the mInstance variable to 0/NULL in a static initializer in the implementation file like so (and a better name would probably be msInstance, but it's too early in my timezone smile.png :
 
GUIDLookup* GUIDLookup::mInstance = 0;
Edited by h3xl3r
1

Share this post


Link to post
Share on other sites

I'm trying to work on my class structure and naming, what's why I keep refactoring my project tongue.png

In this regard only, several things stand out to me. I’ve outlined them here.
#1: Why is “class GUIDLookup{” one tab deeper than the line above it? The 2 lines are unrelated, and indentation is unquestionable in its value.
#2: Public, protected, then private, in that order. No exceptions.
#3: Search my link for “Class functions look much nicer when they are all indented the same amount.”
LSVOID LSE_CALL SetAllocator( CAllocator * _paAllocator );
LSVOID LSE_CALL Reset();
LSE_INLINE LSUINT32 LSE_CALL GetWidth() const;
LSE_INLINE LSUINT32 LSE_CALL GetHeight() const;
LSE_INLINE LSI_PIXEL_FORMAT LSE_CALL GetFormat() const;
LSE_INLINE const LSVOID * LSE_CALL GetBufferData() const;
LSVOID LSE_CALL                        SetAllocator( CAllocator * _paAllocator );
LSVOID LSE_CALL                        Reset();
LSE_INLINE LSUINT32 LSE_CALL           GetWidth() const;
LSE_INLINE LSUINT32 LSE_CALL           GetHeight() const;
LSE_INLINE LSI_PIXEL_FORMAT LSE_CALL   GetFormat() const;
LSE_INLINE const LSVOID * LSE_CALL     GetBufferData() const;

L. Spiro
0

Share this post


Link to post
Share on other sites
About structure for this specific problem:

In this case I would not make this class a singleton either. As there's usually some sort of "Game/Application" class already that's responsible for creating instances of all subsystems on start-up, and seeing as this particular class is naturally a singleton as there can be only ever one instance of it, that is a good place to just create and put getters for your "global tools" there.

So you'd make the master "Game" class create an instance of your GUIDManager once and have a GetGUIDManager getter to let all other subsystems have access to it via "Game".

Sure it could all be structured differently, but this way you avoid making a lot of singletons (bad!) and using the one you already "have to" make anyway. Edited by h3xl3r
0

Share this post


Link to post
Share on other sites
Lua has a couple of mechanisms that allow you to avoid this. I believe in this case you might want to use the registry, though you could also use upvalues too.

This allows you to have a global value in the Lua state, but not in your C++ code. This could be beneficial - for example, if you were writing a multiplayer game, you might want to run the server and client in the same process. Unlike solutions that involve C++ global values, such an implementation would allow two independent GUIDLookup instances to be created that cannot interfere with one another.

As ApochPiQ mentions, it is common to use a Lua wrapper library that will handle these kind of details for you. You should definitely consider doing this if you haven't already explored such options.
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