All Questions

Community

D
Philip Darlow

Underscore function and void

I didn't quite get the underscore vs no underscore functions, I don't quite get also the need to put -> void: after some functions, maybe I've missed the explanation, or maybe there is some general rule that should be obvious for me that I'm not aware of? Will be grateful to point me in the right direction for understanding. Thank you.

  • Nathan Lovato replied
    Solution

    Here are the answers to your questions.

    Leading underscore before a function

    The leading underscore in a function's name means one of two things:

    • It's a virtual function, that is to say, classes that extend this class should override it.
    • It's a private function, that is to say, no other class should access it (this generally means other gdscript files should not call the function)

    The second point also apply to variable names.

    Returning void from a function

    This return type hint helps to prevent errors when writing code. Godot should warn you if:

    • You try to return a value from a void function
    • You try to store or use the return value from a void function

    More importantly, the return type also shows in auto-completion, through the language server in auto-generated documentation, so when coding, you know you're working with a function that doesn't return anything.