godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
88.93k stars 20.17k forks source link

Saved resource loses data when duplicated #94242

Open vvvvvvitor opened 2 months ago

vvvvvvitor commented 2 months ago

Tested versions

System information

Godot v4.2.2.stable (15073afe3) - Freedesktop SDK 23.08 (Flatpak runtime) - Wayland - Vulkan (Forward+) - integrated Intel(R) UHD Graphics (ICL GT1) () - Intel(R) Core(TM) i3-1005G1 CPU @ 1.20GHz (4 Threads)

Issue description

When duplicating a saved resource, it's data seems to get lost.

The resource:

image

The code:

const ITEM_INDEX: Array[Item] = [
    preload("res://res/items/item_abyss_garden_key.tres")
]

# [...]

@export var icon: Texture = PLACEHOLDER_ICON
@export var name: String = "item"
@export var display_name: String = "Item"
@export_multiline var description: String = "The ultimate weapon!"

# [...]

static func get_item_from_index(item_id: int) -> Item:
    return ITEM_INDEX[item_id].duplicate()

When trying to get the values from the resource it returns the values that were predefined, in this case, if I try to get the "name" property, it'll return "item" instead of "garden_key".

When also trying to access the data of the resource directly (load("path").property) I get an error that says the property does not exist, even though it should

Invalid get index 'name' (on base: 'Resource').

Steps to reproduce

Minimal reproduction project (MRP)

N/A

Calinou commented 2 months ago
dalexeev commented 2 months ago

This is due to cyclic dependencies and loading order. You want to have a constant in a class with instances of the same class, but this does not work as expected. If you move the constant to another class (ItemDB), it will work. Also you can use a static variable instead of constant and load() instead of preload(), then it will work in the same class (Item).

MRP: test-94242.zip

vvvvvvitor commented 2 months ago

This is due to cyclic dependencies and loading order. You want to have a constant in a class with instances of the same class, but this does not work as expected. If you move the constant to another class (ItemDB), it will work. Also you can use a static variable instead of constant and load() instead of preload(), then it will work in the same class (Item).

MRP: test-94242.zip

Ah, I see, should I close this issue then?

dalexeev commented 2 months ago

I think no. Even if we don't support this case, it should probably produce an error since constants are evaluated at compile time.