diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2020-01-03 11:32:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-03 11:32:01 +0100 |
commit | 9d3424f61ddd1e6bec78381aa4a0c6acec53d290 (patch) | |
tree | e083361258651b2cef3d51129df131a19c11669c /modules | |
parent | 529f710ec0540b9d3f61a6d75559f13af2f069d2 (diff) | |
parent | 3056c4bd5acc4b5eda71303bf349e0d4f94a89c8 (diff) |
Merge pull request #34688 from sheepandshepherd/gdnative_class_ptr
Expose is_class_ptr to GDNative for dynamic casts
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gdnative/gdnative/gdnative.cpp | 13 | ||||
-rw-r--r-- | modules/gdnative/gdnative_api.json | 15 | ||||
-rw-r--r-- | modules/gdnative/include/gdnative/gdnative.h | 4 |
3 files changed, 32 insertions, 0 deletions
diff --git a/modules/gdnative/gdnative/gdnative.cpp b/modules/gdnative/gdnative/gdnative.cpp index 174ef14ec2..6ef1f2f4b9 100644 --- a/modules/gdnative/gdnative/gdnative.cpp +++ b/modules/gdnative/gdnative/gdnative.cpp @@ -170,6 +170,19 @@ bool GDAPI godot_is_instance_valid(const godot_object *p_object) { return ObjectDB::instance_validate((Object *)p_object); } +void *godot_get_class_tag(const godot_string_name *p_class) { + StringName class_name = *(StringName *)p_class; + ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name); + return class_info ? class_info->class_ptr : NULL; +} + +godot_object *godot_object_cast_to(const godot_object *p_object, void *p_class_tag) { + if (!p_object) return NULL; + Object *o = (Object *)p_object; + + return o->is_class_ptr(p_class_tag) ? (godot_object *)o : NULL; +} + #ifdef __cplusplus } #endif diff --git a/modules/gdnative/gdnative_api.json b/modules/gdnative/gdnative_api.json index 9e5295a936..7e2ca49f8d 100644 --- a/modules/gdnative/gdnative_api.json +++ b/modules/gdnative/gdnative_api.json @@ -140,6 +140,21 @@ "arguments": [ ["const godot_pool_color_array *", "p_self"] ] + }, + { + "name": "godot_get_class_tag", + "return_type": "void *", + "arguments": [ + ["const godot_string_name *", "p_class"] + ] + }, + { + "name": "godot_object_cast_to", + "return_type": "godot_object *", + "arguments": [ + ["const godot_object *", "p_object"], + ["void *", "p_class_tag"] + ] } ] }, diff --git a/modules/gdnative/include/gdnative/gdnative.h b/modules/gdnative/include/gdnative/gdnative.h index 5a6333e814..2fe59b8a73 100644 --- a/modules/gdnative/include/gdnative/gdnative.h +++ b/modules/gdnative/include/gdnative/gdnative.h @@ -286,6 +286,10 @@ void GDAPI godot_print(const godot_string *p_message); bool GDAPI godot_is_instance_valid(const godot_object *p_object); +//tags used for safe dynamic casting +void GDAPI *godot_get_class_tag(const godot_string_name *p_class); +godot_object GDAPI *godot_object_cast_to(const godot_object *p_object, void *p_class_tag); + #ifdef __cplusplus } #endif |