Files
fps_project_1/scripts/save_load.gd

231 lines
6.5 KiB
GDScript

extends Node
## SAVE DATA
#PERSISTENT DATA
var last_hit_path
var player_deaths = 0
var enemies_killed = 0
var shots_fired = 0
#GAME DATA
var data_cleared
var player_loc
var player_rot
var player_health
var player_velocity_cache
var current_gun
var current_ammo
var reserve_ammo
## SAVE DATA PATHS
var persistent_save_path = "user://persistent_data.save"
func _ready() -> void:
SignalBus.shot_fired.connect(shot_fired)
if player_deaths == null:
player_deaths = 0
if enemies_killed == null:
enemies_killed = 0
func save_persistent_data():
var level_control = get_tree().current_scene
var player = level_control.player
var file = FileAccess.open(persistent_save_path, FileAccess.WRITE)
print("LAST HIT PATH " + str(last_hit_path))
file.store_var(last_hit_path)
file.store_var(player.velocity)
file.store_var(get_tree().current_scene.scene_file_path)
file.store_var(get_tree().current_scene.gamemode.resource_path)
file.store_var(GameGlobals.money)
file.store_var(GameGlobals.deposited_money)
file.store_var(GameGlobals.health)
file.store_var(GameGlobals.high_score)
file.store_var(player_deaths)
file.store_var(enemies_killed)
file.store_var(shots_fired)
var held_guns = save_resource_path(GameGlobals.held_guns)
file.store_var(held_guns)
file.store_var(GameGlobals.gun_ammo)
file.store_var(GameGlobals.ammo_reserve)
file.store_var(GameGlobals.current_gun_index)
file.store_var(data_cleared)
file.close()
func load_persistent_data():
var level_control = get_tree().current_scene
if FileAccess.file_exists(persistent_save_path):
var file = FileAccess.open(persistent_save_path, FileAccess.READ)
last_hit_path = file.get_var()
player_velocity_cache = file.get_var()
GameGlobals.current_level = str(file.get_var())
GameGlobals.current_gamemode = file.get_var()
GameGlobals.money = set_nulls_zero(file.get_var())
GameGlobals.deposited_money = set_nulls_zero(file.get_var())
GameGlobals.health = file.get_var()
GameGlobals.high_score = file.get_var()
player_deaths = set_nulls_zero(file.get_var())
enemies_killed = set_nulls_zero(file.get_var())
shots_fired = set_nulls_zero(file.get_var())
var held_guns_encoded = file.get_var()
GameGlobals.held_guns = load_resource_path(held_guns_encoded)
GameGlobals.gun_ammo = file.get_var()
GameGlobals.ammo_reserve = file.get_var()
GameGlobals.current_gun_index = file.get_var()
if GameGlobals.current_gun_index != null:
if !get_tree().current_scene.is_in_group("ui"):
get_tree().current_scene.gun_spawn(GameGlobals.current_gun_index)
data_cleared = file.get_var()
file.close()
else:
print("no data saved...")
last_hit_path = null
player_deaths = null
enemies_killed = null
func save_game_data(level_name):
var level_control = get_tree().current_scene
var player = level_control.player
var game_save_path = str("user://",level_name,"_gamesave.save")
var file = FileAccess.open(game_save_path, FileAccess.WRITE)
#ASSIGN DATA TO VARIABLES
data_cleared = false
player_loc = player.global_position
player_rot = player.global_rotation
#save enemies
var objects = get_tree().get_nodes_in_group("persist")
for object in objects:
if object.scene_file_path.is_empty():
print("persistent node '%s' is not an instanced scene, skipped" % object.name)
continue
# Check the node has a save function.
if !object.has_method("save"):
print("persistent node '%s' is missing a save() function, skipped" % object.name)
continue
var object_data = object.call("save")
var json_string = JSON.stringify(object_data)
file.store_line(json_string)
file.close()
save_persistent_data()
func clear_save_game_data():
data_cleared = true
save_persistent_data()
func clear_persistent_data():
var file = FileAccess.open(persistent_save_path, FileAccess.WRITE)
file.store_var(null)
file.store_var(null)
file.store_var(null)
file.store_var(null)
file.store_var(null)
print("PERSISTENT DATA CLEARED")
file.close()
func check_save_game_exists(level_name):
var game_save_path = str("user://",level_name,"_gamesave.save")
if FileAccess.file_exists(game_save_path):
return true
else:
return false
func load_save_game_data(level_name):
var level_control = get_tree().current_scene
var player = level_control.player
var game_save_path = str("user://",level_name,"_gamesave.save")
if FileAccess.file_exists(game_save_path):
var file = FileAccess.open(game_save_path, FileAccess.READ)
#GET DATA
if !data_cleared:
var current_nodes = get_tree().get_nodes_in_group("persist")
for i in current_nodes:
if i.is_in_group("enemy"):
i.queue_free()
else:
i.queue_free()
while file.get_position() < file.get_length():
var json_string = file.get_line()
var json = JSON.new()
var parse_result = json.parse(json_string)
if not parse_result == OK:
print("JSON Parse Error: ",json.get_error_message()," in ",json_string, " at line ", json.get_error_line())
continue
var node_data = json.data
var new_object = load(node_data["filename"]).instantiate()
get_node(node_data["parent"]).add_child(new_object)
#apply transform if available
if node_data["pos_x"] != null:
new_object.position = Vector3(node_data["pos_x"],node_data["pos_y"],node_data["pos_z"])
if node_data["rot_x"] != null:
new_object.rotation = Vector3(node_data["rot_x"],node_data["rot_y"],node_data["rot_z"])
#apply other save data
for i in node_data.keys():
if i == "filename" or i == "pos_x" or i == "pos_y" or i == "pos_z":
continue
new_object.set(i,node_data[i])
file.close()
await get_tree().create_timer(1).timeout #need to fix this
SignalBus.emit_signal("game_loaded")
GameGlobals.game_loaded = true
else:
print("no data saved...")
func data_validate(file,variable):
if file.get_var(variable):
return file.get_var(variable)
else:
return null
func set_nulls_zero(variable):
if variable == null:
return 0
else:
return variable
func null_data_check(variable,amount): #checks if value is null, adds the number to variable
if variable == null:
variable = amount
else:
variable += amount
return variable
func save_resource_path(array):
var final_array = []
for i in array:
if i is PackedScene:
var resource_path = i.resource_path
if resource_path != "":
final_array.append(resource_path)
return final_array
func load_resource_path(array):
if array != null:
var final_array = []
for i in array:
var i_loaded = load(i)
final_array.append(i_loaded)
return final_array
else:
return []
func shot_fired():
shots_fired += 1 #null_data_check(shots_fired, 1)