Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 27 Nov 2012
Offline Last Active Private

Posts I've Made

In Topic: Class with static compiler const member variable?

21 October 2014 - 08:32 AM

The virtual function would replace the `if`, not the logic you're using in the `if`.

You should never have to care about the specific type of a component and test for it. If you are (like in this case), that implies there's something wrong with your component architecture.

In other words, replace this:

	if(it->second->getType() == IBaseComponent<TransformComponent>::typeID)
		//Do stuffs
with this:
If you're in a context where you need a specific component (e.g., syncing physics back with transform data) then don't loop through all the components. Just grab the one you need:
// this code is still far more verbose than it needs to be, but it's a mechanical translation of your current code
  auto const trnsIt = t->GetComponents().find(IBaseComponent<TransformComponent>::typeID);
  if (trnsIt != t->GetComponents().end())
    //Do stuffs
And if you can, cache that component, since looking it up every time (especially with a `std::map`) is probably more inefficient than keeping a pointer around (but as always, measure to be sure).

Even better though in these kinds of cases would be more purpose-built data structures so you can e.g. just memcpy the data you need from one place to another with zero looping, map lookups, or inner function calls.


I will definitely try to improve other area of my codes with that.


But I may have been wording my question badly, what am trying to do is some kind of interface from my game to a "Editor" (winapi), so I has to loop all the existing component inside each Entity to see what component there is, which correspond to the correct "UIComponent"(Not sure what to call it) instantiation. 




I agree with Sean that it's better to grab the components you need instead of looping over them. I've never really programmed an entity component system and I just whipped this up in half an hour, but you might find it useful.

You code is almost like the way I has it, which sean and other from the forum helped me with not so long ago 

template<class T_S>
T_S* GetComponent()
	std::map<int,BaseComponent*>::iterator it;
	it = componentMap.find(IBaseComponent<T_S>::typeID);

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

I really should make it static_cast seeing that people have told me to do it, but I just havent gotten around to it yet. 

In Topic: Class with static compiler const member variable?

20 October 2014 - 05:01 AM

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? 




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)

In Topic: Class with static compiler const member variable?

19 October 2014 - 11:02 PM

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.

In Topic: I have two of the same function and variable value. Which I want to remove at...

04 July 2014 - 03:49 PM

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. 

In Topic: How to dereference to the derivied class?/How bad is it of my currently way o...

29 May 2014 - 11:39 AM


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