0 votes

Since years, I do not understand why there are setters and getters. Can't you always access a variable/property trough it's name?
Example:

extends Label

func _ready():
    print(text) # prints the gotten text
    print(get_text()) # prints the gotten text
    text = "hello" # sets text
    set_text("hello") # sets text
Godot version 3.2.3.stable
in Engine by (378 points)

3 Answers

0 votes

I'm not entirely sure, I think it's partially preference, and using setters and getters is probably a better practice as many nodes contain them and it can make your code a little easier to read. I remember some GDquest videos making explicit use of them, but then again they some are pretty old.

by (54 points)
0 votes

The reason those functions exist is because in Godot 2 you couldn't access properties directly. Now in Godot 3 you can, but the functions still remain.

by (7,190 points)

I thought, this would exist in all languages, does it not? And afaik, the functions will still be improved or developped.

Sorry, I'm not sure what you're saying.

I thought, setters and getters would exist in every programming language and as far as I know, setter and getter functions will still be being continued to develop.

I don't think that's the case for all programming languages. Like, python doesn't have those since there are no private variables. I guess more setter and getter functions will be added to gdscript because of compatibility reasons though.

0 votes

I found them extremely usefull, since they let adress any property as a string. And this opens a variety of options for dynamic coding, strings are very elastic variable type.

by (2,352 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.