Attention | Topic was automatically imported from the old Question2Answer platform. | |
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
felaix | 2021-01-08 10:41