summaryrefslogtreecommitdiffstats
path: root/modules/gdnative/nativescript/nativescript.cpp
diff options
context:
space:
mode:
authorLeonard Meagher <leonardmeagher2@gmail.com>2019-04-09 22:07:40 -0700
committerLeonard Meagher <leonardmeagher2@gmail.com>2019-05-03 15:06:05 -0700
commitf7eb426e2ebafc5598b0e43baf37d9a50cea1648 (patch)
tree5dfda6e83e3b59b1a52923d7fead0aea4ff15ff2 /modules/gdnative/nativescript/nativescript.cpp
parent5772f60f960ee8c396574f0c6f94def18bb210c7 (diff)
downloadredot-engine-f7eb426e2ebafc5598b0e43baf37d9a50cea1648.tar.gz
Allow overriding how scripted objects are converted to strings
solves #26796 - ADD `String to_string()` method to Object which can be overriden by `String _to_string()` in scripts - ADD `String to_string(r_valid)` method to ScriptInstance to allow langauges to control how scripted objects are converted to strings - IMPLEMENT to_string for GDScriptInstance, VisualScriptInstance, and NativeScriptInstance - ADD Documentation about `Object.to_string` and `Object._to_string` - Changed `Variant::operator String` to use `obj->to_string()`
Diffstat (limited to 'modules/gdnative/nativescript/nativescript.cpp')
-rw-r--r--modules/gdnative/nativescript/nativescript.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/modules/gdnative/nativescript/nativescript.cpp b/modules/gdnative/nativescript/nativescript.cpp
index 5cf144d4fe..b743768783 100644
--- a/modules/gdnative/nativescript/nativescript.cpp
+++ b/modules/gdnative/nativescript/nativescript.cpp
@@ -32,6 +32,7 @@
#include "gdnative/gdnative.h"
+#include "core/core_string_names.h"
#include "core/global_constants.h"
#include "core/io/file_access_encrypted.h"
#include "core/os/file_access.h"
@@ -771,6 +772,27 @@ void NativeScriptInstance::notification(int p_notification) {
call_multilevel("_notification", args, 1);
}
+String NativeScriptInstance::to_string(bool *r_valid) {
+ if (has_method(CoreStringNames::get_singleton()->_to_string)) {
+ Variant::CallError ce;
+ Variant ret = call(CoreStringNames::get_singleton()->_to_string, NULL, 0, ce);
+ if (ce.error == Variant::CallError::CALL_OK) {
+ if (ret.get_type() != Variant::STRING) {
+ if (r_valid)
+ *r_valid = false;
+ ERR_EXPLAIN("Wrong type for " + CoreStringNames::get_singleton()->_to_string + ", must be a String.");
+ ERR_FAIL_V(String());
+ }
+ if (r_valid)
+ *r_valid = true;
+ return ret.operator String();
+ }
+ }
+ if (r_valid)
+ *r_valid = false;
+ return String();
+}
+
void NativeScriptInstance::refcount_incremented() {
Variant::CallError err;
call("_refcount_incremented", NULL, 0, err);