Skip to content

Commit fdcbf5d

Browse files
committed
Update for GDExtension interface changes in Godot 4.5
1 parent 5cbffab commit fdcbf5d

File tree

6 files changed

+68
-15
lines changed

6 files changed

+68
-15
lines changed

binding_generator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,6 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
911911
result.append("\tconst Variant &operator[](int64_t p_index) const;")
912912
result.append("\tVariant &operator[](int64_t p_index);")
913913
result.append("\tvoid set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);")
914-
result.append("\tvoid _ref(const Array &p_from) const;")
915914
result.append("""
916915
struct Iterator {
917916
_FORCE_INLINE_ Variant &operator*() const;

gdextension/gdextension_interface.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,24 @@ typedef struct {
805805
const char *string; // (e.g. "Godot v3.1.4.stable.official.mono")
806806
} GDExtensionGodotVersion2;
807807

808+
/* Called when starting the main loop. */
809+
typedef void (*GDExtensionMainLoopStartupCallback)();
810+
811+
/* Called when shutting down the main loop. */
812+
typedef void (*GDExtensionMainLoopShutdownCallback)();
813+
814+
/* Called for every frame iteration of the main loop. */
815+
typedef void (*GDExtensionMainLoopFrameCallback)();
816+
817+
typedef struct {
818+
// Will be called after Godot is started and is fully initialized.
819+
GDExtensionMainLoopStartupCallback startup_func;
820+
// Will be called before Godot is shutdown when it is still fully initialized.
821+
GDExtensionMainLoopShutdownCallback shutdown_func;
822+
// Will be called for each process frame. This will run after all `_process()` methods on Node, and before `ScriptServer::frame()`.
823+
GDExtensionMainLoopFrameCallback frame_func;
824+
} GDExtensionMainLoopCallbacks;
825+
808826
/**
809827
* @name get_godot_version
810828
* @since 4.1
@@ -2386,6 +2404,7 @@ typedef GDExtensionVariantPtr (*GDExtensionInterfaceArrayOperatorIndexConst)(GDE
23862404
/**
23872405
* @name array_ref
23882406
* @since 4.1
2407+
* @deprecated in Godot 4.5. use `Array::operator=` instead.
23892408
*
23902409
* Sets an Array to be a reference to another Array object.
23912410
*
@@ -3131,6 +3150,17 @@ typedef void (*GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen)(const
31313150
*/
31323151
typedef void (*GDExtensionInterfaceEditorRegisterGetClassesUsedCallback)(GDExtensionClassLibraryPtr p_library, GDExtensionEditorGetClassesUsedCallback p_callback);
31333152

3153+
/**
3154+
* @name register_main_loop_callbacks
3155+
* @since 4.5
3156+
*
3157+
* Registers callbacks to be called at different phases of the main loop.
3158+
*
3159+
* @param p_library A pointer the library received by the GDExtension's entry point function.
3160+
* @param p_callback A pointer to the structure that contains the callbacks.
3161+
*/
3162+
typedef void (*GDExtensionInterfaceRegisterMainLoopCallbacks)(GDExtensionClassLibraryPtr p_library, const GDExtensionMainLoopCallbacks *p_callbacks);
3163+
31343164
#ifdef __cplusplus
31353165
}
31363166
#endif

include/godot_cpp/godot.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ extern "C" GDExtensionInterfaceGetProcAddress gdextension_interface_get_proc_add
4040
extern "C" GDExtensionClassLibraryPtr library;
4141
extern "C" void *token;
4242

43-
extern "C" GDExtensionGodotVersion godot_version;
43+
extern "C" GDExtensionGodotVersion2 godot_version;
4444

4545
// All of the GDExtension interface functions.
46-
extern "C" GDExtensionInterfaceGetGodotVersion gdextension_interface_get_godot_version;
46+
extern "C" GDExtensionInterfaceGetGodotVersion2 gdextension_interface_get_godot_version2;
4747
extern "C" GDExtensionInterfaceMemAlloc gdextension_interface_mem_alloc;
4848
extern "C" GDExtensionInterfaceMemRealloc gdextension_interface_mem_realloc;
4949
extern "C" GDExtensionInterfaceMemFree gdextension_interface_mem_free;
@@ -204,6 +204,7 @@ extern "C" GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8Chars gdextension_inter
204204
extern "C" GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen gdextension_interface_editor_help_load_xml_from_utf8_chars_and_len;
205205
extern "C" GDExtensionInterfaceImagePtrw gdextension_interface_image_ptrw;
206206
extern "C" GDExtensionInterfaceImagePtr gdextension_interface_image_ptr;
207+
extern "C" GDExtensionInterfaceRegisterMainLoopCallbacks gdextension_interface_register_main_loop_callbacks;
207208

208209
class DocDataRegistration {
209210
public:
@@ -228,6 +229,11 @@ class GDExtensionBinding {
228229
GDExtensionInitializationLevel minimum_initialization_level = GDEXTENSION_INITIALIZATION_CORE;
229230
Callback init_callback = nullptr;
230231
Callback terminate_callback = nullptr;
232+
GDExtensionMainLoopCallbacks main_loop_callbacks = {};
233+
234+
inline bool has_main_loop_callbacks() const {
235+
return main_loop_callbacks.frame_func || main_loop_callbacks.startup_func || main_loop_callbacks.shutdown_func;
236+
}
231237
};
232238

233239
class InitDataList {
@@ -262,6 +268,10 @@ class GDExtensionBinding {
262268
void register_terminator(Callback p_init) const;
263269
void set_minimum_library_initialization_level(ModuleInitializationLevel p_level) const;
264270

271+
void register_startup_callback(GDExtensionMainLoopStartupCallback p_callback) const;
272+
void register_frame_callback(GDExtensionMainLoopFrameCallback p_callback) const;
273+
void register_shutdown_callback(GDExtensionMainLoopShutdownCallback p_callback) const;
274+
265275
GDExtensionBool init() const;
266276
};
267277
};

include/godot_cpp/variant/typed_array.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ class TypedArray : public Array {
4040
public:
4141
_FORCE_INLINE_ void operator=(const Array &p_array) {
4242
ERR_FAIL_COND_MSG(!is_same_typed(p_array), "Cannot assign an array with a different element type.");
43-
_ref(p_array);
43+
Array::operator=(p_array);
4444
}
4545
_FORCE_INLINE_ TypedArray(const Variant &p_variant) :
4646
TypedArray(Array(p_variant)) {
4747
}
4848
_FORCE_INLINE_ TypedArray(const Array &p_array) {
4949
set_typed(Variant::OBJECT, T::get_class_static(), Variant());
5050
if (is_same_typed(p_array)) {
51-
_ref(p_array);
51+
Array::operator=(p_array);
5252
} else {
5353
assign(p_array);
5454
}
@@ -68,7 +68,7 @@ class TypedArray : public Array {
6868
public: \
6969
_FORCE_INLINE_ void operator=(const Array &p_array) { \
7070
ERR_FAIL_COND_MSG(!is_same_typed(p_array), "Cannot assign an array with a different element type."); \
71-
_ref(p_array); \
71+
Array::operator=(p_array); \
7272
} \
7373
_FORCE_INLINE_ TypedArray(std::initializer_list<Variant> p_init) : \
7474
Array(Array(p_init), m_variant_type, StringName(), Variant()) { \
@@ -79,7 +79,7 @@ class TypedArray : public Array {
7979
_FORCE_INLINE_ TypedArray(const Array &p_array) { \
8080
set_typed(m_variant_type, StringName(), Variant()); \
8181
if (is_same_typed(p_array)) { \
82-
_ref(p_array); \
82+
Array::operator=(p_array); \
8383
} else { \
8484
assign(p_array); \
8585
} \

src/godot.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ GDExtensionInterfaceGetProcAddress gdextension_interface_get_proc_address = null
4747
GDExtensionClassLibraryPtr library = nullptr;
4848
void *token = nullptr;
4949

50-
GDExtensionGodotVersion godot_version = { 0, 0, 0, nullptr };
50+
GDExtensionGodotVersion2 godot_version = {};
5151

5252
// All of the GDExtension interface functions.
53-
GDExtensionInterfaceGetGodotVersion gdextension_interface_get_godot_version = nullptr;
53+
GDExtensionInterfaceGetGodotVersion2 gdextension_interface_get_godot_version2 = nullptr;
5454
GDExtensionInterfaceMemAlloc gdextension_interface_mem_alloc = nullptr;
5555
GDExtensionInterfaceMemRealloc gdextension_interface_mem_realloc = nullptr;
5656
GDExtensionInterfaceMemFree gdextension_interface_mem_free = nullptr;
@@ -211,6 +211,7 @@ GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8Chars gdextension_interface_editor
211211
GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen gdextension_interface_editor_help_load_xml_from_utf8_chars_and_len = nullptr;
212212
GDExtensionInterfaceImagePtrw gdextension_interface_image_ptrw = nullptr;
213213
GDExtensionInterfaceImagePtr gdextension_interface_image_ptr = nullptr;
214+
GDExtensionInterfaceRegisterMainLoopCallbacks gdextension_interface_register_main_loop_callbacks = nullptr;
214215

215216
struct DocData {
216217
const char *hash = nullptr;
@@ -308,8 +309,8 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
308309
internal::library = p_library;
309310
internal::token = p_library;
310311

311-
LOAD_PROC_ADDRESS(get_godot_version, GDExtensionInterfaceGetGodotVersion);
312-
internal::gdextension_interface_get_godot_version(&internal::godot_version);
312+
LOAD_PROC_ADDRESS(get_godot_version2, GDExtensionInterfaceGetGodotVersion2);
313+
internal::gdextension_interface_get_godot_version2(&internal::godot_version);
313314

314315
// Check that godot-cpp was compiled using an extension_api.json older or at the
315316
// same version as the Godot that is loading it.
@@ -496,6 +497,7 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
496497
LOAD_PROC_ADDRESS(editor_help_load_xml_from_utf8_chars_and_len, GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen);
497498
LOAD_PROC_ADDRESS(image_ptrw, GDExtensionInterfaceImagePtrw);
498499
LOAD_PROC_ADDRESS(image_ptr, GDExtensionInterfaceImagePtr);
500+
LOAD_PROC_ADDRESS(register_main_loop_callbacks, GDExtensionInterfaceRegisterMainLoopCallbacks);
499501

500502
r_initialization->initialize = initialize_level;
501503
r_initialization->deinitialize = deinitialize_level;
@@ -526,6 +528,10 @@ void GDExtensionBinding::initialize_level(void *p_userdata, GDExtensionInitializ
526528
}
527529
level_initialized[p_level]++;
528530

531+
if ((ModuleInitializationLevel)p_level == MODULE_INITIALIZATION_LEVEL_CORE && init_data && init_data->has_main_loop_callbacks()) {
532+
internal::gdextension_interface_register_main_loop_callbacks(internal::library, &init_data->main_loop_callbacks);
533+
}
534+
529535
if ((ModuleInitializationLevel)p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
530536
internal::gdextension_interface_editor_register_get_classes_used_callback(internal::library, &ClassDB::_editor_get_classes_used_callback);
531537

@@ -596,6 +602,18 @@ void GDExtensionBinding::InitObject::set_minimum_library_initialization_level(Mo
596602
init_data->minimum_initialization_level = static_cast<GDExtensionInitializationLevel>(p_level);
597603
}
598604

605+
void GDExtensionBinding::InitObject::register_startup_callback(GDExtensionMainLoopStartupCallback p_callback) const {
606+
init_data->main_loop_callbacks.startup_func = p_callback;
607+
}
608+
609+
void GDExtensionBinding::InitObject::register_frame_callback(GDExtensionMainLoopFrameCallback p_callback) const {
610+
init_data->main_loop_callbacks.frame_func = p_callback;
611+
}
612+
613+
void GDExtensionBinding::InitObject::register_shutdown_callback(GDExtensionMainLoopShutdownCallback p_callback) const {
614+
init_data->main_loop_callbacks.shutdown_func = p_callback;
615+
}
616+
599617
GDExtensionBool GDExtensionBinding::InitObject::init() const {
600618
return GDExtensionBinding::init(get_proc_address, library, init_data, initialization);
601619
}

src/variant/packed_arrays.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ void Array::set_typed(uint32_t p_type, const StringName &p_class_name, const Var
232232
internal::gdextension_interface_array_set_typed((GDExtensionTypePtr *)this, (GDExtensionVariantType)p_type, (GDExtensionConstStringNamePtr)&p_class_name, (GDExtensionConstVariantPtr)&p_script);
233233
}
234234

235-
void Array::_ref(const Array &p_from) const {
236-
internal::gdextension_interface_array_ref((GDExtensionTypePtr *)this, (GDExtensionConstTypePtr *)&p_from);
237-
}
238-
239235
const Variant *Array::ptr() const {
240236
return (const Variant *)internal::gdextension_interface_array_operator_index_const((GDExtensionTypePtr *)this, 0);
241237
}

0 commit comments

Comments
 (0)