Using Godot 3.1, I've written this script for diamonds that the player collects:
extends Area2D
const save_location = "user://data.json"
var details = {
"diamond": 0,
"life": 0,
}
func _on_diamondfinal_body_entered(body):
if body.name == "player":
loading()
func loading():
var file = File.new()
if file.file_exists(save_location):
file.open(save_location, File.READ)
var data = parse_json(file.get_as_text())
details = data
details.diamond += 1
saving()
queue_free()
func saving():
var file = File.new()
file.open(save_location, File.WRITE)
file.store_string(to_json(details))
file.close()
And I have written this script for the "level one" scene:
extends Node2D
const save_location = "user://data.json"
var details = {
"diamond": 0,
"life": 0,
}
func _process(delta):
loading()
$CanvasLayer/Panel/diamond_label.text = str(details.diamond)
$CanvasLayer/Panel/life_label.text = str(details.life)
func loading():
var file = File.new()
if file.file_exists(save_location):
file.open(save_location, File.READ)
var data = parse_json(file.get_as_text())
details = data
I have two problems:
When I play the "level one" scene, I collect diamonds, but their number does not increase in the scene and it remains at 00.
In level one, there are 20 diamonds. How can I set it so that when I go back into the "level one" scene, the diamonds I have collected will not be displayed again?
That means, no matter how many times the player loses and returns to this scene, only the diamonds that they have not already collected will be displayed.