summaryrefslogtreecommitdiff
path: root/modules/gdnative
diff options
context:
space:
mode:
authorKarroffel <therzog@mail.de>2017-04-17 19:52:18 +0200
committerKarroffel <therzog@mail.de>2017-04-17 19:52:18 +0200
commitf4261c88684cf27e7e8e9082c1cf850162c08ac4 (patch)
tree932cfe3e169e438432a295fc75845cdb56be5cc8 /modules/gdnative
parente46af1e23668e23160604742f8f1d22898796d1c (diff)
[GDNative] added "new" method and fixed headers
Diffstat (limited to 'modules/gdnative')
-rw-r--r--modules/gdnative/gdnative.cpp51
-rw-r--r--modules/gdnative/gdnative.h2
-rw-r--r--modules/gdnative/godot/godot_string.h1
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 9289531c5e..4deec0922c 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 {