Invalid get index 'bought' (on base: 'Nil'). WHY?

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

hey,

i’m doing a flappy bird game with shop.
buy button should switch to selected when I buy something but that doesn’t work. everytime when i click on “buy” it crashes.

character.gd code:

 extends Tabs


onready var price2 = str2var($RichTextLabel/Control/Panel2/Label.text)
onready var price3 = str2var($RichTextLabel/Control/Panel3/Label.text)
#onready var price4 = str2var($RichTextLabel/Control/Panel4/Label.text)
#onready var price5 = str2var($RichTextLabel/Control/Panel5/Label.text)
onready var panels = $RichTextLabel/Control

func _ready():
	pass # Replace with function body.

func _process(delta):
	$RichTextLabel/Control.position.y = -$VScrollBar.value
	
func _buy(price, item_no):
	GameManager.load_shop()
	if GameManager.points >= price:
		GameManager.points -= price
		GameManager.shop.bought[item_no] = true
		panels.get_node('Panel' + str(item_no)).get_node('Button').text = 'SELECT'
		GameManager.save_shop()
	else:
		var rem = price - GameManager.points
		$ColorRect/Label.text = 'You need '+str(rem)+'points \n to buy this'
		$ColorRect.show()


func _on_Button_pressed():
	_buy(price2, 1)


func _on_Button2_pressed():
	_buy(price3, 2)


func _on_ok_pressed():
	$ColorRect.hide()

GameManager.gd code:

 extends Node

var points = 0
var shop = {
			'bought' : [true, false, false, false, false]
}
var save_shop_path = 'user://save'


func save_shop():
	var file = File.new()
	file.open(save_shop_path, file.WRITE_READ)
	file.shop_var(shop)
	file.close()
	
func load_shop():
	var file = File.new()
	if not file.file_exists(save_shop_path):
		return false
	file.open(save_shop_path, file.READ)
	shop = file.get_var()
	file.close()
	return true

Label.gd code:

extends Label

export(int) var price

func _ready():
	text = str(price)

its my first godot project and so far everything has gone well

pic of godot with buttons, label etc: Screenshot-2021-01-07-at-20-08-08 hosted at ImgBB — ImgBB

i hope u can help me…

Provided that your item_no reference is valid, either of these are correct ways to access your dictionary.

GameManager.shop.bought[item_no] = true
GameManager.shop['bought'][item_no] = true

So, I don’t see a problem with that specific reference. Are you sure the error is in the code you posted?

jgodfrey | 2021-01-07 23:30

Yes, in:

GameManager.shop.bought[item_no] = true

so in character.gd is the error but i don’t understand why :frowning:

felaix | 2021-01-08 10:41

:bust_in_silhouette: Reply From: Lopy

The error message tells you that GameManager.shop is null. This likely comes from file.get_var() returning null, because it failed to find a var in the File, and indeed, you use file.shop_var(shop) instead of file.store_var(shop) when putting a value inside it.

i understand. thank you very much.

felaix | 2021-01-09 15:44

i renamed ALL in “store” now but i still get the same errors …

felaix | 2021-01-09 19:23

i copied all of code from the video and i get the errors:

E 0:00:01.585   decode_variant: Condition "len < 4" is true. Returned: ERR_INVALID_DATA
  <C++ Source>  core/io/marshalls.cpp:108 @ decode_variant()
  <Stack Trace> GameManager.gd:22 @ load_store()
                Character.gd:18 @ _ready()

and

E 0:00:01.585   get_var: Error when trying to encode Variant.
  <C++ Error>   Condition "err != OK" is true. Returned: Variant()
  <C++ Source>  core/bind/core_bind.cpp:2273 @ get_var()
  <Stack Trace> GameManager.gd:22 @ load_store()
                Character.gd:18 @ _ready()

i can’t understand this and i’m thinking about to end this project -.-

felaix | 2021-01-09 19:40