Using save data

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By charliemacdmv

I have a (probably) stupid question.

I have finally created a save feature in my game. I followed basically every other tutorial out there on saves. (they’re all the same). The save feature is its own autoload script. I’m trying to now use that data/dictionary with an if statement on a different script and it keeps saying invalid index. I just need to see what a value is equal to, to determine text.
My save script is below. Can you only compare dictionaries to other dictionaries?

extends Node
const SAVE_FILE = "user://"
var g_data = {}

func _ready():

func save_data():
	var file =, File.WRITE)

func load_data():
	var file =
	if not file.file_exists(SAVE_FILE):
		g_data = {
			"exp_1": "Get Now",
			"exp_2": "Get Now",
			"exp_3": "Get Now",
			"exp_4": "Get Now",
			"exp_5": "Get Now"
	g_data = file.get_var()

Edited to fix code formatting.

jgodfrey | 2023-05-15 01:06

From your description, it’s not clear whether the error is in the posted code or in the code your refer to as the “different” script. The error will tell you exactly where the problem is in the script. Post the complete error message along with the source code it refers to.

jgodfrey | 2023-05-15 01:12

Thank you. I tried to fix it and it wouldn’t. I will post the error message in the morning.

charliemacdmv | 2023-05-15 02:40

When you add code to a post…

  • Paste the code
  • Select it (just the code)
  • Press the { } button to format it
  • Look at the Preview pane to verify proper formatting

jgodfrey | 2023-05-15 02:42

Here is the code…

extends Panel

onready var Button2000 = get_node("PanelContainer/ScrollContainer/VBoxContainer/HBoxContainer/2000s/Get2000s")
onready var save_file = SaveData.g_data

func _ready():
	Button2000.text = str(save_file.exp_1)

func _on_Get2000s_pressed():
	if save_file.exp_1 == 0:
	elif save_file.exp_1 == 1:
		save_file.exp_1 = 0
		Button2000.text = "Get Now"

func _on_AdMob_rewarded(currency, amount):
	save_file.exp_1 = 1
	Button2000.text = "Remove"

func _on_AdMob_rewarded_video_loaded():

And this is the error code… Invalid get index ‘exp_1’ (on base: Dictionary)

charliemacdmv | 2023-05-15 17:01

And the error points to this line.

func _ready():
    Button2000.text = str(save_file.exp_1)

charliemacdmv | 2023-05-15 17:02

The error should have also indicated the line and character position of the issue in your code. However, based on the error, I assume you must be attempting to access the exp_1 key of your dictionary BEFORE it’s actually been created or that the data you’re loading doesn’t contain the key. For whatever reason, the key doesn’t exist at the time you’re trying to access it.

Set a debugger break point on the line that causes the error and examine the contents of your g_data Dictionary there. I assume the problem will be obvious.

jgodfrey | 2023-05-15 17:11

Thank you @jgodfrey

Getting my head in the right spot helped. I deleted the save_file from the user folder and started over and it is working now.

Appreciate your time helping me with this!!

charliemacdmv | 2023-05-15 17:33