T Porter
T Porter

Reputation: 1281

Why isn't my tool script updating the size of my ColorRect nodes in the editor?

I am building a simple health bar using ColorRect nodes and I want to test that my script is working without running the project. I'm using a Tool script, however changing the values in the editor doesn't affect the size of the ColorRect. If I run the scene using F6 the script runs properly.

Screenshot of the health bar scene in the Godot editor. Max Health is 100, Health is 50. The green health bar is still 100% of the width of the control.

Screenshot of the scene running. The green health bar is 50% of the width of the control.

using Godot;

[Tool]
public class HealthBar : Control
{
  [Export]
  private float _maxHealth = 100;

  [Export]
  private float _health = 100;

  private ColorRect _backgroundRect;
  private ColorRect _healthRect;

  public override void _Ready()
  {
    base._Ready();
    _backgroundRect = this.GetExpectedNode<ColorRect>("Background");
    _healthRect = this.GetExpectedNode<ColorRect>("Health");

    if (_maxHealth < 1)
    {
      _maxHealth = 1;
    }
  }

  public override void _Process(float delta)
  {
    base._Process(delta);
    var maxWidth = _backgroundRect.RectSize.x;
    var healthPercentage = _health / _maxHealth;
    var healthWidth = maxWidth * healthPercentage;
    _healthRect.RectSize = new Vector2(healthWidth, _healthRect.RectSize.y);
  }
}

Am I doing something wrong or is this a known bug with Tool scripts in Godot?

(Godot version: 3.5.1.stable.mono)

Upvotes: 0

Views: 457

Answers (1)

T Porter
T Porter

Reputation: 1281

Ah, I simply needed to close and re-open the tab and it's working now.

Upvotes: 0

Related Questions