+3 votes

I have an exported Color and I want a method to be called everytime I change it in the Editor. As I understand in GDScript, this is possible with setget. I tried the following

[Export]
private Color col {
      get {return col;}
      set {col = value; setColor(0, col);}
 }

where setColor is the method I want to call (to change a child nodes material albedo). But this crashed the editor and i can't even open the project unless I delete .mono.

I know I can also define the class as a Tool and put setColor in the process method, but I would rather not call it every frame unnecessarily.

in Engine by (34 points)
edited by

1 Answer

+6 votes
Best answer

Ok I asked on Discord and my Idol, my shining star @Dael Grybel got me the answer:

In C#, you didn't specify an access modifier, so it's a private property. Properties, with a { get; set; } have an implied private field created behind the scenes. When you decide to customize the property a bit, and make the getter and setter have more logic to it, it no longer creates an implied private field (as I understand), so it's gonna not know what to do. if you write it like this:

[Tool]
public class myClass : Spatial {

     Color col;

     [Export]
     Color _col {
         get {return col;}
         set {col = value; 
         setColor(col);}
     }
  }

It should be good.

by (34 points)
    [Export] private Texture _texture
{
    get {return _texture;}
    set {_texture = value;
    TileSet.TileSetTexture(0, _texture);
    }
}

straightforward example

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 Frequently asked questions and 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 [email protected] with your username.