Jump to content
  • Advertisement
Sign in to follow this  
DarkZoulz

get type of object?

This topic is 4510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! Is there some way I can get the type of an object and then use it to create a new instance of the object? What i'm attempting to do is to implement a way for my component manager to create components of specified types. In essence, all my components have a string ID and I want to be able to supply this ID to a function that will then return a pointer to a newly create component of the type that is associated to the ID. I can just make a switch-case to see if the ID matches any string and then just hard-code all the creation of these objects. But is there any other, better way to do it? One idea I had was to make the components register themselves in the component manager, but in that case I need to be able to get the type of that object and associate it with it's string ID. Am I making any sense?

Share this post


Link to post
Share on other sites
Advertisement
What language are you writing in?


It sounds like you want an abstract factory. However, it might be worth describing the actual purpose of this part of your design - there's a reasonably good chance you can accomplish the same result with a cleaner and less brittle piece of code.

Share this post


Link to post
Share on other sites
I'm using C++.

What i'm trying to build is a component-based entity system for my game engine. Each entity contains a set of components that build up the actual game logic for that specific entity. Maybe you've heard of this approach before?

EDIT: The abstract factory sounds pretty similar to what i'm looking for. I think i'll read some more about them. Thanks for the tip! :)

[Edited by - DarkZoulz on June 15, 2006 4:13:53 PM]

Share this post


Link to post
Share on other sites
I've heard of that approach, yes. What I'm not clear on is why you need to create new instances of the same class as a given object?

Share this post


Link to post
Share on other sites
Sorry if i'm not being clear. I'm not that good at explaining things.

What i'm looking for is basicly a means to create instances of my component objects/classes without having to hard-code each one of my components into my createComponent() function.

ex.

IComponent *pResult = NULL;

switch(componentID)
{
case "ACTOR":
pResult = new Component_Actor;
break;

case "NETWORK":
pResult = new Component_Network;
break;
}

return pResult;



That's what my createComponent() function looks like now.

Metaclasses and reflection sounds good too. I'll have to read up on them also.

Share this post


Link to post
Share on other sites
You could store "prototype" instances somewhere, and use a virtual constructor idiom. But the system still needs a way to get at the prototypes.

Effectively, mapping a bit of text data (e.g. from a file) into an object type (and hence an instance of that object type) is a reflection task, whether you do it manually (e.g. building a std::map<std::string, IComponent*> to store your prototypes) or relying on built-in language stuff (C++ doesn't have any in the standard library, but Boost might be able to help you out).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!