The use of the private keyword

It seems that, opposed to programming C# in Unity, the private keyword is necessary in Godot. Right? In Unity the private keyword is assumed for variables and methods if private is absent in the definitions.

Except for some of the higher level stuff implemented by Godot and Unity, such as resources vs scriptable objects, when it comes to the basic class syntax, I believe it’s the same. If I’m wrong, I’d love to know as well.

Hi Mark,

Yes this is correct, In Unity its assumed if you have none of the keywords then it is assumed to be private but in Godot we have to be explicit otherwise the code does not understand so we have to state public, private or protected.

1 Like

Are you taking about methods, fields, or both? because I did not add an “access modifier” to a field variable inside of the player class and the program ran without any errors.

in C# in general by default methods and variables are private, but to access inside a class or publicly you need to specify

Yes, normally in C# (and C++) by default members of a class are private. And you can access a private field(variable) or method inside of the class that created it; if you want to access it outside of its class it will need to be public (or protected for inherited classes.)
But the point of my question was dealing with how Godot handled these access modifiers. if you look back at the previous post (namely Marc_Calyon’s response) he states that Godot required an explicit access modifier (private, protected, public) So, if one is required for Godot to understand, why does Godot not complain when I leave it off of a field variable? And I like I said before, “Unless he is just talking about methods.”

I originally tried to eliminate the private keyword for a method or variable in Godot, and it didn’t complain, but then again, what I wanted it to do didn’t work until I specified the private keyword. I never have a problem eliminating the private keyword in Unity. But it is required in Godot in at least some situations, if not all situations – I don’t know. Instead of guessing what situations it might not matter, I will always include the qualifying keyword so that Godot doesn’t get confused. GDScript also has its own little quirks, such as indentations that are not required in C#. Perhaps, if the interpretation of C# in Godot gets more advanced, this will become a mute point.

1 Like

so, Godot doesn’t complain it just doesn’t act right if you don’t add an access modifier. Thanks for the heads up, I will use them to avoid any issues.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms