Godot 项目初始化:C++ GDExtension 骨架
这个提交包含在:
+22
@@ -0,0 +1,22 @@
|
||||
#include "example.h"
|
||||
|
||||
#include <godot_cpp/core/class_db.hpp>
|
||||
#include <godot_cpp/core/defs.hpp>
|
||||
#include <godot_cpp/godot.hpp>
|
||||
|
||||
using namespace godot;
|
||||
|
||||
void MyMath::_bind_methods() {
|
||||
// 把 C++ 方法注册给 GDScript 调用
|
||||
ClassDB::bind_method(D_METHOD("add", "a", "b"), &MyMath::add);
|
||||
}
|
||||
|
||||
MyMath::MyMath() {
|
||||
}
|
||||
|
||||
MyMath::~MyMath() {
|
||||
}
|
||||
|
||||
int MyMath::add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
#ifndef MARRY_EXAMPLE_H
|
||||
#define MARRY_EXAMPLE_H
|
||||
|
||||
#include <godot_cpp/classes/ref_counted.hpp>
|
||||
|
||||
namespace godot {
|
||||
|
||||
// 示例 C++ 类:在 GDScript 里可以直接用 MyMath.new() 创建
|
||||
// 之后把你的 C++ 逻辑类都加在这里
|
||||
class MyMath : public RefCounted {
|
||||
GDCLASS(MyMath, RefCounted)
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
MyMath();
|
||||
~MyMath();
|
||||
|
||||
// 示例方法:加法(GDScript 调用: m.add(3, 4) -> 7)
|
||||
int add(int a, int b);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // MARRY_EXAMPLE_H
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "register_types.h"
|
||||
|
||||
#include "example.h"
|
||||
|
||||
#include <godot_cpp/core/class_db.hpp>
|
||||
#include <godot_cpp/core/defs.hpp>
|
||||
#include <godot_cpp/godot.hpp>
|
||||
|
||||
using namespace godot;
|
||||
|
||||
void initialize_marry_module(ModuleInitializationLevel p_level) {
|
||||
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||
return;
|
||||
}
|
||||
// 在这里注册所有 C++ 类
|
||||
GDREGISTER_CLASS(MyMath);
|
||||
}
|
||||
|
||||
void uninitialize_marry_module(ModuleInitializationLevel p_level) {
|
||||
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||
return;
|
||||
}
|
||||
// godot-cpp 在库卸载时自动清理类,无需手动注销
|
||||
}
|
||||
|
||||
// Godot 加载扩展的入口(dll 导出符号)
|
||||
extern "C" {
|
||||
// Initialization.
|
||||
GDExtensionBool GDE_EXPORT marry_library_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
|
||||
godot::GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization);
|
||||
|
||||
init_obj.register_initializer(initialize_marry_module);
|
||||
init_obj.register_terminator(uninitialize_marry_module);
|
||||
init_obj.set_minimum_library_initialization_level(MODULE_INITIALIZATION_LEVEL_SCENE);
|
||||
|
||||
return init_obj.init();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef MARRY_REGISTER_TYPES_H
|
||||
#define MARRY_REGISTER_TYPES_H
|
||||
|
||||
#include <godot_cpp/core/class_db.hpp>
|
||||
|
||||
using namespace godot;
|
||||
|
||||
void initialize_marry_module(ModuleInitializationLevel p_level);
|
||||
void uninitialize_marry_module(ModuleInitializationLevel p_level);
|
||||
|
||||
#endif // MARRY_REGISTER_TYPES_H
|
||||
在新工单中引用
屏蔽一个用户