Jump to content

  • Log In with Google      Sign In   
  • Create Account

Conny14156

Member Since 27 Nov 2012
Offline Last Active Today, 10:10 AM

Topics I've Started

Class with static compiler const member variable?

Yesterday, 10:12 PM

Hi,

I was wondering if it was possible to get a "compiler constant" inside a class? 

What I meant with [Compiler constant] is the kind of constant that works with case statement or #defined or likes Enums and not just a Read-Only.

I have a base class that looks like

template<typename>
class IBaseComponent :public BaseComponent
{
public:
	const unsigned int getType()
	{
		return typeID;
	}
	static const unsigned int typeID;
	
};

I have a Derived class

class TransformComponent: public IBaseComponent<TransformComponent>
{
public:

	TransformComponent();
	//Relative to the Window
	Vector2<int> position;

	//Pixel count and not Multiply
	Vector2<int> size;

	//Yet TBD
	Vector2<int> rotation;
};
.cpp
const unsigned int IBaseComponent<TransformComponent>::typeID = 1000;//<-- The nr doesnt matter

The reason why I "need" a compiler const is that I need it for a switch statement

 

 

for(std::map<int,BaseComponent*>::const_iterator it = t->GetComponents()->begin(); it != t->GetComponents()->end(); it++)
{ 
             switch(it->second->getType())
             {
 
                       {
                               break;
                       }
             }
}

 

 

Like so, but case statement seems to require constant that is known when compiling/Linking. A alternative method to switch is that I can just use

for(std::map<int,BaseComponent*>::const_iterator it = t->GetComponents()->begin(); it != t->GetComponents()->end(); it++)
{	
	if(it->second->getType() == IBaseComponent<TransformComponent>::typeID)
	{	
		//Do stuffs
	}
}

But for my curiosity, is the a way to make it work? 

enum test
{
    a
};
const unsigned int IBaseComponent<TransformComponent>::typeID = test::a; <--- tried to do this but to no avail 

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

04 July 2014 - 02:43 PM

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


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

20 June 2014 - 12:07 PM

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?


How to dereference to the derivied class?/How bad is it of my currently way of doing it?

28 May 2014 - 07:56 AM

Hi,

I have a map like this were I store deriviedclass pointers inside a basepointer

std::map<std::string,BaseComponent*> componentMap;

I was wondering if there were any "smart" way to dereference it to the derivied class?

my current way is

template<class T_S>
T_S* GetComponent()
{
	std::string test = typeid(T_S).name();
	std::map<std::string, BaseComponent*>::iterator it;
	for (int i = 0; i < 6; i++)
	{
		test.erase(test.begin());
	}
	it = componentMap.find(test);
	if (it != componentMap.end())
	{
		T_S* ptTComponent;
		ptTComponent = (T_S*)it->second;

		return ptTComponent;
	}
	else
		return NULL;
}

I use typeid to get the class "name", and the string is ctrl + c and ctrl + v, but I want some more "safe?" way to get the class name. 


how to go to be considered when making save/load functionality?

09 April 2014 - 07:44 AM

Hi,

I done some googling and reading but am still unsure how I should make my save "file", I was thinking about making a text file for readability, but what should I "save" into the file so I don't end up with a thousand differently save files.

 

Me and my friend is trying to make a RPG-ish game (2D), so we got stuffs like health and inventory etc. How am I suppose to save my "inventory" into the file? for health I thougth about just saving it in plain number format but what am I suppose to do with the inventory? should I check each slots to see what item it is and save the said item into a a "Inventory" folder with seperated files as "Slot 01".txt with infomation about the item? I was thinking about just saving itemID but we decided to try to have "Randomized" items where they have a chance to have different component that grant stuffs(ex. Double jump). 

 

 


PARTNERS