0 votes

why does my script think that global.openlevs is null?
enter image description here

here is the global script if it helps:
enter image description here

by the way when i remove the save and load functions the script works for some reason


Godot version 3.4
in Engine by (17 points)

Please copy and paste your code here.


onready var global = $"/root/Global"
onready var openlevss = global.openlevs

func ready() -> void:
for i in range($levels.get

for level in $levels.get_children():
    if str2var(level.name) in range(openlevss+1):
        level.disabled = false
        level.disabled = true


var openlevs = 1

var levels = []

var file_name = "user://saved.save"

func savegame():
var save
file = File.new()
savefile.open(filename, File.WRITE)

func loadgame():
var save
file = File.new()
if savefile.fileexists(filename):
var openlevs = save
return openlevs

func ready():
openlevs = load

1 Answer

0 votes
Best answer

I found that your save data type is not correct, so after function ready() runs, you will get a null.

Try this following

var openlevs = 1
var fileName = "res://save.save"

func _ready():
    if loadFile(): # check isn't null
        openlevs = loadFile()

func save():
    var saveFile = File.new()
    saveFile.open(fileName, File.WRITE)
    var saveDatas = {"openlevs": openlevs} # note: the data type

func loadFile():
    var loadFile = File.new()
    if loadFile.file_exists(fileName):
        loadFile.open(fileName, File.READ)
        var loadDatas = loadFile.get_var()
        return loadDatas["openlevs"] # return an int in this case

This example in my test is ok, hope this help.

by (526 points)
selected by

sorry it did not work
it still thinks the value is null

Have you set the "Global.gd" as Singleton (AutoLoad)?

yes i did

enter image description here

I found a new answer above maybe you can try this.

Thank you very much it finally worked

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.