Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom callables for GDScript static methods #82755

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "gdscript_compiler.h"
#include "gdscript_parser.h"
#include "gdscript_rpc_callable.h"
#include "gdscript_static_callable.h"
#include "gdscript_warning.h"

#ifdef TOOLS_ENABLED
Expand Down Expand Up @@ -886,7 +887,7 @@ bool GDScript::_get(const StringName &p_name, Variant &r_ret) const {
if (top->rpc_config.has(p_name)) {
r_ret = Callable(memnew(GDScriptRPCCallable(const_cast<GDScript *>(top), E->key)));
} else {
r_ret = Callable(const_cast<GDScript *>(top), E->key);
r_ret = Callable(memnew(GDScriptStaticCallable(const_cast<GDScript *>(top), E->key)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is not the only place. You can also access a static method via an instance.

HashMap<StringName, GDScriptFunction *>::ConstIterator E = sptr->member_functions.find(p_name);
if (E) {
if (sptr->rpc_config.has(p_name)) {
r_ret = Callable(memnew(GDScriptRPCCallable(this->owner, E->key)));
} else {
r_ret = Callable(this->owner, E->key);
}
return true;
}

But according to the issue this already works. I find it a little weird that in this implementation, is_custom() and is_standard() will return different values for static_func and Test.static_func.

}
return true;
}
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class GDScript : public Script {
friend class GDScriptCompiler;
friend class GDScriptDocGen;
friend class GDScriptLanguage;
friend class GDScriptStaticCallable;
friend struct GDScriptUtilityFunctionsDefinitions;

Ref<GDScriptNativeClass> native;
Expand Down
86 changes: 86 additions & 0 deletions modules/gdscript/gdscript_static_callable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**************************************************************************/
/* gdscript_static_callable.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "gdscript_static_callable.h"

#include "gdscript.h"

#include "core/templates/hashfuncs.h"

bool GDScriptStaticCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
return p_a->hash() == p_b->hash();
}

bool GDScriptStaticCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
return p_a->hash() < p_b->hash();
}

uint32_t GDScriptStaticCallable::hash() const {
return h;
}

String GDScriptStaticCallable::get_as_text() const {
String class_name = script->get_class();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs script.is_valid() check.

if (script->get_path().is_resource_file()) {
class_name += "(" + script->get_path().get_file() + ")";
}
return class_name + "::" + String(method);
}

CallableCustom::CompareEqualFunc GDScriptStaticCallable::get_compare_equal_func() const {
return compare_equal;
}

CallableCustom::CompareLessFunc GDScriptStaticCallable::get_compare_less_func() const {
return compare_less;
}

bool GDScriptStaticCallable::is_valid() const {
return script.is_valid() && script->has_method(method);
}

ObjectID GDScriptStaticCallable::get_object() const {
return script->get_instance_id();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs script.is_valid() check.

}

StringName GDScriptStaticCallable::get_method() const {
return method;
}

void GDScriptStaticCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
r_return_value = script->callp(method, p_arguments, p_argcount, r_call_error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r_return_value = script->callp(method, p_arguments, p_argcount, r_call_error);
r_return_value = function->call(nullptr, p_arguments, p_argcount, r_call_error);

Also needs script.is_valid() check and hot reloading implementation, see #81628. Your approach with StringName is safer.

}

GDScriptStaticCallable::GDScriptStaticCallable(const Ref<GDScript> &p_script, const StringName &p_method) {
script = p_script;
method = p_method;
h = method.hash();
h = hash_murmur3_one_64(script->get_instance_id(), h);
}
62 changes: 62 additions & 0 deletions modules/gdscript/gdscript_static_callable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**************************************************************************/
/* gdscript_static_callable.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef GDSCRIPT_STATIC_CALLABLE_H
#define GDSCRIPT_STATIC_CALLABLE_H

#include "core/object/ref_counted.h"
#include "core/variant/callable.h"
#include "core/variant/variant.h"

class GDScript;

class GDScriptStaticCallable : public CallableCustom {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is worth adding a comment that this wrapper was added only as a workaround to the issue described in #79521 (comment).

Ref<GDScript> script;
StringName method;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of StringName, you can use a GDScriptFunction pointer here.

Suggested change
StringName method;
GDScriptFunction *function = nullptr;

Although your approach with StringName is safer.

uint32_t h = 0;

static bool compare_equal(const CallableCustom *p_a, const CallableCustom *p_b);
static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b);

public:
uint32_t hash() const override;
String get_as_text() const override;
CompareEqualFunc get_compare_equal_func() const override;
CompareLessFunc get_compare_less_func() const override;
bool is_valid() const override;
ObjectID get_object() const override;
StringName get_method() const override;
void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override;

GDScriptStaticCallable(const Ref<GDScript> &p_script, const StringName &p_method);
virtual ~GDScriptStaticCallable() = default;
};

#endif // GDSCRIPT_STATIC_CALLABLE_H
Loading