+2 votes

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.

Godot version 3.5
in Engine by (37 points)
edited by

2 Answers

+2 votes

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.

by (16,664 points)

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

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();

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

+1 vote
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.

by (1,244 points)

Thank you, thats right.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.