0 votes

Hello ,

I just watched this tutorial Tutorial and followed everything he did to make inventory and item system.I swear i checked code thousand times already but still im getting same error invalid set index 'text' (on base 'null instance') with value of type 'string' and cant launch scene.

the code looks like this:

we have .json file
{
"Tree Branch": {
"ItemCategory": "Resource",
"StackSize": 99,
"Description": "A sturdy tree branch that can be used for crafting."
},
"Slime Potion": {
"ItemCategory": "Consumable",
"AddHealth": 5,
"AddEnergy": 32,
"StackSize": 99,
"Description": "It smells like medicine."
},
"Iron Sword": {
"ItemCategory": "Sword",
"ItemAttack": 3,
"ItemSpeed": 0.75,
"StackSize": 1,
"Description": "Quite a rusty sword, but should be able to get the job done."
},
}

we have json,gd script which is autoloaded

extends Node

var item_data: Dictionary

func ready():
item
data = LoadData ("res://Data/ItemData.json")

func LoadData(filepath):
var json
data
var file_data = File.new()

file_data.open(file_path, File.READ)
json_data = JSON.parse(file_data.get_as_text())
file_data.close()
return json_data.result

and we have item.gd which is giving me errors and wont open scenes because of that.

extends Node2D

var itemname
var item
quantity

func _ready():
#if randi() % 2 == 0:
#$TextureRect.texture = load("res://sprites/item/Iron Sword.png")
#else:
#$TextureRect.texture = load("res://sprites/item/Slime Potion.png")

var rand_val = randi() % 3
if rand_val == 0:
    item_name = "Iron Sword"
elif rand_val == 1:
    item_name = "Tree Branch"
else:
    item_name = "Slime Potion"

$TextureRect.texture = load("res://sprites/item/" + item_name + ".png")
var stack_size = int(JsonData.item_data[item_name]["StackSize"])
item_quantity = randi() % stack_size + 1

if stack_size == 1:
    $Label.visible = false
else:
    $label.text = String(item_quantity)

func additemquantity(amounttoadd):
itemquantity += amounttoadd
$Label.text = String(item
quantity)

func decreaseitemquantity(amounttoremove):
itemquantity -= amounttoremove
$Label.text = String(item
quantity)

so i get error and its always pointing to this line

else:
    $label.text = String(item_quantity)

As far I can understand about this , that somehow they didnt get info from .json file .
i tiped pass in that line and scene will opens .
ofc its suppose to show 3 items which is declared in .json file and show theyr amounts , but instead it shows all 3 items but the "Iron Sword" doesnt have amount .
any ideas what causes this problem and how can i fix it?

in Engine by (17 points)

2 Answers

+1 vote
Best answer

This:

 $label.text

should be:

$Label.text

(note the capital L)

by (10,894 points)
selected by

those misstakes always are the smallest .
thanks for you time man thats helped!

0 votes

hello, I have exactly the same problem on the same tutorial in the same place, I do not understand
if you find a solution could you tell me splease

by (21 points)

hello , if you getting the same error as me you need to change in item.gd this code

if stack_size == 1:
    $Label.visible = false
else:
    $Label.text = String(item_quantity)

to be exzact its $Label atleeast it was for me . just check if you didn put lower case L instead of capital L

Without seeing code, and the specific error, it's hard to say what the problem is...

Unrelated, but @karmo77 - your "question" should not have been posted as an answer to @cenka's original question. In the future, try to post something like that as 1) additional Comments, 2) post a brand new question, or 3) use the Ask a related question button.

Thanks.

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.