This script resource did not evaluate as null, but .get_global_name() disagrees

Godot Version

Godot 4.3.stable

Question

G’day!

I’m working on an editor plugin to make developing my game easier/faster, which includes a bunch of custom node types I’ve made with the add_custom_type() method. I only really care about these custom nodes, and different custom nodes will be given to different objects which manage them, so when _handles() is called i quickly check if that node has a script attached to it (as all custom nodes do) and then check of that script resource exists in any dictionaries associated with those different manager objects. here is the code:

func _handles(object: Object) -> bool:
	var objScript : Script = object.get_script()
	if(objScript != null && 
			ChunkRef.has(objScript.get_global_name()) 
		 || ToolRef.has(objScript.get_global_name())):
		return true
	else:
		return false

In theory, if objScript returned null it shouldn’t evaluate those next two conditions, but I select a standard node in the editor (I’m using control nodes in this case) I’m getting the error:

res://addons/my_addon/the_script_im_using.gd:36 - Cannot call method 'get_global_name' on a null value.

And that ‘if’ statement is on line 36, so I’m sure that’s what its referring to. Is the ‘objScript != null’ check incorrect somehow? Or do evaluations not happen in order like this? Most of my programming experience is in C and C++ where an ‘if’ statement won’t evaluate Y after X if X evaluates as false in ‘if(X && Y)’.

This isn’t breaking anything, but I would like to understand exactly what is happening a little better. Any help would br greatly appreciated! :slight_smile:

try separate this to first check if it’s null or not, then the following conditions

1 Like

Good thinking. This doesn’t return the same error:

func _handles(object: Object) -> bool:
	var objScript : Script = object.get_script()
	if(objScript == null):
		return false 
	elif(  ChunkRef.has(objScript.get_global_name()) 
		|| ToolRef.has(objScript.get_global_name())):
		return true
	else:
		return false

I guess ‘if’ statments just aren’t handles in the same way as C! Thank you

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.