Is there any way to report missing property/functions when trying to access them on a object instance?

Godot Version

Godot 4.6

Question

I would like to trigger editor warnings/errors for code such as:

 # assume Structure is a correctly defined class_name

func assign_sleeping_structure(p_structure: Structure):
	p_structure.nonexistent_func() # <-

This code doesn’t trigger warnings/errors in the editor but I would like Godot to report them, is there any option available?
Currently Godot triggers the error only at runtime:

Invalid call. Nonexistent function ‘nonexistent_func’ in base ‘Node3D (Structure)’.

Actually I remember previous Godot versions were able to report these kind of issues, but maybe memory deceives me.

Enable “Unsafe Method Access” warning (or error) in Project Settings > Debug > GDScript. There’s an analogous warning/error for properties too.

1 Like

Thanks @normalized that was exactly what I was looking for!
I guess I’ll need to improve typings across my game. I wento from 0 warnings to 70+:

1 Like

Just a followup question: now I get warnings even for code like this where I didn’t expect them:

extends RayCast3D

func _physics_process(_delta: float) -> void:
	var mouse_pos = get_viewport().get_mouse_position()
	if is_colliding():
		var col = get_collider()
		if col is StackArea: # StackArea is a class_name with `on_mouse_enter`
			col.on_mouse_enter() # <- warning triggered here

The triggered warning is

W 0:00:00:354 GDScript::reload: The method “on_mouse_enter()” is not present on the inferred type “Variant” (but may be present on a subtype).

UNSAFE_METHOD_ACCESS

The only workaround that I found is to cast the type of the “col” variable like

(col as StackArea).on_mouse_enter()

However this seems redundant to me. Is there any PR working to improve warning reports in such cases? Or is it a better way to handle this kind of type casts?

Cast or declare col as typed.

Yes but that should be redundant in cases such as:

if col is StackArea:
	col.on_mouse_enter()

As the is keyword is already enforcing the type, no?

Depends what you mean by “enforcing”. It checks the type at runtime but it doesn’t make the variable typed at compile time. That wouldn’t make much sense as you can have situations like this:

if col is StackArea or col is SomethingElseEntirely:
	col.on_mouse_enter()

You need to explicitly declare the type, otherwise the compiler cannot know it so it cannot check its methods.

1 Like

Makes sense I guess, thanks again!

In the end I declared a function that accepts the custom type, so that the casting is implicit in the function call and no warning is triggered:

func _physics_process():
   var col = get_collider()
   if col is StackArea:
  	_handle_stack_area_intersection(col)

func _handle_stack_area_intersection(stack_area: StackArea):
   pass # do stuff
1 Like