Proposal: Access Modifiers for Classes in GDScript

Proposal: Access Modifiers for Classes in GDScript
Introduction
Currently, GDScript does not support access modifiers (private, protected, etc.), which are common in many programming languages like C#, Java, and Python (with _ and __ conventions). The lack of access control makes it difficult to encapsulate logic properly and prevent unintended access to internal data structures.

This proposal suggests adding access modifiers to GDScript to improve code safety, maintainability, and encapsulation.

Why is this important?
Encapsulation & Data Safety:

  1. Without private or protected members, all classes expose their internals, which can lead to unintended modifications from outside the intended scope.
    This increases the risk of bugs and accidental changes.
    Better Code Organization & Readability

  2. Explicit access modifiers improve readability by indicating which parts of the class are meant for internal use.
    This is especially important in large projects with multiple developers.
    Aligning with Object-Oriented Programming (OOP) Principles

  3. Godot already supports OOP concepts like inheritance and polymorphism, but without access control, it’s harder to enforce encapsulation.
    Consistency with C# in Godot

  4. C# in Godot already supports private, protected, and public access modifiers. Adding similar functionality to GDScript would make it more consistent across the engine.

Proposed Solution
Introduce three levels of access control:
:white_check_mark: public (default) – Accessible from anywhere (same as current behavior).
:white_check_mark: protected – Accessible only within the class and its subclasses.
:white_check_mark: private – Accessible only within the same class.

Backward Compatibility & Implementation

  • The default behavior can remain public to ensure compatibility with existing scripts.
  • Access modifiers can be optional, allowing users to adopt them gradually.
  • A basic implementation could be enforced at the parser level to restrict access accordingly

Conclusion
Adding access modifiers to GDScript would significantly improve encapsulation, maintainability, and project scalability. It would also make GDScript more aligned with modern programming practices and Godot’s existing C# implementation.

This feature would be a valuable addition to Godot 4.3+, helping developers write safer and more maintainable code.

**

So, would you like to have access modifiers in GDScript?

**

1 Like

Yes it does.

Well, it supports public and private access modifiers.

If you precede a variable of function name with an underscore (_), it is private and cannot be accessed outside of that script, except by inherited classes.

As far as I know, there’s no concept of protected in GDScript.

var _my_var: String = "I am a private variable."

func _my_function(member_var: int) -> void:
    return "I am a private function. I must be called within this script."

Even with a preceeding underscore the variable and function can be used outside the script, just like in Python. There is however a warning if a underscored “private” variable is unused within the class body, as Godot seems to expect private variables to work but does not enforce them (as of 4.4).

Proposals can be submitted to this github issues page, though private/public access is already proposed.

2 Likes

Noted. Thanks for the info.