• 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

I have two of the same function and variable value. Which I want to remove atleast one of them

3 posts in this topic

Hi, 

Iam trying to remove one of the two function and the corresponding variable, because they basically does the same thing. Both the variable holds a "unique ID". 

Pseudo code

class Base
{
    int typeID;
    virtual int getType()//Edit: forgot virtual
    {
         return typeID;
    }
}
template<class>
class IBase : public IBase<Base>
{
    static unsigned int Type()
    {
	return typeID;
    }
protected:
    static int typeID;//Edit: forgot protected
}

class Render : public IBase<Render> 
{

}
class GameObject
{
    std::map<int, Base*> map;
    template<class T_S>
    T_S* GetComponentT()
    {
        std::map<std::string, BaseComponentTest*>::iterator it;
        int a = IBaseComponent<T_S>::Type()

        it = map.find(a);

        if (it != map.end())
            return (T_S*)it->second;

        return NULL;
    }
}

I have two diffrent scenario where I had to "split" into two function.

The first one is when I only have a Base pointer

void GameObject::AddComponent(Base* componentToAttach)
{
	if (!componentMapT.count(componentToAttach->getType()) > 0)//I want to get the unique ID here
	{
		componentToAttach->attachOn(this);
		componentMapT.insert(componentMapT.end(),
			std::make_pair
			(
			componentToAttach->getType(),//I want to get the Unique ID here
			componentToAttach
			)
			);
	}

}

Here I want to add the pointer into a map, with the key bieng the ID, so I will need to have some kind of virtual function (so far as I know of) which returns the correctly id.

 

and in my second scenario I want to get the pointer out of the map

template<class T_S>
T_S* GetComponentT()
{
	std::map<std::string, Base*>::iterator it;
	int a = IBase<T_S>::Type();

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

and in a "third" scenario which is the combined scenario.

template<class Archive>
void save(Archive& archive, const GameObject& go)
{
	//if (go != NULL)
	{
		archive(versionName);
		archive(go.name);
		archive(go.componentMap.size());
		for (std::map<int, Base*>::const_iterator it = go.map.begin(); it != go.map.end(); ++it)
		{	
			archive(it->first);
			if(it->first == IBaseComponent<RenderComponent>::Type())
			{
				IBaseComponent<RenderComponent>* const rcp = (RenderComponent*)it->second;
				archive(*rcp);
			}
		}
	}
}

Is there a way to remove the base.getType? I tried to make the ID in base static, but than figured out that its is shared across all derivied classes which gave me wrong ID. I tried to do a few diffrent way but cannot bypass the problem. 

 

For the third scenario I tried to have a template<class Archive, class T> but cereal start giving me error messages, so I do not think that will be possible, but if it were possible I could remake scenario 1 into a template function similar to scenario 2(But that may give me problem in the future, but thats for the future me to worry laugh.png).

 

So I guess that I only got a few choices to choice from:

1. Keep both the function and have dublicated variables

2. use something else beside cereal to output into binary

3. Some kind of magic way I do not know of

Edited by Conny14156
0

Share this post


Link to post
Share on other sites

I'm not sure if you want the ID to be the same for all instances of the same class or a unique one for each instance. In Base typeID isn't static but in IBase it is.

Anyway, this is the code that I use to get unique IDs for each class (identical for each instance of the same class) in my event management system. Perhaps you can use it for your purpose.
 

class IEventBase
{
public:
	virtual size_t GetType() const = 0;
};

template<class T>
class IEvent : public IEventBase
public:
	size_t GetType() const override final;
	static const size_t Type;
};

template<class T>
const size_t IEvent<T>::Type = typeid(IEvent<T>).hash_code();

class KeyHitEvent final : public IEvent<KeyHitEvent>
{
};

In this case, IEventBase is the class that need to be used to hold variables of any event type.
To get the unique ID of a specific class you access the member variable (e.g. KeyHitEvent::Type), to get the unique class ID of an IEventBase* you use the virtual member function (e.g. ptr->GetType()).

Edited by Jan2go
0

Share this post


Link to post
Share on other sites

I'm not sure if you want the ID to be the same for all instances of the same class or a unique one for each instance. In Base typeID isn't static but in IBase it is.

Anyway, this is the code that I use to get unique IDs for each class (identical for each instance of the same class) in my event management system. Perhaps you can use it for your purpose.
 


In this case, IEventBase is the class that need to be used to hold variables of any event type.
To get the unique ID of a specific class you access the member variable (e.g. KeyHitEvent::Type), to get the unique class ID of an IEventBase* you use the virtual member function (e.g. ptr->GetType()).

 

I forgot to mention it, but I want the ID to be unique across all instances of the class. and yeah your way works wonder, if I just makes my ID public instead of protected, and just have my base be pure virtual. 

Man I feel so stupid, If I just made my id public instead of protected I coulda override the base virtual funtion, I tried to have a static override but the compiler didnt like that. Man I feel so stupid I been at this for the past days. 

0

Share this post


Link to post
Share on other sites

if I just makes my ID public instead of protected


Now that you mention it, I realized that I removed the public together with ID-unrelated functions from the code I posted. Obviously it isn't of much use when everything is private. biggrin.png
Fixed it for future reference.
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