changed variables not effecting _physics_process(delta)

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

I keep changing a variable with a timer, but the changed variable doesn’t affect my
_physics_procress(delta) function. Any idea why? The timer is working as the print function in this code works

extends Area2D

var speed=30
var dir=Vector2(0,0)

func _on_Timer_timeout():
	print("timeout")
	var randnum=rand_range(1,8)
	if(randnum==1):
		dir=Vector2(0,1)
	if(randnum==2):
		dir=Vector2(0,-1)
	if(randnum==3):
		dir=Vector2(1,0)
	if(randnum==4):
		dir=Vector2(-1,0)
	if(randnum==5):
		dir=Vector2(1,1)
	if(randnum==6):
		dir=Vector2(1,-1)
	if(randnum==7):
		dir=Vector2(-1,1)
	if(randnum==8):
		dir=Vector2(-1,-1)

func _physics_process(delta):
	translate(speed*dir)
:bust_in_silhouette: Reply From: kidscancode

rand_range() returns a float in the range, so you’re never going to have your random number be a whole number.

Use this instead to get an integer from 1 through 8:

var randnum = randi() % 8 + 1