Skip to content

Commit

Permalink
Add save and load node, part of #62
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanLovato committed Oct 14, 2018
1 parent e3439a6 commit 1fffd26
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 0 deletions.
26 changes: 26 additions & 0 deletions interface/menus/save_and_load/LoadButton.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[gd_scene format=2]

[node name="LoadButton" type="Button"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_right = 200.0
margin_bottom = 62.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
focus_mode = 2
mouse_filter = 0
mouse_default_cursor_shape = 0
size_flags_horizontal = 3
size_flags_vertical = 3
toggle_mode = false
enabled_focus_mode = 2
shortcut = null
group = null
text = "Load ID 1"
flat = false
align = 1


26 changes: 26 additions & 0 deletions interface/menus/save_and_load/SaveButton.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[gd_scene format=2]

[node name="SaveButton" type="Button"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_right = 200.0
margin_bottom = 62.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
focus_mode = 2
mouse_filter = 0
mouse_default_cursor_shape = 0
size_flags_horizontal = 3
size_flags_vertical = 3
toggle_mode = false
enabled_focus_mode = 2
shortcut = null
group = null
text = "Save ID 1"
flat = false
align = 1


7 changes: 7 additions & 0 deletions interface/menus/save_and_load/SaveUI.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends Control

func get_save_buttons():
return $Row/SaveButtons.get_children()

func get_load_buttons():
return $Row/LoadButtons.get_children()
97 changes: 97 additions & 0 deletions interface/menus/save_and_load/SaveUI.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
[gd_scene load_steps=5 format=2]

[ext_resource path="res://interface/theme/default.theme" type="Theme" id=1]
[ext_resource path="res://interface/SaveUI.gd" type="Script" id=2]
[ext_resource path="res://interface/SaveButton.tscn" type="PackedScene" id=3]
[ext_resource path="res://interface/LoadButton.tscn" type="PackedScene" id=4]

[node name="SaveUI" type="Control" index="0"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_right = 448.0
margin_bottom = 160.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 0
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 1
theme = ExtResource( 1 )
script = ExtResource( 2 )
_sections_unfolded = [ "Theme" ]

[node name="Row" type="HBoxContainer" parent="." index="0"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 32.0
margin_top = 32.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 1
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 1
custom_constants/separation = 16
alignment = 0
_sections_unfolded = [ "custom_constants" ]

[node name="SaveButtons" type="VBoxContainer" parent="Row" index="0"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_right = 200.0
margin_bottom = 128.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 1
mouse_default_cursor_shape = 0
size_flags_horizontal = 3
size_flags_vertical = 1
alignment = 1

[node name="SaveButton" parent="Row/SaveButtons" index="0" instance=ExtResource( 3 )]

margin_bottom = 56.0

[node name="SaveButton2" parent="Row/SaveButtons" index="1" instance=ExtResource( 3 )]

margin_top = 72.0
margin_bottom = 128.0
text = "Save ID 2"

[node name="LoadButtons" type="VBoxContainer" parent="Row" index="1"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_left = 216.0
margin_right = 416.0
margin_bottom = 128.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 1
mouse_default_cursor_shape = 0
size_flags_horizontal = 3
size_flags_vertical = 1
alignment = 1

[node name="LoadButton" parent="Row/LoadButtons" index="0" instance=ExtResource( 4 )]

margin_bottom = 56.0

[node name="LoadButton3" parent="Row/LoadButtons" index="1" instance=ExtResource( 4 )]

margin_top = 72.0
margin_bottom = 128.0
text = "Load ID 2"


71 changes: 71 additions & 0 deletions save/SaveAndLoad.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
extends Node

signal game_saved(id)
signal game_loaded(id)

onready var GAME_VERSION = ProjectSettings.get_setting("application/config/version")
var SAVE_DIRECTORY = "res://save/" if ProjectSettings.get_setting("application/config/debug") else "res://user/"
const SAVE_FILE_EXT = ".game"

func save_game(id):
var save_data = {
"version": GAME_VERSION,
"data": []
}
# Get nodes in group works in tree order, from top to bottom
# So we are guaranteed to get the parent first
for node in get_tree().get_nodes_in_group("save"):
var node_save_data = node.get_save_data()
assert node_save_data["filename"] != ""
assert node_save_data["parent"] != ""
save_data["data"].append(node_save_data)

var directory = Directory.new()
if not directory.dir_exists(SAVE_DIRECTORY):
directory.make_dir_recursive(SAVE_DIRECTORY)

var path = get_save_file_path(id)
var file = File.new()
file.open(path, File.WRITE)
file.store_string(to_json(save_data))
file.close()
emit_signal("game_saved", id)

func load_game(id):
var path = get_save_file_path(id)
var file = File.new()
if not file.file_exists(path):
print("Couldn't load: file %s does not exist." % path)
return
file.open(path, File.READ)
var save_data = parse_json(file.get_as_text())
file.close()

for node in get_tree().get_nodes_in_group("save"):
node.queue_free()

for node_data in save_data["data"]:
var node = load(node_data["filename"]).instance()
get_node(node_data["parent"]).add_child(node)
var properties = node_data["properties"]
for property in properties.keys():
var value
if is_vector_2(property, node):
value = parse_vector_2(properties[property])
else:
value = properties[property]
node.set(property, value)
emit_signal("game_loaded", id)

func get_save_file_path(id):
return SAVE_DIRECTORY + str(id) + SAVE_FILE_EXT

func is_vector_2(property_name, node):
return typeof(node.get(property_name)) == typeof(Vector2())

func parse_vector_2(json_string):
var numbers_string = json_string.substr(1, len(json_string) - 2)
var comma_position = numbers_string.find(",")
var value_x = float(numbers_string.left(comma_position))
var value_y = float(numbers_string.right(comma_position + 1))
return Vector2(value_x, value_y)
9 changes: 9 additions & 0 deletions save/SaveAndLoad.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=2]

[ext_resource path="res://SaveAndLoad.gd" type="Script" id=1]

[node name="SaveAndLoad" type="Node" index="0"]

script = ExtResource( 1 )


0 comments on commit 1fffd26

Please sign in to comment.