I am working on making a banking UI for my game and when run it gives me the error
Invalid set index ‘visible’ (on base: ‘null instance’) with value of type ‘bool’.
which is weird because i have the exact same code for making things visible else where in the game and that works fine. this code also has a error with displaying text as strs which I also have done at other times
thanks for any help in advance
Here is the code
extends Area2D
var gold = Inventory.gold
var bankGold = Inventory.bankGold
I’m thinking the node may have a different name when you reference it with the $ or % syntax. Double check that, this is the most likely reason.
Maybe the _on_body_entered fires before the entire scene is ready. So the CanvasLayer isn’t in the scene yet and you are already trying to set its visibility?
Also I’d recommend to extract the nodes you are referencing into onready variables like this:
@onready var start_screen: Control = %CanvasLayer/startScreen
And then in _ready you can check if they are not null. Just to be safe.
thanks for that you where right it was the wrong name
Btw if I was wondering if you knew of a way to make a label display a int that’s are var and update as the var gets changed
I have this code here
gold and bank gold are vars in an auto load script it loads on start but doesn’t update with the vars
var gold = Inventory.gold
var bankGold = Inventory.bankGold
func _process(delta):
%ColorRect/gold.text = str(gold)
%ColorRect/bankGold.text = str(bankGold)
For a new project updating the label every frame is fine. Once you start having a lot of UI elements, i tend to go for this approach:
Have a GameState autoload (singleton)
The GameState has a should_update_ui signal, which it emits every time a displayed property changed
The UI (CanvasLayer maybe?) connects to this signal and updates only when necessary
But don’t worry about it too much. A single label is fine. When you have a lot of UI elements, some solution will emerge. Focus on your game firt and good luck!
That’s because you only get the values from your Autoload once (outside the scope of your _process function, i.e. when the script is loaded). If you define the variables in _process (i.e. every frame anew), the labels should update correctly:
func _process(delta):
var gold = Inventory.gold
var bankGold = Inventory.bankGold
%ColorRect/gold.text = str(gold)
%ColorRect/bankGold.text = str(bankGold)
or shorter, by using the Autoloads variables directly: