0 votes

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

in Engine by (81 points)

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.

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.

1 Answer

+1 vote
Best answer

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.

by (1,945 points)
selected by

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!

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.