Godot Version
4.6
Question
making an editor plugin to help make items/tiles easier for my game, i store all the them in a custom resoruce thats just an array, no matter what i do i cant get the resource to update, ive gone as far as deleting it and remaking it with the code below but it still holds all the old values, like the chached version is taking over.my script shows no errors. in fact it tells me yup saving 22 items but the DB forever stays at 20 , i have restarted the editor and my pc many times, very very rarley it will work and ill see the new items, but not consistantly please help. this was supposed to save me time but ive spent 2 days on this so far, trying all sorts
func _on_rebuild_db_pressed() -> void:
# 1) Scan disk for items (do NOT rely on DB)
var files := _scan_tres_files(ITEMS_DIR)
if files.is_empty():
_set_status("ERROR: No .tres files found in: %s" % ITEMS_DIR)
return
if not _ensure_itemdata_script():
return
var typed: Array[Resource] = [] # keep as Resource to avoid type issues in tool scripts
var ok_count := 0
var bad_count := 0
for p in files:
var res := ResourceLoader.load(p, "", ResourceLoader.CACHE_MODE_IGNORE)
if res == null:
bad_count += 1
continue
if _get_script_path_of(res) != ITEMDATA_SCRIPT_PATH:
bad_count += 1
continue
# Validate minimal fields
var id_v = res.get("id")
if id_v == null or StringName(id_v) == &"":
bad_count += 1
continue
ok_count += 1
typed.append(res)
if typed.is_empty():
_set_status("ERROR: Rebuild cancelled. 0 valid ItemData found in: %s" % ITEMS_DIR)
return
# 2) Sort deterministically by id (stable output)
typed.sort_custom(func(a: Resource, b: Resource) -> bool:
var aid := String(a.get("id"))
var bid := String(b.get("id"))
return aid.naturalnocasecmp_to(bid) < 0
)
# 3) Back up existing DB
if ResourceLoader.exists(ITEM_DB_PATH):
var abs_src := ProjectSettings.globalize_path(ITEM_DB_PATH)
var abs_bak := ProjectSettings.globalize_path(ITEM_DB_PATH + ".bak")
if FileAccess.file_exists(abs_bak):
DirAccess.remove_absolute(abs_bak)
var rerr := DirAccess.copy_absolute(abs_src, abs_bak)
if rerr != OK:
_set_status("ERROR: Failed to backup Item DB (err=%d): %s" % [rerr, ITEM_DB_PATH + ".bak"])
return
# 4) Create a brand new DB resource and save it
# Assumes: class_name ItemDatabaseResource, export var items: Array[ItemData]
var new_db := ItemDatabaseResource.new()
new_db.set("items", typed)
new_db.emit_changed()
_ensure_res_dir(ITEM_DB_PATH.get_base_dir())
var err := ResourceSaver.save(new_db, ITEM_DB_PATH)
if err != OK:
_set_status("ERROR: Failed to write Item DB (err=%d): %s" % [err, ITEM_DB_PATH])
return
# 5) Refresh editor filesystem (optional but helps)
if Engine.is_editor_hint() and _editor_if != null:
_editor_if.get_resource_filesystem().scan()
# 6) HARD VERIFY: reload what we just wrote (REPLACE bypasses cache)
var reloaded := ResourceLoader.load(ITEM_DB_PATH, "", ResourceLoader.CACHE_MODE_REPLACE)
if reloaded == null:
_set_status("VERIFY FAILED: could not reload Item DB after save.")
return
var arr_any = reloaded.get("items")
var count := 0
if arr_any is Array:
count = (arr_any as Array).size()
print("VERIFY Item DB reload items_count=", count)
if arr_any is Array:
var a := arr_any as Array
for i in range(min(5, a.size())):
var it = a[i]
if it is Resource:
print(" [", i, "] id=", StringName((it as Resource).get("id")), " path=", (it as Resource).resource_path)
# 7) Update the dock list from disk too
_refresh_items_list()
_set_status("Rebuilt Item DB from disk with %d items. (skipped %d)" % [ok_count, bad_count])
then the resource is just a simple array of resoruces @tool
@tool
extends Resource
class_name ItemDatabaseResource
@export var items: Array[ItemData] =