Random beginner-question #7: set_pickable(false) vs. input_pickable = false

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pferft

Hi everyone,

In my code, both get_node("Area2D").set_pickable(false) and get_node("Area2D").input_pickable = false work, so I could exchange them as I liked. Is one of them preferable for some reason?

:bust_in_silhouette: Reply From: jgodfrey

Generally, if you’re simply setting or getting a value, the direct access property syntax is preferred (so, the input_pickable in your example). The property setters and getters syntax allow for more complex scenarios when needed (on custom variables).

Generally, prior to Godot 3.0, most (all?) property access was done via the in-built setter/getter syntax, but now most (all?) properties allow direct access - which makes for cleaner code.

Thanks for the “input”!

Come to think of it, I, the beginner, have never stumbled over a “Getter”-situation yet. Could you tell me when I would need that?

pferft | 2020-08-20 07:46

Related to your input_pickable example, if you look at the Property Descriptions docs for the _CollisionObject2D`, you’ll see that each property has a defined getter and setter.

CollisionObject2D — Godot Engine (stable) documentation in English

For input_pickable, the setter is set_pickable(val) and the getter is is_pickable(). Those either set or get the value of the input_pickable property. So you can use either the property or the getter/setter methods to access and update the value.

However, the place where getters/setters are probably most useful is when used with your own, custom variables. If you create setters / getters for a custom variable, your code can be notified each time the value of the variable is either 1) being updated or 2) being retrieved. You know, because a function you define is called on each access, prior to the value being changed or returned.

The advantage here is that your code can decide how to handle the request, and can even decide to modify it or not allow it at all it in the case of a setter. Or, in the case of a getter, your code can decide what value to return based on the request.

You can check out more details in the docs here:

GDScript reference — Godot Engine (stable) documentation in English

jgodfrey | 2020-08-20 14:11

Thank you Jgodfrey, this is certainly abolutely crucial to understand.
Setting up custom variables is a fascinating thing in the first place (again, for a beginner, just because it provides that feeling of really using an incredibly versatile tool in any individualy way you want, basically “telling” it what to do), and creating values that way and then using them hits exactly that “magic” programming spot… I’ll dive into the setter/getter method and can’t wait for my first value to come up! : )

pferft | 2020-08-24 08:49