I can’t figure out how to load files from a save. When I run the load_game() function, I get the error “Invalid set index ‘texture_normal’ (on base: ‘TextureButton’) with value of type ‘String’.”
extends Node2D
@onready var TextureNode = get_node("TextureButton")
func _ready():
load_game()
get_tree().get_root().files_dropped.connect(_on_files_dropped)
func _on_files_dropped(files):
var path = files[0]
var image = Image.new()
image.load(path)
var image_texture = ImageTexture.new()
image_texture.set_image(image)
if image_texture == null:
#TextureNode.texture_normal = "res://backgroundvs2.png"
pass
TextureNode.texture_normal = image_texture
save_game(image_texture)
func _on_file_dialog_file_selected(path):
var image = Image.new()
image.load(path)
var image_texture = ImageTexture.new()
image_texture.set_image(image)
if image_texture == null:
#TextureNode.texture_normal = "res://backgroundvs2.png"
pass
TextureNode.texture_normal = image_texture
save_game(image_texture)
func _on_texture_button_pressed():
$FileDialog.popup()
func save_game(image_texture):
var file = FileAccess.open("res://save.txt", FileAccess.WRITE)
var saved_data = {}
saved_data["texture"] = image_texture
var json = JSON.stringify(saved_data)
file.store_string(json)
file.close()
print("saving")
func load_game():
print("loading")
var file = FileAccess.open("res://save.txt", FileAccess.READ)
var json = file.get_as_text()
var saved_data = JSON.parse_string(json)
if saved_data != null:
TextureNode.texture_normal = saved_data["texture"]
file.close
I don’t really use GDScript, but from what I can see, you’re trying to save the actual image data as text, which I’m not sure would be ideal in this case. I would try and save the image the user picked, and put it in the AppData folder, and only save the path to said image inside the AppData folder, instead of the actual image data.
The property texture_normal is of Texture2D type, so you cannot assign a JSON string to it. It would be better to save the whole image to user://, then load the image.
OK, I’ve changed the code, and now I’m not getting any errors, but when I save the texture variable it prints as null, and when I load the var the TextureButton loses its texture. Also the “on_file_dialog_file_selected” function I’m kind of ignoring right now, so no need to pay attention to it. Thanks for the replies.
extends Node2D
@onready var TextureNode = get_node("TextureButton")
var save_path = "user://image.save"
func _ready():
get_tree().get_root().files_dropped.connect(_on_files_dropped)
func _on_files_dropped(files):
var path = files[0]
var image = Image.new()
image.load(path)
var image_texture = ImageTexture.new()
image_texture.set_image(image)
if image_texture == null:
#TextureNode.texture_normal = "res://backgroundvs2.png"
pass
TextureNode.texture_normal = image_texture
save_game()
func _on_file_dialog_file_selected(path):
var image = Image.new()
image.load(path)
var image_texture = ImageTexture.new()
image_texture.set_image(image)
if image_texture == null:
#TextureNode.texture_normal = "res://backgroundvs2.png"
pass
TextureNode.texture_normal = image_texture
save_game()
func _on_texture_button_pressed():
$FileDialog.popup()
func save_game():
var file = FileAccess.open(save_path, FileAccess.WRITE)
var imagething = TextureNode.texture_normal
file.store_var(imagething)
file.close()
print("saving")
func load_game():
print("loading")
if FileAccess.file_exists(save_path):
var file = FileAccess.open(save_path, FileAccess.READ)
TextureNode.texture_normal = file.get_var()
print(file.get_var())
else:
print("no file found")
func _on_button_pressed():
load_game()
Thanks for the reply, I’ve changed the script so that it stores the variable and doesn’t save it as text. I’m not actually sure how to save it to the AppData folder, I’m saving it to user:// currently.