A function that gets called every 10 seconds and increises a value (enemy spawnrate) in a sine wave fashion.

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

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. :slight_smile:

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.

:bust_in_silhouette: Reply From: TheNewGrant

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():
    $Timer.start()       # Restart the 10 second timer

Don’t forget to make your increase_spawnrate and/or spawn_enemies functions to spawn your monsters.

Go here for some more documentation on Timers.

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")


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

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)

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.

TheoTheTorch | 2021-03-28 11:37