How do I get updates to color via Script show in Editor, not just runtime?

Godot Version

v4.2.2

Question

Hello,

I have a simple setup with a Node2D with a ColorRect as a child. I’m using a script on the Node2D to change the color of the color rect. This is done so that when I instantiate this scene in my main game, I have a ‘top level’ control for the color of the ColorRect without having to unlock the scene to change it.

However, when changing the exported color variable, it doesn’t update in the editor, only at runtime. This makes sense to me, as it’s happening in the ‘onready’ function.

I’m just stuck on how to get this to work in both runtime and in the editor, as I’d like to use it for setting the basic colors as I place the nodes in my scene.

Any help would be greatly appreciated.

Cheers

Have a look at @tool-scripts. They run in the editor

Thanks! I’ve taken a look at toolscripts and it seems like the right approach, but I’m having trouble actually getting it to work. Based on the docs, this should set the color in the editor:

@tool
extends Node2D


var currentpos:Vector2
var mouse_offset:Vector2 = Vector2(0,0)
var targetpos:Vector2
var dragging:bool = false

@onready var dragtarget:Area2D = get_node("Area2D")
@onready var drag_shape:ColorRect = get_node("ColorRect")

@export var targetcolor:Color
signal dropped(newPos:Vector2)

# Called when the node enters the scene tree for the first time.
func _ready():
	drag_shape.color=targetcolor
	currentpos = self.position
	dragtarget.input_event.connect(handle_mouse)
	

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Engine.is_editor_hint():
		drag_shape.color=targetcolor

But this doesn’t seem to work, although as before it does work once running the game.

Responding to my own post here, but it turns out it does work when the scene is brought into another tree, but not in the scene where its built. Not sure if thats intended or not.

With toolscripts you sometimes have to restart the engine or scene to work properly

Ahh, that was the solution. Thank you so much, i was running in circles :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.