Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | InstelGames | |
Old Version | Published before Godot 3 was released. |
So I created two scenes named scene_a and scene_b Scene_a had a continue button which I just connected to a main scene_a script with just a simple code like this… func _on_continue_button_pressed():
get_tree.change_scene(“res://Scenes/scene_b.tscn”)
This made it so when I clicked the ContinueButton it changed to scene_b.
In scene_b I have a control node with a script, I added a player (KinematicBody2D) then I followed the Godot docs Saving tutorial, which I then got this code into my Control Node Script this is what the code looked like in the Control Node (Btw there are correct spacing it just didn’t load)
extends Control
func _ready():
set_process(true)
var savenodes = get_tree().get_nodes_in_group("Persist")
for i in savenodes:
pass
func _process(delta):
if Input.is_action_pressed("ui_left"):
get_node("player").move(Vector2(-5, 0))
func save():
var savedict = {
filename=get_filename(),
parent=get_parent(),
posx=get_pos().x,
posy=get_pos().y
}
return savedict
func save_game():
var savegame = File.new()
savegame.open("user://savegame.save", File.WRITE)
var savenodes = get_tree().get_nodes_in_group("Persist")
for i in savenodes:
var nodedata = i.save()
savegame.store_line(nodedata.to_json())
savegame.close()
func load_game():
var savegame = File.new()
if !savegame.file_exists("user://savegame.save"):
return
var savenodes = get_tree().get_nodes_in_group("Persist")
for i in savenodes:
i.queue_free()
var currentline = {} # dict.parse_json() requires a declared dict.
savegame.open("user://savegame.save", File.READ)
while (!savegame.eof_reached()):
currentline.parse_json(savegame.get_line())
# First we need to create the object and add it to the tree and set its position.
var newobject = load(currentline["filename"]).instance()
get_node(currentline["parent"]).add_child(newobject)
newobject.set_pos(Vector2(currentline["posx"],currentline["posy"]))
# Now we set the remaining variables.
for i in currentline.keys():
if (i == "filename" or i == "parent" or i == "posx" or i == "posy"):
continue
newobject.set(i, currentline[i])
savegame.close()
func _on_MenuButton_pressed():
get_tree().change_scene("res://Scenes/scene_a.tscn")
Then I wanted to make it so I could go back to the menu screen ( Just above )
then when I click on the continue button again I should reload my KinematicBody2D’s position right? ( I added a move function so I could see if it would reload it’s current pos ) But nothing happens. I don’t get any errors, just nothing. I’ve read the Docs many times and they explain it in a way that I can’'t really understand…
Well rip, anyone have any ideas?