+1 vote

So I'm making a game where enemies spawn non stop, and every 10 seconds, I want to increase the spawnrate to make the game be more dificult as time progresses.

the function would look something like this:

func increase_spawnrate() -> void:
    spawnrate *= 1.2 # This line needs replcacement

Boom. It sounds so easy, yet I don't know how to do it. Pls help me. It'd be very appreciated. :)

Edit: I want the growth to be like a sine wave, not linear. Everything else is set up, the spawning, calling this method every 10 seconds and such. I just want to increase a value in a sinewave fashion, that's everything missing.

Godot version Godot 3.2.3
in Engine by (122 points)
edited by

2 Answers

+1 vote

Sounds like you need the timer node for this. With the timer node, you can set its wait time (time waited before returning timeout() signal) to 10 seconds.
Your code could look like this.
Setup: Add Timer node to hierarchy, connect timeout() signal to the script, set wait time to 10, set autostart to true (to start adding enemies as soon as the timer node loads).
Possible code:

func _ready():
    $Timer.start()    # If autostart isn't selected, will start the timer
    spawn_enemies(spawnrate)    # Do this or else it will take 10 secs                    
                                # before enemies spawn

func _on_Timer_timeout():
    increase_spawnrate()
    spawn_enemies(spawnrate)
    $Timer.start()       # Restart the 10 second timer

Don't forget to make your increasespawnrate and/or spawnenemies functions to spawn your monsters.

Go here for some more documentation on Timers.
https://docs.godotengine.org/en/stable/classes/class_timer.html#class-timer-property-wait-time

You already have your increase_spawnrate function set up. From the look of it, it will work fine. For a spawn_enemies function, you can set up a variable (outside the function)

loadEnemy = preload("res://path_to_enemy_scene")

or

loadEnemy = load("res://path_to_enemy_scene")

Either work, the first loads the enemy at the start of the scene, the second loads the enemy when the code is executed.
After that, you will need to add the enemies to the scene (you can do this in a spawn_enemies function)

func spawn_enemies(spawnrate):
    for enemy in range(spawnrate):
        enemy = loadEnemy.instance()
        add_child(enemy)

this function creates a new instance of the enemy scene and then adds that instance to your hierarchy as a node.

Edit: I noticed that you wanted your number of enemies to increase in a linear fashion. By multiplying to your spawn rate you are increasing the number of enemies spawned at a quadratic rate. To make it linear just add a specified amount to your spawn rate (like spawn rate += 2)

by (71 points)

Thank you for the long answer! I see you worked hard on this. But I think I might have given too little information. The spawning and the method calling every 10 seconds IS working. I just don't know how to increrase a number in a SINE wave sytle. Not linearly.
Do you have an answer for that? Sorry, for the hastle I created by being unclear.

0 votes

[https://rickandmortyhoodies.com/][1]

[1]: https://rickandmortyhoodies.com/ Don't forget to make your increasespawnrate and/or spawnenemies functions to spawn your monsters.

by (14 points)
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.