Skip to content

Custom type converters for method binding and variant casting#1953

Open
YakoYakoYokuYoku wants to merge 1 commit intogodotengine:masterfrom
YakoYakoYokuYoku:converters
Open

Custom type converters for method binding and variant casting#1953
YakoYakoYokuYoku wants to merge 1 commit intogodotengine:masterfrom
YakoYakoYokuYoku:converters

Conversation

@YakoYakoYokuYoku
Copy link

With this API users can bind methods which have types in their signature that are not supported by default in the Godot C++ bindings. Closes godotengine/godot-proposals#14507 proposal.

Sample usage.

// Extracted from one of the headers of a library.

struct Vector {
  float x;
  float y;
  float z;
};

// In another source file. Define these converters so that `Vector` can be used in method signatures.

_FORCE_INLINE_ godot::Vector3 vector_to_vector3(const Vector &v) {
  return godot::Vector3(v.x, v.y, v.z);
}

_FORCE_INLINE_ Vector vector_from_vector3(const godot::Vector3 &v) {
  return Vector(v.x, v.y, v.z);
}

// This registers the converters so that they can be used in method bindings, variants, etc.
// We pass both `vector_to_vector3` and `vector_from_vector3` here.

GD_REGISTER_CONVERSION(Vector, godot::Vector3, vector_to_vector3, vector_from_vector3);

// In a source file which has a wrapper for a type from a library, in this case with the `Entity` class.
// Notice that we use the library types and that the converters are called elsewhere.

Vector Entity::get_position() const {
  return wrapped.position();
}

void Entity::set_position(const Vector &v) {
  wrapped.position() = v;
}

void Entity::_bind_methods() {
  godot::ClassDB::bind_method(godot::D_METHOD("get_position"), &Entity::get_position);
  godot::ClassDB::bind_method(godot::D_METHOD("set_position", "position"), &Entity::set_position);
}

@YakoYakoYokuYoku YakoYakoYokuYoku requested a review from a team as a code owner March 21, 2026 14:51
Signed-off-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom type converters for GDExtension

1 participant