All Questions

Community

Daniel Queiroz Porto

Why is it important to use a static functions instead of normal functions in this case?

I haven't seen the following videos yet, this is the video I had to stop today, so sorry if this is explained further in the following videos. Also, I'm graduated as a graphic designer and have been migrating to programming since your first video on godot back at the end of 2016 (Thanks!! I actually got a job in a game company that uses Godot here in brazil at the end of 2018!), so I might not be understanding some basic programming principles. The way I grasped static functions is that they are used in a class when we want to be able to do things with that class without having an instance of it necessarily. So in my work, I see the more experienced programmers using it to create helper functions that are inside the responsibility of that particular class but need to be called anywhere without having to create an instance of it just for that or without having a reference to a particular instance of it. But since we are using nodes and not specific class_name in this one, we will only be able to access these methods after the scene tree is loaded. We will never use them without a reference to the node. So why is it important for them to be static? They do fit the description of "helper" functions but they could still be normal functions and not modify the script's properties directly. Or maybe a better question is, what are the advantages of using static functions here instead of normal ones?
  • Nathan Lovato replied

    Hi,

    It's not important or necessary to use a static function. In general, static functions prevent you from accessing the state of a class: its member variables. So they prevent you from modifying these.

    And in general, the main source of bugs in a program is modifying the state an object. Again, static functions force you to calculate and return a new value instead of modifying existing values in place.

  • Daniel Queiroz Porto replied

    Thanks!