Problem with rand_range()

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

So I made a selection variable that is rand_range(-10, 10) and if it is <=0 it follows player 1and if it is >= 1 it follows player 2. It was only following player 1, so I experimented a little and then it would only follow player 2. I put it back to the original code, and it only followed player 1. After some thinking, I realized that it was because the computer was only picking numbers less than or equal to five. Before I get this suggestion - yes, I have tried changing the maximum number. It always chooses to follow player 1. Any suggestions? Here is my code:

extends KinematicBody2D

onready var player = get_node("../Player")
onready var player2 = get_node("../Player2")
var velocity = Vector2()
var speed = 5000
enum {RIGHT, LEFT, UP, DOWN, IDLE_RIGHT, IDLE_LEFT, IDLE_UP, IDLE_DOWN}
var anim
var new_anim
var state
var min_distance = 4
var selection = rand_range(-10, 10)

func change_state(new_state):
	state = new_state
	match state:
		RIGHT:
			new_anim = 'right'
		LEFT:
			new_anim = 'left'
		UP:
			new_anim = 'up'
		DOWN:
			new_anim = 'down'
		IDLE_RIGHT:
			new_anim = 'idle_right'
		IDLE_LEFT:
			new_anim = 'idle_left'
		IDLE_UP:
			new_anim = 'idle_up'
		IDLE_DOWN:
			new_anim = 'idle_down'

func _ready():
	change_state(IDLE_RIGHT)

func follow(target):
	if target.position.x > position.x && abs(target.position.x - position.x) > min_distance:
		velocity.x += speed
		change_state(RIGHT)
	if target.position.x < position.x && abs(target.position.x - position.x) > min_distance:
		velocity.x -= speed
		change_state(LEFT)
	if target.position.y > position.y && abs(target.position.y - position.y) > min_distance:
		velocity.y += speed
		change_state(DOWN)
	if target.position.y < position.y && abs(target.position.y - position.y) > min_distance:
		velocity.y -= speed
		change_state(UP)


func _process(delta):
	if selection >= 1:
		follow(player2)
	if selection <= 0:
		follow(player)
	if new_anim != anim:
		anim = new_anim
		$AnimationPlayer.play(anim)
	

func _physics_process(delta):
	velocity = move_and_slide(velocity * delta) 
:bust_in_silhouette: Reply From: hilfazer

It looks like you’re not calling randomize() anywhere in your code. You need to call it once before any random funcion that’s supposed to return different values every time. Good place for calling randomize() is some autoload if you’re using any. Calling it more than once will not hurt but is unnecessary.

You don’t need variables for both players, you can have one and initialize it with node reference in _enter_tree() depending on value of selection. You can reassign this variable anytime selection changes, if you’ll want it to change at all.

where would I put the randomize()?

ThreeSpark | 2018-08-11 05:25

For example in _init() function of a parent scene of players and enemies.

hilfazer | 2018-08-11 06:52

did you mean to use randf_range()