Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Charlie Micou |
I have some structured data within a C++ module.
I’d like to be able to access that data through its structural hierarchy from GDScript.
Example:
class Foo : Object {
int a;
}
class Bar : Object {
Foo foo;
}
class SomeNode : Node {
Bar bar;
}
Bar SomeNode::get_bar() {
return bar;
}
void SomeNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_bar"), &SomeNode::get_bar);
}
I’d like to be able to do something along the lines of some_node_instance.get_bar().get_foo().get_a()
.
This seems like it’s probably a very common thing to want to do. What is the ‘correct’/best practices/idiomatic way of approaching this?
Some things that come to mind:
- Make Bar a Node instead of an Object and make an instance of it child of SomeNode, make Foo a Node instead of an Object and make it a child of an instance of Bar
- Express a way to serialize the Foo and Bar Objects so that they are valid value return types (though I’m not sure how to approach this)
Follow up question for an edge-case:
I’m expecting that the answer to my question will go along the lines of “make Bar a Node instead of an Object and make it a child of SomeNode” so that the structure is already cleanly expressed.
Unfortunately, the exact situation I’m in is that my SomeNode has an internal data representation based on a third party library. My Bar class is a wrapper around the internal data that allows accessing it in terms of Godot’s primitives. I’d prefer to not keep two representations of the same data around at all times, so would like to be able to return the structured data in a lightweight way without holding on to a child node for the sake of conversion.