0 votes

Hi,
I'm spawning a bird at given time in this script.
.....this script works fine but I'd like to ask if there is a better solution?
Thanks for answers.

extends Position3D

var bird_time = 0
#------------------------
#------------------------
func _ready():
    set_process(true)
#-----------------------------------------
func _process(delta):
    bird_time += 1
    if bird_time == 5:
        spawn_bird()
    if bird_time == 800:
        bird_time = 0
        return
#------------------------------------------
func spawn_bird():
    var new_bird
    var bird = preload("res://scenes/bird_03.scn")
    new_bird = bird.instance()
    add_child(new_bird)
#-------------------------------------------
in Engine by (450 points)

2 Answers

0 votes
Best answer

You may want to used fixed processing, it has an exact time step.

Also you don't want to use counters, because they will run at different speeds based on the frame rate. Use the delta time given by the loop. Accumulate it up to some timer value.

func ready():
    set_fixed_process(true)

var bird_spawn_delay = 5.0/60.0

var timer = 0.0

func _fixed_process(delta):

    if(timer >= bird_spawn_delay):
        timer -= bird_spawn_delay
        spawn_bird()

    timer += delta
by (5,206 points)
selected by

Thanks very much....yes it's a better solution.

0 votes

Why not just use a Timer? That's what they're for. Set its wait_time, autostart (or start it in code), and connect the timeout() signal:

func _on_Timer_timeout():
    spawn_bird()
by (20,481 points)

Thanks very much too,......i know, I can use Timer for that but I'm learning GDscript so I write scripts as much as I can.

I understand that, but you shouldn't write scripts just to write scripts. There will be plenty of scripting needed in your projects. Save the scripting for the things you need, and use the built-in engine tools - it's the whole point of using an engine in the first place.

Thank you....you're right....so, this is the meaning that AnimationPlayer or Timer nodes are faster than writing script - It is clear...but also faster for performance?...because built-in tools?

Well, for something like a Timer, you probably wouldn't see much performance difference, but in general, yes. The engine is written in C++ and many optimizations have been made before it comes to you. Scripting is intended to be the "glue" you use to tie the various components you need together, and to implement your game logic.

Thank you very much for your help and clarifications....i appreciate it.

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.