1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-22 15:06:45 +00:00

Expose registration of physics servers to GDExtension

This exposes PhysicsServer2DManager and PhysicsServer3DManager.
This commit is contained in:
Ricardo Buring
2022-09-06 15:35:33 +02:00
parent 019253512d
commit 532e378cd9
10 changed files with 222 additions and 67 deletions

View File

@@ -876,9 +876,7 @@ PhysicsServer2D::~PhysicsServer2D() {
singleton = nullptr;
}
Vector<PhysicsServer2DManager::ClassInfo> PhysicsServer2DManager::physics_2d_servers;
int PhysicsServer2DManager::default_server_id = -1;
int PhysicsServer2DManager::default_server_priority = -1;
PhysicsServer2DManager *PhysicsServer2DManager::singleton = nullptr;
const String PhysicsServer2DManager::setting_property_name(PNAME("physics/2d/physics_engine"));
void PhysicsServer2DManager::on_servers_changed() {
@@ -889,10 +887,19 @@ void PhysicsServer2DManager::on_servers_changed() {
ProjectSettings::get_singleton()->set_custom_property_info(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers));
}
void PhysicsServer2DManager::register_server(const String &p_name, CreatePhysicsServer2DCallback p_creat_callback) {
ERR_FAIL_COND(!p_creat_callback);
void PhysicsServer2DManager::_bind_methods() {
ClassDB::bind_method(D_METHOD("register_server", "name", "create_callback"), &PhysicsServer2DManager::register_server);
ClassDB::bind_method(D_METHOD("set_default_server", "name", "priority"), &PhysicsServer2DManager::set_default_server);
}
PhysicsServer2DManager *PhysicsServer2DManager::get_singleton() {
return singleton;
}
void PhysicsServer2DManager::register_server(const String &p_name, const Callable &p_create_callback) {
//ERR_FAIL_COND(!p_create_callback.is_valid());
ERR_FAIL_COND(find_server_id(p_name) != -1);
physics_2d_servers.push_back(ClassInfo(p_name, p_creat_callback));
physics_2d_servers.push_back(ClassInfo(p_name, p_create_callback));
on_servers_changed();
}
@@ -925,7 +932,11 @@ String PhysicsServer2DManager::get_server_name(int p_id) {
PhysicsServer2D *PhysicsServer2DManager::new_default_server() {
ERR_FAIL_COND_V(default_server_id == -1, nullptr);
return physics_2d_servers[default_server_id].create_callback();
Variant ret;
Callable::CallError ce;
physics_2d_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce);
ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
}
PhysicsServer2D *PhysicsServer2DManager::new_server(const String &p_name) {
@@ -933,6 +944,18 @@ PhysicsServer2D *PhysicsServer2DManager::new_server(const String &p_name) {
if (id == -1) {
return nullptr;
} else {
return physics_2d_servers[id].create_callback();
Variant ret;
Callable::CallError ce;
physics_2d_servers[id].create_callback.callp(nullptr, 0, ret, ce);
ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
}
}
PhysicsServer2DManager::PhysicsServer2DManager() {
singleton = this;
}
PhysicsServer2DManager::~PhysicsServer2DManager() {
singleton = nullptr;
}