semi-idle-arpg/scripts/PlayerData.gd

46 lines
1.5 KiB
GDScript

# player save data resource to use with ResourceSaver
class_name PlayerData
extends Resource
@export var player_name := "Player"
@export var constitution := 1
@export var experience := 0.0
@export var level := 1.0
@export var base_health := 100
@export var base_resource := 100
@export var weapon_type: Globals.Weapon.WeaponType
@export var zone_duration := 0
# Confused on where this all goes...
# Let's assume the PlayerState is the node where we save the player data in game
var PlayerState: Node
var save_path := "user://player_data.tres" # <- tres is Text RESource
func save() -> void:
var data := PlayerData.new()
data.player_name = PlayerState.player_name
data.constitution = PlayerState.constitution
data.experience = PlayerState.experience
data.level = PlayerState.level
data.base_health = PlayerState.base_health
data.base_resource = PlayerState.base_resource
data.weapon_type = PlayerState.weapon_type
data.zone_duration = PlayerState.zone_duration
var error := ResourceSaver.save(data, save_path)
if error:
print("An error happened while saving data: ", error)
func load() -> void:
var data: PlayerData = load(save_path)
PlayerState.player_name = data.player_name
PlayerState.constitution = data.constitution
PlayerState.experience = data.experience
PlayerState.level = data.level
PlayerState.base_health = data.base_health
PlayerState.base_resource = data.base_resource
PlayerState.weapon_type = data.weapon_type
PlayerState.zone_duration = data.zone_duration