Setter/getter using an external function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Yeldham
:warning: Old Version Published before Godot 3 was released.

Is there a way to use a function of another script with setget?

The best way to answer that would be to try it, e.g.:

var some_prop = 0 setget preload("res://other_script.gd").some_method,

But I think it will probably not work. I’m curious about why you’d need that though, usually setters and getters are for the given class’ variables.

Akien | 2017-10-19 12:06

I want to make that some variables can’t be accessed externally, so I use setget with a function that just prints that an attempt to access them is being made. The problem is, is quite annoying to write the same function in all scripts that I want to do this, so I tried to put the function on my game manager singleton, to no success.

And no, preloading didn’t work.

Yeldham | 2017-10-19 13:21

:bust_in_silhouette: Reply From: Akien

I asked reduz to be sure, and as I supposed this is not possible currently.

The setget syntax where you can reference local methods as method_name is quite specific, and making it work for external scripts would be far from trivial and likely not worth the trouble, even if your use case is kind of valid. So you’ll have to keep copying your method in all scripts for now.

A better alternative would likely be to have a real way to set methods as private, so that you don’t have to hack it with setget.

I see, it would be great to have some sort of actual method privatization in GDScript, hope to see it sooner than later.

And thanks for giving me some of your time, and for helping develop this amazing thing that is Godot!

Yeldham | 2017-10-19 14:08