Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+6 votes

Did anyone manage to change text via code? I tried many ways they suggested but most of them don't work for godot 4. I also try label.modulate or set

red = Color(1.0,0.0,0.0,1.0)
set("theme_override_colors/font_color",red)

but they all warned "Invalid method on base Label". Here is my code:

@onready var status = $Label
func _on_ok_pressed():
if label.text == passWords:
	status.text = "Correct"
else:
	status.add_theme_color_override("font_color", Color(1, 0, 0))
	status.text = "Try again"
asked in CSC380Jan2024 by (1 point)

1 Answer

+3 votes

This seems to work for me:

	myLabel.set_deferred("theme_override_colors/font_color", Color.BLUE);

NOTE: The way I got the "theme_override_colors/font_color" property name was by right-clicking on the font_color property in the theme overrides section of the node inspector, and choosing "copy property path".

This approach ought to work for setting MANY properties from code.

(The set_deferred means the property won't be updated immediately, but rather just after the scripts get done running but before the next render happens.)

answered by (3.5k points)
+1

It works perfectly! Thank you professor!

...