All Questions

Community

Y
Thomas Yang

Difference between "_function" vs "function" naming convention

Hi, it's not mentioned in the article but I always noticed your team deliberately use "_function" in some situations.

My understanding is that you'll use "_function" when you don't intend to call it directly (kinda like setter) in the method. But I feel like I saw some outliers so I'm not too sure if my understanding is accurate.

  • Nathan Lovato replied

    It's a little confusing because Godot uses the leading underscore for virtual functions, and we use it for pseudo-private functions.

    Virtual functions are functions defined on a parent class but that don't do anything by themselves. Like _process, _unhandled_input, etc.

    To do something with them you need to redefine them in your scripts.

    The convention in GDScript is to start their name with an underscore.

    But most of the time, we use the leading underscore to tell "Don't call this function from outside this class." This is what you call pseudo-private.

    Some languages allow you to define public and private properties and methods. Public methods can be called from outside the class (for example, one node calls a function on another node) while with private ones, you can't.

    In GDScript there's no notion of public or private so we use a convention instead. The leading underscore tells teammates "please don't use this from another node or object."