• Content count

  • Joined

  • Last visited

Community Reputation

279 Neutral

About Conny14156

  • Rank
  1. Class with static compiler const member variable?

    How would you recommend I would do it as virtual function? mind giving a example? I need to know what type the component is, cause depending on the type I need to instantiate another component. Is it possible to somehow upcast with just the base class access?      PS not really a problem seeing that IF does what I need, but I just feel like there would be a more proper way to do it. (But I guess dont fix what not broken argument works here)
  2. Class with static compiler const member variable?

    The moment I noticed switch didnt work, I just switched to if else statments. But I just cannot rid the problem from my mind for some reason.
  3. 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
  4. 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. 
  5. 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(; archive(go.componentMap.size()); for (std::map<int, Base*>::const_iterator it =; it !=; ++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 ).   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
  6. 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?
  7. So many complex word and method! But will try to improve it from all the feedback! I think I will go towards Juliean way with the CRTP method, it seems kind of fun. if I make it to work, I will try to improve it with SeanMiddleditch suggestion.   And I will remake my std::map to a std::unordered_map as Servant of the Lord suggested. if I got all the above to work 
  8. 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. 
  9. Thanks for all the help guys,  I think I will go with the binary and seeing that this is a school project(and hobby), we not thinking about porting for the moment and will just be for PC. so I want to try at this binary save "challenge". beside cereal is there any other lib I should look at?(Not that am complaining just wanted to know what options I have)
  10. 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).     
  11. Help to understand my own misstake?

    i feel stupid and thanks >.<
  12. Hi, Today/Yesterday I been having a few problem in my code which I manged to "solve", but I was wondering why my original approach didnt work?  Basically I have this std::map with a vector inside it like std::map<int, std::vector<GameObject*>> testMap; than i started to insert stuffs like so mapTest.insert(std::pair<int, std::vector<GameObject*>>(renderID,{gameObjectTest})); //first try mapTest.insert(mapTest.end(),std::pair<int, std::vector<GameObject*>>(renderID,{gameObjectTest})); //second try I also tried to have a tempVector where I push_back the gameObjectTest in it and just had tempVector instead of { gameObjectTest }    At this point I got no syntax error or the likes, and when I ran the code it works great. But when I tried to access the gameobject via; I get a std::out_of_range error, but instead of using .at I tried to use std::map<int, std::vector<GameObject*>>::iterator it = mapTest.begin(); it->; for now it works and I get no std::out_of_range error. I was wondering how come .at doesnt work but .begin (aka iterator) works?
  13. Why does this work?(Class prototype?)

    That what I was actually using it for, in one of my classes I had a parameter class pointer, and now I know its a "legit" thing I think I have other places where I can do similar things instead of the way I have done it untill now!
  14. Hi, I was wondering if anyone could tell me if I should do it this way and why it works   I have two classes, class A and class B in two differently headers.   in class A header I have #include "b.h" class a { b testVar; } and in class B Header I have  #include "a.h" class b { a testVar; } if I do it this way, the compiler gonna complains about alot of stuffs >.<, but instead of including I somehow(accidently) got class a; class b { a testVar; } and I was wondering how this worked? is it like extern/global variables? or is it more of a function prototype, but how would class a inside b.h, knows that it got defined inside a.h? and how come those two doesnt clash in name if it doesnt work as a prototype?