Invalid assignment of property or key 'itemhad' with value of type 'bool' on a base object of type 'Nil'

Godot Version

4.6

Question

Hello :slight_smile:

I’m sure I’m just forgetting a simple part, but ive been struggling with this for about two days now. I tried to make it so that an object would detect if the player has something in their inventory and only do what it can off of that, but whenever I try to collect the object now, it says “Invalid assignment of property or key ‘itemhad’ with value of type ‘bool’ on a base object of type ‘Nil’.”

I’m sure ive just forgotten something, but every forum post i can find like this doesn’t work for me. Does anyone know what i did wrong?

(I made itemhad a global variable here:

extends Resource

@export var itemhad: bool = false

And i know its detecting this code. The breakpoint is when i try to set it as true in the object script:

func interact(player: Player): ## pass in player
	print("interacted")
	player.inventory.add_item(held_item) ## new line, you must make `add_item` on your player
	Globals.itemhad = true
	queue_free()
	if Globals.itemhad != false:
		print("true")

And when i run debug, it crashes with the error when i try to interact with the object)

Your Globals is null, that’s very interesting. How is Globals defined? Is there any chance Globals is deleting itself such as queue_free()

1 Like
extends Resource

@export var itemhad: bool = false

The globals is that :slight_smile: i went into project settings and added it to that globals tab at the top, but the thing above is the whole script

I suspect you need to define it like this:

class_name Globals 
extends Resource

static var itemhad: bool = false
1 Like

How did you “made it a global variable”? Godot won’t allow setting up scripts that extend Resource as autoloads. They at least must extend Node. There should be an error message regarding this in the debugger on startup.

So either extend Node or make it a static variable in a named class as @that_duck suggested.

2 Likes

Alrighty so i added that :smiley: (had to change the class name since it said it hid a global singleton)

class_name globals

extends Resource
static var itemhad: bool = false

but it still has the same error D:

What is the error message?

ooooooh wait i see that now thank you

Do not add a class_name Globals.

Can you share your existing Globals script? The one you added as an Autoload?

Sorry I misunderstood, it may be better to extend from Node for your autoload. You also will not need the variable to be static nor @exported

This is it :slight_smile: although i think i might have to make it inherit a node?

extends Resource
@export var itemhad: bool = false

okay, ive made it inherit a node and changed the scripts to reflect that, now it says Invalid assignment of property or key ‘itemhad’ with value of type ‘bool’ on a base object of type ‘Node (globals2.gd)’.

I don’t think @export is appropriate for resources.

The error recognizes the script is attached, but fails to find the variable itemhad. are you sure itemhad is spelled correctly? Both in the declaration var and it’s use later with Globals.itemhad?

1 Like

THANK YOU SM <333333333 (im so mad at myself) i had to make it inheret a node but it wasnt spelt correctly haha

sometimes you just need to be called out on these things

Thank you :DDDDDDD