Merge pull request #27886 from LeonardMeagher2/obj_to_string
Allow overriding how scripted objects are converted to strings
This commit is contained in:
@ -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);
|
||||
|
||||
@ -208,6 +208,7 @@ public:
|
||||
virtual bool has_method(const StringName &p_method) const;
|
||||
virtual Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
|
||||
virtual void notification(int p_notification);
|
||||
String to_string(bool *r_valid);
|
||||
virtual Ref<Script> get_script() const;
|
||||
virtual MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const;
|
||||
virtual MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const;
|
||||
|
||||
Reference in New Issue
Block a user