Skip to content

Commit

Permalink
Reimplement Resource._local_to_scene_setup & remove workaround
Browse files Browse the repository at this point in the history
Reimplements the virtual method _setup_local_to_scene, lost in #51970, and renames it to `_local_to_scene_setup`.

Also removes the redundant `setup_local_to_scene_requested` signal.
  • Loading branch information
Mickeon committed Oct 8, 2022
1 parent bbac819 commit f8727b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
11 changes: 7 additions & 4 deletions core/io/resource.cpp
Expand Up @@ -362,8 +362,10 @@ Node *Resource::get_local_scene() const {
}

void Resource::setup_local_to_scene() {
// Can't use GDVIRTUAL in Resource, so this will have to be done with a signal
emit_signal(SNAME("setup_local_to_scene_requested"));
if (get_script_instance()) {
Callable::CallError ce;
get_script_instance()->callp(SNAME("_local_to_scene_setup"), nullptr, 0, ce);
}
}

Node *(*Resource::_get_local_scene_func)() = nullptr;
Expand Down Expand Up @@ -431,7 +433,6 @@ void Resource::_bind_methods() {

ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
ADD_SIGNAL(MethodInfo("changed"));
ADD_SIGNAL(MethodInfo("setup_local_to_scene_requested"));

ADD_GROUP("Resource", "resource_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
Expand All @@ -440,8 +441,10 @@ void Resource::_bind_methods() {

MethodInfo get_rid_bind("_get_rid");
get_rid_bind.return_val.type = Variant::RID;

::ClassDB::add_virtual_method(get_class_static(), get_rid_bind, true, Vector<String>(), true);

MethodInfo _local_to_scene_setup("_local_to_scene_setup");
::ClassDB::add_virtual_method(get_class_static(), _local_to_scene_setup, true, Vector<String>(), true);
}

Resource::Resource() :
Expand Down
19 changes: 15 additions & 4 deletions doc/classes/Resource.xml
Expand Up @@ -17,6 +17,21 @@
<description>
</description>
</method>
<method name="_local_to_scene_setup" qualifiers="virtual">
<return type="void" />
<description>
Override this method to customise the newly duplicated Resource created from [method PackedScene.instantiate], if the original's [member resource_local_to_scene] is set to [code]true[/code].
[b]Example:[/b] Assign a random value to [code]health[/code] for every duplicated Resource from an instantiated scene, excluding the original.
[codeblock]
extends Resource

var health = 0

func _local_to_scene_setup():
health = randi_range(10, 40)
[/codeblock]
</description>
</method>
<method name="duplicate" qualifiers="const">
<return type="Resource" />
<param index="0" name="subresources" type="bool" default="false" />
Expand Down Expand Up @@ -84,9 +99,5 @@
[b]Note:[/b] This signal is not emitted automatically for custom resources, which means that you need to create a setter and emit the signal yourself.
</description>
</signal>
<signal name="setup_local_to_scene_requested">
<description>
</description>
</signal>
</signals>
</class>
2 changes: 2 additions & 0 deletions editor/project_converter_3_to_4.cpp
Expand Up @@ -218,6 +218,7 @@ static const char *gdscript_function_renames[][2] = {
{ "_set_current", "set_current" }, // Camera2D
{ "_set_editor_description", "set_editor_description" }, // Node
{ "_set_playing", "set_playing" }, // AnimatedSprite3D
{ "_setup_local_to_scene", "_local_to_scene_setup" }, // Resource
{ "_toplevel_raise_self", "_top_level_raise_self" }, // CanvasItem
{ "_update_wrap_at", "_update_wrap_at_column" }, // TextEdit
{ "add_animation", "add_animation_library" }, // AnimationPlayer
Expand Down Expand Up @@ -664,6 +665,7 @@ static const char *csharp_function_renames[][2] = {
{ "_SetCurrent", "SetCurrent" }, // Camera2D
{ "_SetEditorDescription", "SetEditorDescription" }, // Node
{ "_SetPlaying", "SetPlaying" }, // AnimatedSprite3D
{ "_SetupLocalToScene", "_LocalToSceneSetup" }, // Resource
{ "_ToplevelRaiseSelf", "_TopLevelRaiseSelf" }, // CanvasItem
{ "_UpdateWrapAt", "_UpdateWrapAtColumn" }, // TextEdit
{ "AddAnimation", "AddAnimationLibrary" }, // AnimationPlayer
Expand Down

0 comments on commit f8727b3

Please sign in to comment.