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

How to add a template class object into a map? and how stupid is my way?

1 post in this topic

Hi,

This is a followup question on this topic. I didn't want to ressurect a old thread, so I made new one. I was wondering now that I have a interface template class that my RenderComponent class inherit from

template<typename>
class IBaseComponent :public BaseComponentTest
{
public:
	static unsigned int Type(void)
	{
		static const int type = GenerateID();	
		return type;
	}
protected:
};

and and this is how my testcomponent is

class RenderComponent : public IBaseComponent<RenderComponent>	
{
public:
	sf::Sprite sprite;
	int renderlayer = 1;
private:
	const std::string componentName = "RenderComponent";
	
	
	template < class Archive>//, GameObject& go>
	friend void save(Archive& archive, const RenderComponent& go);
	template < class Archive>//, GameObject& go>
	friend void load(Archive& archive, RenderComponent& go);
};

I got map of

std::Map<int,BaseComponentTest*>

and I have a function about like this

template<class T_S>
	T_S* GetComponentTest()
	{
		std::map<int, BaseComponentTest*>::iterator it;
		int a = IBaseComponent<T_S>::Type();
		it = componentMapT.find(a);

		if (it != componentMapT.end())
			return 	(T_S*)it->second;
		
			return NULL;
	}

P.S

is this better than my old one where I used typeid()?

 

I have a add function to that look about like this


void GameObject::AddComponent(BaseComponentTest* componentToAttach)
{
	//use map inbuild function to check if there is a
	if (!componentMapT.count(componentToAttach->GenerateID()) > 0)//<---- Not "Working" as intended, It works the way it suppose to but not what I want :<
	{
		componentToAttach->attachOn(this);
		componentMapT.insert(componentMapT.end(),
			std::make_pair
			(
			//IBaseComponent<componentToAttach>::Type(),//	->GenerateID(),
			componentToAttach
			)
			);
	}
}

If I were to do it this way, I will get the BaseComponentTest type id instead of the one I want, which would be whichever the derivied class its suppose to be. I want to check if the component is already inside the map. 

Should I do it this way?

template<class TS>
bool addComponent(BaseComponentTest* componentToAdd)
{
     if (!componentMapT.count(IBaseComponent<TS>::Type() > 0)
	{
             	componentToAttach->attachOn(this);
		componentMapT.insert(componentMapT.end(),
		std::make_pair
		(
			IBaseComponent<TS>::Type(),//	->GenerateID(),
			componentToAttach
		)
		);
        }
}

Or should I have a int inside my BaseComponentTest that the same as the 

static const int type = GenerateID();
public int IDTYPE = type;

but its public so I can acess it outside of the class?

Edited by Conny14156
0

Share this post


Link to post
Share on other sites

At the time of getting the type ID, if all you have is a pointer to BaseComponentTest, then you need your Type() function to be a virtual member function of both BaseComponentTest and IBaseComponent<T>.  The fact that it is a static function on IBaseComponent<T> (and I don't know how you defined it on BaseComponentTest) means that from a BaseComponentTest pointer, the program has no way of running the code for the specific component, and thus returning the correct ID.  Static functions are useful when you know the type in question at compile time.  In this case, that's clearly not the case.

 

It is easy to think that member functions are relevant only when you need to access data within an instance of the class, and it might seem at first glance that the type is independent of the instance.  It's ID is stored (reasonably) in a static variable, after all, not in a member variable that is a part of every single instance.  But what is less obvious is that the type of an instance is also stored as part of the instance, and is accessible through virtual member functions.  If you want to understand this at a deeper level, read up a bit on vtables.  Essentially, every class (that needs it, i.e., has virtual functions) stores a hidden member variable that points to additional information related to the type of that instance.  Without access to that hidden member variable through a virtual function, you have no way of accessing that information, and thus of finding out what type the instance is.

Edited by Andy Gainey
2

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