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 deprecates the redundant `setup_local_to_scene_requested` signal.
  • Loading branch information
Mickeon committed Sep 6, 2023
1 parent 8449592 commit 75b5f45
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
5 changes: 3 additions & 2 deletions core/io/resource.cpp
Expand Up @@ -379,8 +379,8 @@ 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"));
GDVIRTUAL_CALL(_local_to_scene_setup);
}

void Resource::reset_local_to_scene() {
Expand Down Expand Up @@ -458,8 +458,9 @@ 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);

GDVIRTUAL_BIND(_local_to_scene_setup);
}

Resource::Resource() :
Expand Down
2 changes: 2 additions & 0 deletions core/io/resource.h
Expand Up @@ -33,6 +33,7 @@

#include "core/io/resource_uid.h"
#include "core/object/class_db.h"
#include "core/object/gdvirtual.gen.inc"
#include "core/object/ref_counted.h"
#include "core/templates/safe_refcount.h"
#include "core/templates/self_list.h"
Expand Down Expand Up @@ -81,6 +82,7 @@ class Resource : public RefCounted {
void _take_over_path(const String &p_path);

virtual void reset_local_to_scene();
GDVIRTUAL0(_local_to_scene_setup);

Check failure on line 85 in core/io/resource.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

member access into incomplete type 'ScriptInstance'

Check failure on line 85 in core/io/resource.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

member access into incomplete type 'ScriptInstance'

Check failure on line 85 in core/io/resource.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

member access into incomplete type 'ScriptInstance'

Check failure on line 85 in core/io/resource.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

member access into incomplete type 'ScriptInstance'

Check failure on line 85 in core/io/resource.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

member access into incomplete type 'ScriptInstance'

Check failure on line 85 in core/io/resource.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

member access into incomplete type 'ScriptInstance'

Check failure on line 85 in core/io/resource.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

member access into incomplete type 'ScriptInstance'In file included from ./core/input/input_event.h:35:

Check failure on line 85 in core/io/resource.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

member access into incomplete type 'ScriptInstance'

public:
static Node *(*_get_local_scene_func)(); //used by editor
Expand Down
36 changes: 17 additions & 19 deletions doc/classes/Resource.xml
Expand Up @@ -19,6 +19,21 @@
Override this method to return a custom [RID] when [method get_rid] is called.
</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 @@ -58,20 +73,8 @@
<method name="setup_local_to_scene">
<return type="void" />
<description>
Emits the [signal setup_local_to_scene_requested] signal. If [member resource_local_to_scene] is set to [code]true[/code], this method is called from [method PackedScene.instantiate] by the newly duplicated resource within the scene instance.
For most resources, this method performs no logic of its own. Custom behavior can be defined by connecting [signal setup_local_to_scene_requested] from a script, [b]not[/b] by overriding this method.
[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 _init():
setup_local_to_scene_requested.connect(randomize_health)

func randomize_health():
health = randi_range(10, 40)
[/codeblock]
Calls [method _local_to_scene_setup]. If [member resource_local_to_scene] is set to [code]true[/code], this method is called from [method PackedScene.instantiate] by the newly duplicated resource within the scene instance.
For most resources, this method performs no logic of its own. Custom behavior can be defined by overriding [method _local_to_scene_setup] in a script, [b]not[/b] by overriding this method.
</description>
</method>
<method name="take_over_path">
Expand Down Expand Up @@ -102,10 +105,5 @@
[b]Note:[/b] This signal is not emitted automatically for properties of custom resources. If necessary, a setter needs to be created to emit the signal.
</description>
</signal>
<signal name="setup_local_to_scene_requested">
<description>
Emitted when [method setup_local_to_scene] is called, usually by a newly duplicated resource with [member resource_local_to_scene] set to [code]true[/code]. Custom behavior can be defined by connecting this signal.
</description>
</signal>
</signals>
</class>
2 changes: 2 additions & 0 deletions editor/renames_map_3_to_4.cpp
Expand Up @@ -213,6 +213,7 @@ const char *RenamesMap3To4::gdscript_function_renames[][2] = {
{ "_get_configuration_warning", "_get_configuration_warnings" }, // Node
{ "_set_current", "set_current" }, // Camera2D
{ "_set_editor_description", "set_editor_description" }, // Node
{ "_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 @@ -642,6 +643,7 @@ const char *RenamesMap3To4::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 75b5f45

Please sign in to comment.