diff options
author | Thomas Herzog <karroffel@users.noreply.github.com> | 2017-04-17 20:41:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-17 20:41:32 +0200 |
commit | 68d79b21c36ee2208970ee45471c4423ef32e3f1 (patch) | |
tree | 63619af4d063fbc1ba9ba36dee46acdc24539478 /modules/gdnative | |
parent | a88c20f0ca4d970bc079544e87cd00f15f744d62 (diff) | |
parent | f4261c88684cf27e7e8e9082c1cf850162c08ac4 (diff) |
Merge pull request #8440 from karroffel/gdnative-new-method
[GDNative] added "new" method and fixed headers
Diffstat (limited to 'modules/gdnative')
-rw-r--r-- | modules/gdnative/gdnative.cpp | 51 | ||||
-rw-r--r-- | modules/gdnative/gdnative.h | 2 | ||||
-rw-r--r-- | modules/gdnative/godot/godot_string.h | 1 |
3 files changed, 54 insertions, 0 deletions
diff --git a/modules/gdnative/gdnative.cpp b/modules/gdnative/gdnative.cpp index b543a486d3..a4c9085f45 100644 --- a/modules/gdnative/gdnative.cpp +++ b/modules/gdnative/gdnative.cpp @@ -391,6 +391,55 @@ void GDNativeScript::get_script_signal_list(List<MethodInfo> *r_signals) const { } } +Variant GDNativeScript::_new(const Variant **p_args, int p_argcount, Variant::CallError &r_error) { + + /* STEP 1, CREATE */ + + if (!library.is_valid() || ((String)script_name).empty() || !script_data) { + r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + + r_error.error = Variant::CallError::CALL_OK; + REF ref; + Object *owner = NULL; + + GDNativeScriptData *_baseptr = script_data; + while (_baseptr->base_data) { + _baseptr = _baseptr->base_data; + } + + if (!(_baseptr->base_native_type == "")) { + owner = ClassDB::instance(_baseptr->base_native_type); + } else { + owner = memnew(Reference); //by default, no base means use reference + } + + Reference *r = owner->cast_to<Reference>(); + if (r) { + ref = REF(r); + } + + // GDScript does it like this: _create_instance(p_args, p_argcount, owner, r != NULL, r_error); + // @Todo support varargs for constructors. + GDNativeInstance *instance = (GDNativeInstance *)instance_create(owner); + + owner->set_script(Ref<GDNativeScript>(this).get_ref_ptr()); + owner->set_script_instance(instance); + if (!instance) { + if (ref.is_null()) { + memdelete(owner); //no owner, sorry + } + return Variant(); + } + + if (ref.is_valid()) { + return ref; + } else { + return owner; + } +} + Ref<GDNativeLibrary> GDNativeScript::get_library() const { return library; } @@ -438,6 +487,8 @@ void GDNativeScript::_bind_methods() { ClassDB::bind_method(D_METHOD("get_script_name"), &GDNativeScript::get_script_name); ClassDB::bind_method(D_METHOD("set_script_name", "script_name"), &GDNativeScript::set_script_name); + ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &GDNativeScript::_new, MethodInfo(Variant::OBJECT, "new")); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "GDNativeLibrary"), "set_library", "get_library"); ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "script_name"), "set_script_name", "get_script_name"); } diff --git a/modules/gdnative/gdnative.h b/modules/gdnative/gdnative.h index 89270b4e26..27e0c3788b 100644 --- a/modules/gdnative/gdnative.h +++ b/modules/gdnative/gdnative.h @@ -184,6 +184,8 @@ public: virtual void get_script_method_list(List<MethodInfo> *p_list) const; virtual void get_script_property_list(List<PropertyInfo> *p_list) const; + Variant _new(const Variant **p_args, int p_argcount, Variant::CallError &r_error); + Ref<GDNativeLibrary> get_library() const; void set_library(Ref<GDNativeLibrary> p_library); diff --git a/modules/gdnative/godot/godot_string.h b/modules/gdnative/godot/godot_string.h index cf5a22dd8d..83ed5d6ec1 100644 --- a/modules/gdnative/godot/godot_string.h +++ b/modules/gdnative/godot/godot_string.h @@ -35,6 +35,7 @@ extern "C" { #endif #include <stdint.h> +#include <wchar.h> #ifndef GODOT_CORE_API_GODOT_STRING_TYPE_DEFINED typedef struct godot_string { |