can save and load player loc/rot and held guns
This commit is contained in:
@@ -1,13 +1,22 @@
|
||||
extends Node
|
||||
|
||||
## SAVE DATA
|
||||
#Persistent
|
||||
#PERSISTENT DATA
|
||||
var last_hit_path
|
||||
var player_deaths
|
||||
var enemies_killed
|
||||
|
||||
#GAME DATA
|
||||
var player_loc
|
||||
var player_rot
|
||||
var held_guns
|
||||
var current_ammo
|
||||
var reserve_ammo
|
||||
|
||||
|
||||
## SAVE DATA PATHS
|
||||
var persistent_save_path = "user://persistent_data.save"
|
||||
var game_save_path = "user://game_save_data.save"
|
||||
|
||||
func _ready() -> void:
|
||||
if player_deaths == null:
|
||||
@@ -23,6 +32,8 @@ func save_persistent_data():
|
||||
print("SAVING PLAYER DEATHS " + str(player_deaths))
|
||||
file.store_var(enemies_killed)
|
||||
print("SAVING ENEMIES KILLED " + str(enemies_killed))
|
||||
|
||||
file.close()
|
||||
|
||||
func load_persistent_data():
|
||||
if FileAccess.file_exists(persistent_save_path):
|
||||
@@ -33,17 +44,60 @@ func load_persistent_data():
|
||||
print("PLAYER DEATHS : " + str(player_deaths))
|
||||
enemies_killed = file.get_var()
|
||||
print("ENEMIES KILLED : " + str(enemies_killed))
|
||||
|
||||
file.close()
|
||||
else:
|
||||
print("no data saved...")
|
||||
last_hit_path = null
|
||||
player_deaths = null
|
||||
enemies_killed = null
|
||||
|
||||
|
||||
func save_game_data():
|
||||
pass
|
||||
var level_control = get_tree().current_scene
|
||||
var player = level_control.player
|
||||
var file = FileAccess.open(game_save_path, FileAccess.WRITE)
|
||||
#ASSIGN DATA TO VARIABLES
|
||||
player_loc = player.global_position
|
||||
player_rot = player.global_rotation
|
||||
held_guns = save_resource_path(level_control.held_guns)
|
||||
current_ammo = level_control.ammo_current
|
||||
reserve_ammo = level_control.ammo_reserve
|
||||
|
||||
#SAVE DATA
|
||||
file.store_var(player_loc)
|
||||
file.store_var(player_rot)
|
||||
file.store_var(held_guns)
|
||||
file.store_var(current_ammo)
|
||||
file.store_var(reserve_ammo)
|
||||
|
||||
file.close()
|
||||
|
||||
func load_save_game_data():
|
||||
pass
|
||||
var level_control = get_tree().current_scene
|
||||
var player = level_control.player
|
||||
|
||||
if FileAccess.file_exists(game_save_path):
|
||||
var file = FileAccess.open(game_save_path, FileAccess.READ)
|
||||
|
||||
#GET DATA
|
||||
player_loc = file.get_var()
|
||||
player_rot = file.get_var()
|
||||
var held_guns_encoded = file.get_var()
|
||||
held_guns = load_resource_path(held_guns_encoded)
|
||||
current_ammo = file.get_var()
|
||||
reserve_ammo = file.get_var()
|
||||
|
||||
|
||||
#APPLY DATA
|
||||
player.global_position = player_loc
|
||||
player.global_rotation = player_rot
|
||||
level_control.held_guns = held_guns
|
||||
level_control.ammo_current = current_ammo
|
||||
level_control.ammo_reserve = reserve_ammo
|
||||
file.close()
|
||||
else:
|
||||
print("no data saved...")
|
||||
|
||||
func data_validate(file,variable):
|
||||
if file.get_var(variable):
|
||||
@@ -61,3 +115,22 @@ func persistent_data_calc(variable,amount):
|
||||
func load_data():
|
||||
load_persistent_data()
|
||||
load_save_game_data()
|
||||
|
||||
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)
|
||||
print("Resource Path: ", resource_path)
|
||||
print("FINAL ARRAY ", final_array)
|
||||
return final_array
|
||||
|
||||
func load_resource_path(array):
|
||||
var final_array = []
|
||||
for i in array:
|
||||
var i_loaded = load(i)
|
||||
final_array.append(i_loaded)
|
||||
print("loaded ",final_array)
|
||||
return final_array
|
||||
|
||||
Reference in New Issue
Block a user