Typing problem: What am I doing wrong?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By EdaoX

Hi everyone.

I’m trying to pass an instantiated object to a function, but the variable gets set to null when inside the body of that function:

func on_item_button_pressed( type : String ) -> void :
	var item : Item = ItemsManager.make_item(type)
    # Here the variable "item" is set, and is the desired node
	GlobalVariables.world.add_item_at_random_position(item)



func add_item_at_random_position( item : Item ) -> void :
    # Here, "item" is received as null
	add_item(item, get_random_navigation_map_point())

Fiddling with the code in confusion, I noticed that by not specifying the type in the parameter of add_item_at_random_position, it works as intended. To make myself clearer:

func add_item_at_random_position( item ) -> void :
    # This way, "item" here is set as that node as it should be
	add_item(item, get_random_navigation_map_point())

Any idea what I’m doing wrong? I tested with the debugger but couldn’t find anything wrong. The call stack is executed exactly as I posted, nothing in between. I feel like this might be some typing related bug.

Any help is very appreciated. Thanks in advance!

I’ve had similar problems, almost always dealing with custom classes. I think it’s best to just use a Godot base class. Maybe try “Node” instead?

nationality | 2019-05-13 04:12

Just like nationality said, it’s a problem with custom classes. I also had similar problems in other use cases with them. The best temporary fix I can think of is an if statement:

if item is Item:
    do stuff
else:
    throw error

Fenderbate | 2019-05-15 13:04