Using an If statement in func _process(delta):

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

Hi, i’m trying to increase the frequency enemies spawns, based on the quantity of points (that is increasing constantly). First i tried using While but crashed my game, them i thought using _process(delta) but the if statement requires a value (if doesnt have one says "invalid operands ‘Nil’ and ‘int’ in operator ‘>=’), so i set a value but since is in _process(delta) it keeps coming back to that value.

func _process(delta):
score = 0
if score >= 0 and score < 50:
	$EnemyTimer.set_wait_time(0.6)
elif score >= 50 and score < 150:
	$EnemyTimer.set_wait_time(0.5)
elif score >= 150 and score < 200:
	$EnemyTimer.set_wait_time(0.4)
else:
	$EnemyTimer.set_wait_time(0.3)
print($EnemyTimer.wait_time)
func _on_ScoreTimer_timeout():
score +=1
$HUD.update_score(score)

func new_game():
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()
$HUD.update_score(score)
$HUD.show_message("Get Ready!")

i hope i explain my self properly, if you could help me it would be awesome.

:bust_in_silhouette: Reply From: timothybrentwood
func _process(delta):
    score = 0

Makes your score variable get reset to 0 on the start of every frame (happens more than 60 times a second), this is not what you want. Do that in the _ready() function or where you declare your score variable:

func _ready():
    score = 0

or

var score = 0

_ready() gets called one time shortly after the node enters the SceneTree which will make sure your score variable is initialized (non-Nil) before using it.