How to make a tween loop of 3 changing colors work outside of "_ready()" functions

Godot Version

v4.6.3.stable.steam [7d41c59c4]

Question

Hello, I’ve been trying to test out tweening on my own as a way to teach myself a little more about GDscript.

I wanted to see if I could use tweening to change a Sprite2D’s colors on a loop with the “modulate” property. At first I tried to write the code inside the _process() function and while it did modulate and loop the tweens it sent out a lot of errors so I decided to use the _ready() function instead and it worked fine there.

extends Sprite2D

@onready var sprite_2d: Sprite2D = $"."

var tween = create_tween()

func _ready() -> void:
	tween.set_loops(0)
	tween.tween_property(sprite_2d, "modulate", Color.RED, 0.5)
	tween.tween_property(sprite_2d, "modulate", Color.BLUE, 0.5)
	tween.tween_property(sprite_2d, "modulate", Color.GREEN, 0.5)

Though, I wanted to see if I could use the code outside of the _ready() function as I may need to do so in future scenarios. When I did do it even though there were no errors being sent in the debugger the tweening stopped working.

extends Sprite2D

@onready var sprite_2d: Sprite2D = $"."

var tween = create_tween()

func _ready() -> void:
	pass

func tweening_colors() -> void:
	tween.set_loops(0)
	tween.tween_property(sprite_2d, "modulate", Color.RED, 0.5)
	tween.tween_property(sprite_2d, "modulate", Color.BLUE, 0.5)
	tween.tween_property(sprite_2d, "modulate", Color.GREEN, 0.5)


Is there a way I could rectify this? What am I missing?

What code is calling your func tweening_colors()?

Are you familiar with programming?

Have you followed this tutorial?

Good evening, I am not familiar with programming and I just recently started learning GDScript for the first time ever. I have been indeed following the attatched tutorial, though I have not finished it to completion.

Ah, the function wasn’t being used anywhere so of course it didn’t work!

Sorry about that, since it’s my first time interfacing with all of this, things like that end up slipping past me.

Anyways, thanks for the help!