godot: `Resource.duplicate(true)` doesn't duplicate subresources stored in Array or Dictionary properties
Bugsquad note: This issue has been confirmed several times already. No need to confirm it further.
Godot version
4.0.stable
System information
Manjaro Linux
Issue description
This behavior is poorly documented, and it breaks expectations. It is mentioned in the docs only under Array.duplicate(), but not Dictionary class, or Resource.duplicate().
This affects also “Make Unique” and “Make Unique (Recursive)” for custom resources.
Here’s the problematic code: https://github.com/godotengine/godot/blob/fc7adaab7b3856a7831d402ea2bbb27efe7b7d8a/core/variant/variant_setget.cpp#L1889-L1900
The “breaks stuff 😦” comment in the code was left in 2018 (d280b14e6e7b686d4e1d873e6b1ac14754404075).
Steps to reproduce
For simple code see reproduction project.
To reproduce on your own:
- Have a resource with a property of type Array[SomeOtherResource]
- One of the following: 2a) “Make Unique” on parent resource in inspector. 2b) Or duplicate(true) from code.
- Check if SomeOtherResource items inside array are unique.
Minimal reproduction project
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 121
- Comments: 40 (9 by maintainers)
Links to this issue
Commits related to this issue
- Rework how point geometry is handled - Single points setter, single property, no setter overridding - Copy-pasted shapes are sharing geometry by default now (in line with the editor conventions) - Im... — committed to limbonaut/SmartShape2D by limbonaut 4 years ago
- Circumvented important bug: https://github.com/godotengine/godot/issues/74918 Also added hitpoints, visuals, playing cards with effects. — committed to Juules32/fusion-forge by Juules32 2 years ago
- Circumvented important bug: https://github.com/godotengine/godot/issues/74918 Also added hitpoints, visuals, playing cards with effects. — committed to Juules32/fusion-forge by Juules32 2 years ago
It’s troublesome that this isn’t fixed yet, my game relies a lot on duplicating resources and arrays and I was really miffed when I just found out deep copying isn’t functioning properly. This should be tracked and added to a milestone.
Can this at least be updated in the documentation ASAP even if it won’t be fixed any time soon? The Resource.duplicate docs confidently state:
Which is clearly not true and causing headaches for many users in this thread. It’s now been over 1 year with no comments from the team.
@Johnrobmiller You can help test and review the open PR to help get it merged. Godot is a project developed primarily by volunteers, and is provided AS IS without any guarantee of fitness for purpose. If this bug is a deal breaker for you, that’s sad, but we do what we can with limited resources. Please be respectful for the work of contributors.
Yeah this seems like a pretty major bug to me. It’s workaroundable but I don’t particuarly like the elegancy of any of the workarounds.
Oh my god, I was confused why all my enemy mobs die at the same time like they have shared HP. Had to make multiple loops to manually duplicate subresources in my ready function to work around this. This is rough for stat heavy games out there.
I confim that
my_resource = my_resource.duplicate(true)doesn’t make unique sub-resources as the documentation says. Godot 4.1.We were discussing this issue on a discord with @monxa and Yuuto, and here are some key takes:
Also stumbled upon this issue after a day of frustration. The docs should really be updated as “If subresources is true, a deep copy is returned; nested subresources will be duplicated and are not shared.” is factually not correct.
As nobody explicitly mentioned the simple (but dumb) workaround: after creating a duplicate using
duplicate(true), you can iterate over the arrays and dictionaries in those resources (and the ones in those, if you have them nested) and replace the elements with duplicates of themselves. Like so:Any news on this? It’s pretty crucial for a resource heavy game 😕
Why is this not fixed? Isn’t this supposed to be the way to clone data/references in GDScript? I can’t imagine a coding language where you cannot clone data or references.
It is true that Godot is a cherished open-source tool that people love and that we all are grateful for. But it’s equally true that people are relying on this game engine for their careers and livelihood. Letting issues like this persist for years on end is irresponsible.
I see an open PR – can that not be tested and merged?
I can’t believe this hasn’t been fixed, my project relies heavily on resources that have arrays or dictionaries in them, for localToScene to not work with them is really bad. I’m now having to write duplication methods for more than 10 different classes.
This doesn’t seem to be limited to arrays. I have a resource that contains a node, set on ready, and no matter what I do, all resources on my enemies seem to share that same node setting, even if it’s set AFTER the resource is duplicated.
The problem is specifically on the line
projectile_action.projectile_position_node = enemy.get_node(projectile_action.projectile_position_node_path)All enemies fire projectiles from the same node (so enemy A may be shooting at me, but the projectile fires from enemy F).
For reference, here’s the fire projectile resource:
Even if I set the resource as local to scene, it still has the issue. Am I doing a dumb or are non-export values within a resource shared?
Edit: Ok, this does actually loop around to being relevant to the topic at hand. Seems the problem for me wasn’t the duplicate() of the resource but, in fact, the array of resources on the enemy scene being shared.
So, after duplicating the array, it fixed the issue:
The method in resource that duplicates itself is kind of a pain in the ass if you have a million resources inside of resources isn’t it? You would have to make the same method for a billion different resources. This really needs to be fixed
After thinking for a while why copying resources nested in arrays didn’t work, I opened github and searched for this issue as expected. The document should be adjusted quickly to avoid more victims. 🌝
Edit: So, I kinda figured it out where my (and many others) issues come from regarding duplicating resources and saving and loading them.
The initial problem is that saving and loading subresources just does not work if you simply save and load using ResourceSaver and RexourceLoader IF any of your subresources are actually instanced. ResourceSaver will just save a reference to the project file instance.
Now, the "FLAG_BUNDLE_RESOURCES " should fix it, but it is actually not working right now (#65393)
If you want to save your instanced subresources you need to deep duplicate them using “BaseResource”.
It’s also important du make deep duplications when you have a resource “template” saved that you later want to add to an array List multiple times.
I am trying to use this, together with: https://github.com/godotengine/godot/issues/65393#issuecomment-1847601360 to save and load resources that contain an Array with other Resources.
It does not work unfortunately:
Has anyone managed to save and load resources that contain an Array of another resources?
I’ve tested this recently in 4.2.1 and no, it wasn’t fixed when using
Arrayat least. If you think it’s fixed, please post a simple example of it working, as it might help solve the issue.Here’s the PR I made to add this exception to the docs: https://github.com/godotengine/godot/pull/94142
This is being tracked in https://github.com/godotengine/godot-proposals/issues/317 and https://github.com/godotengine/godot-proposals/issues/4672.
I’m not sure if this is related or a new bug, but I’m finding nothing is duplicated in my custom resource. I’ve got a custom resource with a bunch of different variables including
dictionariesand standalone variableintsand such.And when attempting to use theIt seems likeduplicate(true)function none of it is copied over. The dictionaries are blank and the integers are also their default value.integersbased onenumsare not copied, but regularintegersare… strange.But essentially I have to manually copy over every value from one resource to another, which is far from ideal.
it’s not fix now. has any way to fix this problem?
I made a gdscript class, it’s extend Resource to implement a function call _duplicate to fix this problem.
But please fix this in the engine as soon as possible.
the class code is :
I’m guessing one of the issues with this (though perhaps not the only reason it was removed) is because resources can self-reference which would break this. Here is a pseudo-code thought on it:
This isn’t in a working state but I am trying to look at what direction it might be able to go and help the conversation around this as I need this fixed myself.
This also occurs on all 3.x variants.