-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveGame.gd
More file actions
53 lines (37 loc) · 1.08 KB
/
SaveGame.gd
File metadata and controls
53 lines (37 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
extends Node2D
var SAVE_DIR = "user://saves/"
var SAVE_FILE_NAME = "PlayerSave.tres"
var player_data = PlayerData.new()
func _ready():
verify_save_directory(SAVE_DIR)
func verify_save_directory(path: String):
DirAccess.make_dir_absolute(path)
func save_data(path:String):
var file = FileAccess.open(path,FileAccess.WRITE)
if file == null:
print(FileAccess.get_open_error())
return
var data = {
"global.position":{
"x":player_data.global_position.x,
"y":player_data.global_position.y
}
}
file.close()
func load_data(path:String):
if FileAccess.file_exists(path):
var file = FileAccess.open(path,FileAccess.READ)
if file == null:
print(FileAccess.get_open_error())
return
var content = file.get_as_text()
file.close()
player_data = PlayerData.new()
else:
printerr("Cannot open non existant file")
func _process(delta: float) -> void:
pass
func _on_save_pressed() -> void:
ResourceSaver.save(PlayerData, SAVE_DIR + SAVE_FILE_NAME)
func _on_load_pressed() -> void:
player_data = ResourceLoader.load(SAVE_DIR + SAVE_FILE_NAME).duplicate(true)