(Solved) Tween method

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cristian

Hi, I need to call a method by time, like Spawn each second.

I have this Spawn method, but how I can call it?

 tween.TweenMethod(this, "Spawn", null, null, 5);

Doen’t work.

:bust_in_silhouette: Reply From: jgodfrey

The easiest way is probably with a Timer node. The basics are as follows:

  • Add a Timer to your scene
  • Turn on its Autostart property in the inspector
  • Make sure the One Shot property is off
  • Set the Wait Time property to an appropriate interval (1 second in your example)
  • Connect its timeout event to a script function
  • In that function, do whatever you need (call Spawn in your example)

That’ll set up a timer that will repeatedly call the code in the wired timeout function every Wait Time seconds.

Yes~~~~, that was my first choice but I would like to use Tweens.
Thank you.

Cristian | 2022-08-09 02:56

It doesn’t work well.

	// In the Ready method
	timer = new Timer();
	AddChild(timer);
	timer.Connect("timeout", this, "_on_Timer_timeout");
	timer.WaitTime = 1;
	timer.OneShot = false;

	// In the Spawn method
	Spawn();
	timer.Start();

Cristian | 2022-08-09 03:56

I agree, Tweens are not appropriate to solve this task. Timers are the way to go

godot_dev_ | 2022-08-09 14:32

:bust_in_silhouette: Reply From: Bernard Cloutier
var looping_tween = create_tween().set_loops()
looping_tween.tween_callback(self, "Spawn").set_delay(1)

Be sure to keep a reference to looping_tween so that you can call stop() on it once you’re done spawning.

Thank you, thats right.

Cristian | 2022-08-10 18:33