Update loading demos for 4.0 (#776)

This commit is contained in:
Jonathan Nicholl
2022-12-13 10:51:04 -05:00
committed by GitHub
parent 1b2ce74a39
commit 095beddcb9
133 changed files with 728 additions and 1703 deletions

View File

@@ -2,34 +2,36 @@ extends Button
# This script shows how to save data using the JSON file format.
# JSON is a widely used file format, but not all Godot types can be
# stored natively. For example, integers get converted into doubles,
# and to store Vector2 and other non-JSON types you need `var2str()`.
# and to store Vector2 and other non-JSON types you need to convert
# them, such as to a String using var_to_str.
# The root game node (so we can get and instance enemies).
## The root game node (so we can get and instance enemies).
@export var game_node: NodePath
# The player node (so we can set/get its health and position).
## The player node (so we can set/get its health and position).
@export var player_node: NodePath
const SAVE_PATH = "user://save_json.json"
func save_game():
var file = File.new()
file.open(SAVE_PATH, File.WRITE)
var file := FileAccess.open(SAVE_PATH, FileAccess.WRITE)
var player = get_node(player_node)
# JSON doesn't support complex types such as Vector2.
# `var2str()` can be used to convert any Variant to a String.
# JSON doesn't support many of Godot's types such as Vector2.
# var_to_str can be used to convert any Variant to a String.
var save_dict = {
player = {
position = var2str(player.position),
health = var2str(player.health),
position = var_to_str(player.position),
health = var_to_str(player.health),
rotation = var_to_str(player.sprite.rotation)
},
enemies = []
}
for enemy in get_tree().get_nodes_in_group("enemy"):
for enemy in get_tree().get_nodes_in_group(&"enemy"):
save_dict.enemies.push_back({
position = var2str(enemy.position),
position = var_to_str(enemy.position),
})
file.store_line(JSON.new().stringify(save_dict))
@@ -37,28 +39,26 @@ func save_game():
get_node(^"../LoadJSON").disabled = false
# `load()` is reserved.
func load_game():
var file = File.new()
file.open(SAVE_PATH, File.READ)
var json = JSON.new()
var file := FileAccess.open(SAVE_PATH, FileAccess.READ)
var json := JSON.new()
json.parse(file.get_line())
var save_dict = json.get_data()
var save_dict := json.get_data() as Dictionary
var player = get_node(player_node)
# JSON doesn't support complex types such as Vector2.
# `str2var()` can be used to convert a String to the corresponding Variant.
player.position = str2var(save_dict.player.position)
player.health = str2var(save_dict.player.health)
var player := get_node(player_node) as Player
# JSON doesn't support many of Godot's types such as Vector2.
# str_to_var can be used to convert a String to the corresponding Variant.
player.position = str_to_var(save_dict.player.position)
player.health = str_to_var(save_dict.player.health)
player.sprite.rotation = str_to_var(save_dict.player.rotation)
# Remove existing enemies and add new ones.
for enemy in get_tree().get_nodes_in_group("enemy"):
enemy.queue_free()
# Remove existing enemies before adding new ones.
get_tree().call_group("enemy", "queue_free")
# Ensure the node structure is the same when loading.
var game = get_node(game_node)
var game := get_node(game_node)
for enemy_config in save_dict.enemies:
var enemy = preload("res://enemy.tscn").instantiate()
enemy.position = str2var(enemy_config.position)
enemy.position = str_to_var(enemy_config.position)
game.add_child(enemy)