-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs |
||||||
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(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs |
||||||
} | ||||||
|
||||||
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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also needs |
||||||
} | ||||||
|
||||||
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); | ||||||
} |
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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of
Suggested change
Although your approach with |
||||||
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 |
There was a problem hiding this comment.
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.
godot/modules/gdscript/gdscript.cpp
Lines 1615 to 1623 in f3566bc
But according to the issue this already works. I find it a little weird that in this implementation,
is_custom()
andis_standard()
will return different values forstatic_func
andTest.static_func
.