Checking if variable of type Resource holds an actual Resource or just its basic state(idk what its caled)

Godot Version

4.6

Question

Hi, sorry if it was already answerd. So I am trying to make a preview for a model made from smaler parts.

func reset_weapon(part_list):
for i in weapon.get_children():
i.queue_free()
weapon.remove_child(i)

for i in part_list.size():
	print(part_list[i].item)
	if part_list[i].item == null:
		pass
	else:
		var instance = part_list[i].item.model.instantiate()
		weapon.add_child(instance)
		if part_list[i].part_slot_parent == "Base":
			pass
		else:
			instance.position = part_list[i].part_slot_parent  + part_list[i].item.position

Problem is that part_list holds slots for parts even if they are empty(its important to be like that),

so when code gets to the slot that doesnt have any item it treats it as base of resource for parts

and crashes with error:

WeaponPreview.reset_weapon: Invalid access to property or key ‘model’ on a base object of type ‘GDScript’.

So i thought about checking if it holds any item before but both cheking for null and for resource do not work.

Any ideas how to do this correctly? Thanks.

Post the item class code.

Item Class code

class_name ItemData
extends Resource

@export var name: String
@export var description:String
@export var model:PackedScene
@export var icon:Texture

and every slot in part list looks like this:

var slot = {
	"item":ItemData,
	"part_slot_type": type,
	"part_slot_parent":slot_parent
}

According to the error message it look like you didn’t assign an of type ItemData there, but instead you just assigned the class.

Can you show the code where you fill part_list?

So the part list is just sent by signal and those two function fill them

this one creates empty slots:

func add_part_slot(type, slot_parent):
	var slot = {
		"item":ItemData,
		"part_slot_type": type,
		"part_slot_parent":slot_parent
	}
	slot.item == null
	slot.part_slot_type = type
	slot.part_slot_parent = slot_parent
	
	if weapon_part_list.is_empty():
		weapon_part_list.insert(0, slot)
	else:
		weapon_part_list.insert(weapon_part_list.size(), slot)
		

and this one ads item to empty slots:

func add_attachment(slot, item_to_add):
	for i in range(weapon_part_list.size()):
		if weapon_part_list[i] == slot:
			weapon_part_list[i].item = item_to_add
			
	##adding new attachment slots from part
	for i in item_to_add.attachment_slots:
		add_part_slot(i.type, i)
	
	reset_weapon_prewiev.emit(weapon_part_list)
	

You assign ItemData class to “item”, which you shouldn’t do in the first place. Note that using a semicolon it the dictionary doesn’t do a type declaration. Inside the dictionary definition it works as an assignment, so “item” value will now be a reference to the GDScript object that represents the class, exactly as reported by the error message.

Then you later attempt to override it with null. This would be fine but you don’t really override it because you mistakenly used the equality operator == instead of the assignment operator =:

slot.item == null

This does nothing. It doesn’t assign null. Try changing it to:

slot.item = null

In fact you don’t need any of the assignmnt after you define the slot dictionary. Simply do:

var slot = {
	"item": null,
	"part_slot_type": type,
	"part_slot_parent": slot_parent
}

Thank you so much, I sat at it for so long. In other engines it would be shown as error so I didnt pay any attention to it

It wouldn’t be shown as an error in any language that has == and = operators.

Im pretty sure that in unity i did similar thing and it was marked as error but mayby I remember it wrong. Anyway thank you so much

C# has the == operator. Doing standalone expression that have no effect will be syntactically correct in either C# or GDScript. However the script editor can be set up to issue a warning if it encounters such a line. I think Godot in fact issues this warning by default for such expressions:

image

yeah you are right it was hidden in a wall of other warnings that I didnt hide