If statement identifier does not exist in scope

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

I’m trying to use a function parameter as an identifier in an if statement.

func dict_from_children(node : Node, child_type := Object, dict := {}):
for child in node.get_children():
	if child is child_type:
		dict[child.name] = child

Doing so gives me this error:

Could not find type "child_type" in the current scope.

It does the same thing when I remove the if statement from the for loop. It also does the same thing when I replace

if child is child_type:


if child is dict:

(only, of course, giving me the same error for “dict” instead)

In case it’s relevant, this function doesn’t exist inside a defined class. It’s just in its own script.

Anyone know why it doesn’t recognize function parameters as identifiers?

:bust_in_silhouette: Reply From: zhyrin

You cannot pass a type as a parameter. child_type will be an instance of an object, or null.
However, you can pass the Script of your expected type and check against that.
In the answers here I give two different but similar solutions: https://forum.godotengine.org/147024/type-type-hint

There is also this built-in function that supposedly does the same: @GDScript — Godot Engine (stable) documentation in English

:bust_in_silhouette: Reply From: godot_dev_

If the indentation used in this post matches the indentation of you project, the issue is that your function’s code has an indentation issue. Because it’s not indented, the engine doesn’t recognized it as part of the dict_from_children function, and that is why it can’t find the local variables you mention.

Add an indent to each line of your function