Get eror at index at base resource

Godot Version

4.2.1

Question

I dont have any idea how to parse the resource save, i never used this before

here my resource data :

the question is i want only get player_name, player_armor, health and player_biome_location

im using that for at GUI overlay

here the code about my GUI
extends Control

@onready var character_name = $Character_Name
@onready var biome_location = $Biome_Location
@onready var Health = $Health_Bar
@onready var Armor = $Armor_Bar
@onready var pause = $PauseMenu

var save = load("user://Save/Progress/Save_Progress.tres")

func _ready():
	if save is Resource:
		var player_name = save.player_name
		var player_armor = save.player_armor
		var player_health = save.player_health
		var player_location = save.player_biome_location
		player_singleton.setter_name(player_name)
		player_singleton.setter_armor(player_armor)
		player_singleton.setter_health(player_health)
		player_singleton.setter_location(player_location)
		character_name.set_text(player_singleton.getter_name())
		biome_location.set_text("Location : "+ player_singleton.getter_location())
		Health.set_value_no_signal(player_singleton.getter_health())
		Armor.set_value_no_signal(player_singleton.getter_armor())
	else:
		print("Failed to load resource data.")
	
	
func _on_player_health_changed(percentage:float):
	Health.set_value_no_signal(percentage)
	
func _on_player_armor_changed(percentage:float):
	Armor.set_value_no_signal(percentage)

is there something im missing at the code or something?

at stack trace is like this:

thx for help

Does the resource exist at the path? I don’t see you saving the resource anywhere, so also check the default values. You need to save the resource to disk with Resource.save(), otherwise it’ll only remain in memory and lose the values once the game is closed.

in that path ?
yes is that exist also the data is at the pic num 1 the top one, sir

ohh here the picture the file and the path :

also here the dataprogress data class im using for the save entry data :

class_name Data_Progress
extends Resource

@export var player_position: Vector2
@export var level_path:String
@export var player_name : String
@export var player_health : int
@export var player_armor : int
@export var player_biome_location : String
@export var mission_wolf_rescued : int
@export var mission_boss_elimated : int
@export var mission_enemy_elimated : int
@export var ammmo : int = 30
@export var mag : int = 150
@export var saved_data:Array[SavedData]=[]

func UpdatePos(value : Vector2):
	player_position = value

func add_magazine():
	if mag <=150:
		mag += 30

for save and load function
im put at the game scene in here :

extends Node2D

@onready var _world_root:WorldRoot = %WorldRoot
@onready var _save_progress:SaveLoadManagerFile =%SaveLoadManagerFile
@onready var _pause_screen = %PauseMenu
@onready var _alamac = %Alamanac_Main_Menu
var _paused:bool = false

var cros_hair_cursor = load("res://Assets/Image/Cursor/Cross_Hair/Layer 1.png")
var hand_cursor = load("res://Assets/Image/Cursor/hand_paw.png")
func _ready():
	Input.set_custom_mouse_cursor(cros_hair_cursor)
	_world_root.level_exit_reached.connect(_on_level_exit_reached)
	
func _on_level_exit_reached(next_level):
	_world_root.load_level_async(next_level)
	
	
func _process(_delta):
	if Input.is_action_just_pressed("pause"):
		_pause(not _paused)
	if Input.is_action_just_pressed("Quick_Save"):
		_save_progress.save_game()
	if Input.is_action_just_pressed("Quick_Load"):
		_save_progress.load_game()
	if Input.is_action_just_pressed("open_wolf_almanac"):
		_pause_alamac(not _paused)
			
func _pause(value:bool):
	_paused = value
	_pause_screen.visible = _paused
	get_tree().paused = _paused

func _pause_alamac(value:bool):
	_paused = value
	_alamac.visible = _paused
	get_tree().paused = _paused


# Called when the save game button is pressed
func _on_save_game():
	print("Save game!")
	_save_progress.save_game()
	
# Called when the load game button is pressed
func _on_load_game():
	print("Load game!")
	_save_progress.load_game()
	# _pause(true)
	
func mouse_exited():
	Input.set_custom_mouse_cursor(cros_hair_cursor)

func mouse_entered():
	Input.set_custom_mouse_cursor(hand_cursor)

for the save_load_manager class, here the code :

extends Node
class_name SaveLoadManagerFile

@onready var play:Players =  %Player as Players
@onready var _world_root:WorldRoot = %WorldRoot as WorldRoot


#func _ready():
	#new_game()
# Function to save game data
func save_game():
	var save_progress: Data_Progress = Data_Progress.new()
	
		# save the path to the currently loaded level
	save_progress.level_path = _world_root.get_current_level_path()
	
	#store player health, position, biome_location, armor, mission log, ammo and mag
	save_progress.player_position = play.global_position
	save_progress.player_name = player_singleton.name_character
	save_progress.player_biome_location = player_singleton.location
	save_progress.player_health = player_singleton.health
	save_progress.player_armor = player_singleton.armor
	save_progress.mission_wolf_rescued = mission_data_stat.wolf_rescued
	save_progress.mission_boss_elimated = mission_data_stat.boss_kill
	save_progress.mission_enemy_elimated = mission_data_stat.enemy_kill

	
	# collect all dynamic game elements	
	var saved_data:Array[SavedData] = []
	get_tree().call_group("game_events", "on_save_game", saved_data)
	
	# store them in the savegame
	save_progress.saved_data = saved_data
	
	# write the savegame to disk
	ResourceSaver.save(save_progress, "user://Save/Progress/Save_Progress.tres")
	print("Game saved successfully.")


# Function to load game data
func load_game():
	# fix any paths that may be broken after a game update
	var fixing_path = Path_Fixer.fix_paths("user://Save/Progress/Save_Progress.tres")
	print("Loading from ", fixing_path)
	
	# load the savegame securely
	var save_progress:Data_Progress = SafeResourceLoader.load(fixing_path) as Data_Progress
	if save_progress == null:
		return
	
	# first restore the level
	# this may take a few frames, so we wait with await
	await _world_root.load_level_async(save_progress.level_path)
	
	# clear the stage
	get_tree().call_group("game_events", "on_before_load_game")
	
	# restore player position
	play.global_position = save_progress.player_position
	# restore player name
	player_singleton.name_character = save_progress.player_name 
	#restore player biome location
	player_singleton.location = save_progress.player_biome_location
	
	# verify & restore player health
	player_singleton.health = min(save_progress.player_health, 100)
	# verify & restore player armor
	player_singleton.armor = min(save_progress.player_armor,100)
	# verify & restore player ammo
	play.bullet = min(save_progress.ammmo,30)
	# verify & restore player Mag
	play.magazine = min(save_progress.mag,150)
	
	# restore all dynamic game elements	
	for item in save_progress.saved_data:
		# skip over data we don't use anymore
		if item is UnusedData:
			continue
		
		# load the scene of the saved item and create a new instance
		var scene := load(item.scene_path) as PackedScene
		var restored_node = scene.instantiate()
		# add it to the world root
		_world_root.add_child(restored_node)
		# and run any custom load logic
		if restored_node.has_method("on_load_game"):
			restored_node.on_load_game(item)
	
	
func save_file_exist()->bool:
	return ResourceLoader.exists("user://Save/Progress/Save_Progress.tres")
	
func new_game():
	var save_progress: Data_Progress = Data_Progress.new()
	
	

Looks like you’re only converting it to Resource before accessing it. I don’t know if that has anything to do with it, but try if save is Data_Progress.

1 Like

need me to inisitate the Data_Progress class for that, sir?
im really never using resource class before

ahhh finaly working
thx yeah need to convert from the save resource file to data_progress

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.