#Generate unique ID for subclasses

1 messages · Page 1 of 1 (latest)

opaque nebula
#

So I have this struct ComponentBase:

struct ComponentBase {
  ComponentBase() = default;
};

Which is inherited by various other components. When you initialise an object, it may come with a couple of these subclassed components in a vector which each need to have a unique ID in the collection. I can't use typeid(obj) or typeid(obj).name() since pybind11 doesn't work with the first one and the strings are name scrambled with the second.

Its also pretty difficult to use a virtual function to do get_type() for the component since I have methods like this:

  template <typename T>
  inline auto get_component(const GameObjectID game_object_id) const -> std::shared_ptr<T> {
    return std::static_pointer_cast<T>(get_component(game_object_id, GET_COMPONENT_TYPE_HERE));
  }

  [[nodiscard]] auto get_component(GameObjectID game_object_id, TODO component_type) const -> std::shared_ptr<ComponentBase>;

Which can't be passed an instance of the component since I'm trying to get that component. What would be the best way to solve this?

wooden canopyBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

knotty badge
opaque nebula
#

Its not for each instance, its for each type of component so a unique ID for each class

knotty badge
#

that returns some enum value

opaque nebula
#
  template <typename T>
  inline auto get_component(const GameObjectID game_object_id) const -> std::shared_ptr<T> {
    return std::static_pointer_cast<T>(get_component(game_object_id, GET_COMPONENT_TYPE_HERE));
  }

How would I get the type here? It would need an instance

knotty badge
#

that's how

opaque nebula
#

I'm calling get_component to get the component and I cast it to T. get_component expects the component type

knotty badge
#
{
  static CompType getType()
  {
    return CompType::MAGIC_COMPONENT;
  }
}

?

opaque nebula
#

can that be overriden in the subclasses? for some reason when i tried earlier, it didn't work

knotty badge
#

you can redefine any static function in any class, inheritance is irrelevant

hard flame
#

@opaque nebula you could use std::type_index

#

There's also some tricks to get a unique random I'd for each class

hard flame
opaque nebula
#

I can't use std::type_index. I have some pybind11 bindings for this class and it can't convert std::type_index

hard flame
#

You could always get the hash from the type_index and hope for the best

opaque nebula
#

For get_component, its definition would be:

auto Registry::get_component(const GameObjectID game_object_id, const std::type_index &component_type);

And pybind11 can't convert std::type_index to Python

#

It would make this easier, but whatever. Static methods seem to work now, idk why they wern't working earlier though

hard flame
#

Do you need to be able to get that Id from the python side?

#

Or are you getting that Id from the CPP side and passing it to python?

opaque nebula
#

Its used on the C++ side, but I'm not sure how I would pass in a value from python which will let me get that component out

#

So you would think that passing type(obj) from python would work, but it can't be converted to std::type_index which is kinda annoying so I'm having to try this other approach

hard flame
#
opaque nebula
#

?

hard flame
opaque nebula
#

but would it allow for a python interface

hard flame
#

if you get the ID from the cpp side you should be able to pass it no problem

opaque nebula
#

its about passing a value from the python side

hard flame
#

but where are you creating it from?

opaque nebula
#

i was just using typeid() in the c++ code

hard flame
#

alright so what's the problem?

opaque nebula
#

how would i pass a value from python to c++ which is used to find a component stored

hard flame
#

do you pass the value from cpp to python and then back to cpp?

opaque nebula
#

i pass the type i want from python to cpp and cpp should pass the component back

hard flame
#

where/how do you get the type on the python side?

opaque nebula
#

thats what im trying to figure out, how would i know what to pass so c++ can read it? ive been using strings and since i know what their value is, its easy to pass the string to c++ and get the component

signal saffron
#

you will need some kind of "registry" on the C++ side that knows all the possible IDs anyway,
and a way to then instantiate the correct type of thing

opaque nebula
#

i may just stay with the static method idea for now. i can always change it later on