Should UNSAFE_PROPERTY_ACCESS be ignored?

Godot Version

4.6

Question

So I got this warning:

Line xx (UNSAFE_PROPERTY_ACCESS): The property "my_function" is not present on the inferred type "my_node" (but may be present on a subtype).

In my game, there are times I am connecting signals to different objects for the player to control so I would like to do something like my_signal.connect( my_node.my_function ). But since these different objects could be CharacterBody3D, Node3D or even something else entirely, I need to declare the reference my_node to be:

var my_node : Node

This result in the warning above, but if I ensure that all the different objects that will be put into my_node will definitely have my_function, then there should not be any problem, right?

When is it ok and when is it not ok to ignore this warning? Any hidden caveat?

You could use something like this just to be extra safe:

if my_node.has_method("my_function"):
    my_signal.connect(my_node.my_function)
else:
    push_error("my_node does not have expected method: my_function")

If you scripts also have class names you can also do:

if my_node is MyCustomNode:
    my_signal.connect(my_node.my_function)
else:
    push_error("my_node does is not of expected type MyCustomNode.")

I personally prefer to keep fix and clean up all my errors and warnings just to be safe. You don’t know when something unexpected can happen. And I have been screwed over during a jam where an error did not produce any problems when running from the editor but would always crash the game when exported to an executable. If you’re confident you’ll somehow be always up to code and never miss a declaration then you can use @warning_ignore("UNSAFE_PROPERTY_ACCESS") to at least stop the warning from flooding your debugger.

Also I find it really interesting that you connect the signal from the emitter. I usually connect it from the listener like emitter_node.my_signal.connect(my_function).

1 Like