0 votes

I create an object through my_object.new() and then add it to an array. This array is then saved to a savefile to then be loaded. When I load the savefile, the object turns up as null in the array. Is this something I'm doing wrong? Thx in advanced

Save/load functions:

unc saveGame():
print(str(party))
var savedScene = get_tree().current_scene.filename
var savedPos = get_tree().get_nodes_in_group("Player")[0].position
var data = {
    "party": party,
    "inventory": inventory,
    "equipment": equipment,
    "keyItems": keyItems,
    "gold": gold,
    #"questList": AllQuests.questArray,
    "currentQuests": currentQuests,
    "completedQuests": completedQuests,
    "currentScene": savedScene,
    "currentPos": savedPos,


}
var file = File.new()
file.open(savePath,File.WRITE)
file.close()

var error = file.open(savePath,File.WRITE)
if error == OK:
    file.store_var(data)
    file.close()

print("Saved")

func loadGame():
var loadedData
var file = File.new()
if file.file_exists(savePath):
    var error = file.open(savePath,File.READ)
    if error == OK:
        loadedData = file.get_var()
        print(str(loadedData))
        yield(get_tree().create_timer(1),"timeout")
        file.close()
        parseData(loadedData)
        SceneChanger.changeScene(loadedData.currentScene,0)

print("loaded")

parseData(loadedData)

func parseData(data):
clearData()
for id in data.party:
    party.append(instance_from_id(id.object_id))
    print(str(instance_from_id(id.object_id)))
for id in data.inventory:
    inventory.append(instance_from_id(id.object_id))
for id in data.equipment:
    equipment.append(instance_from_id(id.object_id))
for id in data.keyItems:
    keyItems.append(instance_from_id(id.object_id))
gold = data.gold
for id in data.currentQuests:
    currentQuests.append(instance_from_id(id.object_id))
for id in data.completedQuests:
    completedQuests.append(instance_from_id(id.object_id))
previousPos = data.currentPos
Godot version 3.3.2
in Engine by (16 points)

1 Answer

0 votes

I seem to have found my own answer which is that what is being saved is not the object itself but a point in memory to where the object exists. Of course when opening a new instance of the game, that object no longer exists.

Using inst2dict() to save the objects as a dictionary gets around this issue.

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.