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

Resource.duplicate(true) also duplicates the script #58031

Closed
mphe opened this issue Feb 12, 2022 · 3 comments
Closed

Resource.duplicate(true) also duplicates the script #58031

mphe opened this issue Feb 12, 2022 · 3 comments

Comments

@mphe
Copy link
Contributor

mphe commented Feb 12, 2022

Godot version

3.4.2 and 4.0 alpha

System information

Arch Linux

Issue description

Calling Resource.duplicate(true) to make a deep copy of a resource will also duplicate the assigned script. This bug exists in both Godot 3 and 4.
However, in Godot 3 it was possible to override the duplicate function to manually handle copying. In Godot 4.0 overriding does not work.

Technically, it behaves correctly as it is supposed to make a deep copy, but I don't think it should include the script.

Steps to reproduce

The reproduction project contains a custom resource CustomResource and a tool node ResourceDuplicator that instantiates one CustomResource and then duplicates it in _ready.

  1. Get the reproduction project (made for 4.0)
  2. Open main.tscn
  3. Add node -> ResourceDuplicator

Immediately after, Godot will log an error of another class hiding CustomResource.

- built-in:3 - Parse Error: Class "CustomResource" hides a global script class.
- modules/gdscript/gdscript.cpp:873 - Method/function failed. Returning: ERR_PARSE_ERROR

This happens because Godot also copies the script. Upon saving you can also see the duplicated script in main.tscn.

[gd_scene load_steps=6 format=3 uid="uid://c4debhsy0cak8"]

[ext_resource type="Script" path="res://ResourceDuplicator.gd" id="1_siepb"]
[ext_resource type="Script" path="res://CustomResource.gd" id="2_gg6uf"]

[sub_resource type="Resource" id="Resource_5rudm"]
script = ExtResource( "2_gg6uf" )

[sub_resource type="GDScript" id="GDScript_bv7cb"]
script/source = "@tool
extends Resource
class_name CustomResource

# Does not seem to get called
func duplicate(deep: bool = false):
    push_warning(\"DUP\")
    return CustomResource.new()
"

[sub_resource type="Resource" id="Resource_v1am0"]
script = SubResource( "GDScript_bv7cb" )

[node name="Node2D" type="Node2D"]

[node name="ResourceDuplicator" type="Node" parent="."]
script = ExtResource( "1_siepb" )
resource = SubResource( "Resource_5rudm" )
resource2 = SubResource( "Resource_v1am0" )

Minimal reproduction project

resource_script_duplication_4.0.zip

@Calinou Calinou added this to the 4.0 milestone Feb 12, 2022
mphe added a commit to mphe/SmartShape2D that referenced this issue Feb 13, 2022
@DaGamingWolf
Copy link

big oof on this. i was running in circles trying to find the source.

@mphe
Copy link
Contributor Author

mphe commented Mar 3, 2023

Seems to be fixed in Godot 4.0.
Is it planned to backport this fix to 3.x? Otherwise, this issue can probably be closed.

@akien-mga
Copy link
Member

Indeed this was fixed by #71142, and this issue is a duplicate of #33079.

The fix could be backported if needed (without the property usage constant rename and reodering, to preserve compatibility).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants