+3 votes
in Engine by (31 points)

I need to ask: are you used to structured programming?

If not, maybe you can look for some introductions to python to learn how to use things like functions (you can find a couple recommended in the Q&A).

3 Answers

0 votes
Best answer

First, I understand the title of the question is pretty much self-explanatory, but you should describe your question better. For example, what do you mean as a "part" of the script? Is it a method? Are you trying to run a whole section of it? How many approaches have you tried before asking the question?

Now, let's think about the game loop. You know you want to keep repeating a piece of code, so intuitively we can think on writing that code on the game loop (process or fixed_process) and have our way on it. But to repeat something every X seconds, we would have to get information on Time spent each loop; we could use Godot timer node, but usually I like to do this on the script I'm working with by simply storing time spent each loop (that's the 'delta' that process and fixed_process gives you), and when this timer is bigger than a threshold, reset it and run the piece of code I want. For example:

timer = 0
timer_limit = 1000 # in miliseconds

..timer += delta
..if (timer > timer_limit):
....timer -= timer_limit

by (304 points)
selected by
+9 votes

You can also use a Timer:

func _ready():
    # Create a timer node
    var timer = Timer.new()

    # Set timer interval

    # Set it as repeat

    # Connect its timeout signal to the function you want to repeat
    timer.connect("timeout", self, "repeat_me")

    # Add to the tree as child of the current node


func repeat_me():


by (28,789 points)

Thank you!
This worked flawslessly for what I needed :).

  • Doing some coastal theme and needed to extend the background sound with additional sounds (like seagulls) do let them hear themselves every now and then. So I extended the script with some randomize(). Only "error" I have is that the first time, it plays it 2 times after each other, even if the first random number is 10 (for 10 seconds).
    Still need to add something so it chooses a rondom sound from a list/array/variable to make it even more diverse -
+2 votes

you can use the one line timer at the end(or start) of your function and , and then recall the same function at the end.

func example():
     yield(get_tree().create_timer(timeBetweenCalls), "timeout")
by (18 points)

Calling the same function again and again (recursion) might slow down the operation due to calling procedure. Iteration seems better here.

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 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 webmaster@godotengine.org with your username.