Problem with turret-like enemy bullet_direction system

Currently all the enemy does choose between 1, 2, 3 and 4.
and call the group “Bullet” to play one of these functions
(for example it will call Change_direction_up when it chooses the number 1) :

# this is a part of the bullet script.

func Change_direction_up():
	direction = "Up"
	print(direction)
func Change_direction_down():
	direction = "Down"
	print(direction)
func Change_direction_left():
	direction = "Left"
	print(direction)
func Change_direction_right():
	direction = "Right"
	print(direction)

Then, it will spawn a bullet, and the bullet will use _process to move the bullet accordingly to the direction variable


# also a part of the bullet script

func _process(delta):
	if direction == "Up":
		rotation_degrees = 0
		position.y -= speed
	if direction == "Down":
		rotation_degrees = 180
		position.y += speed
	if direction == "Left":
		rotation_degrees = 270
		position.x -= speed
	if direction == "Right":
		rotation_degrees = 90
		position.x += speed

What I want to do, is that when I spawn a turret, the turret will randomly choose which direction it will shoot in, and keep on doing that.
But instead, all the turrets will shoot in the same direction


(blue plunger shaped things are bullets)

Scripts:

extends Node2D

#
#this is the script of the bullet
#

@export var direction = "No_direction" #Set this variable at either "Up", "Down", "Left", or "Right".
var speed = 3

func Change_direction_up():
	direction = "Up"
	print(direction)
func Change_direction_down():
	direction = "Down"
	print(direction)
func Change_direction_left():
	direction = "Left"
	print(direction)
func Change_direction_right():
	direction = "Right"
	print(direction)

func _process(delta):
	if direction == "Up":
		rotation_degrees = 0
		position.y -= speed
	if direction == "Down":
		rotation_degrees = 180
		position.y += speed
	if direction == "Left":
		rotation_degrees = 270
		position.x -= speed
	if direction == "Right":
		rotation_degrees = 90
		position.x += speed

func _on_area_2d_body_entered(body):
	get_tree().call_group("Player", "hit_by_bullet")

extends Node2D

#
# this is the script of the enemy/turret-thing
#

var BULLET = load("res://All scenes/bullet.tscn")
var rng = RandomNumberGenerator.new()
var random_number
var pos = position

func _ready():
	choose_direction_of_bullet()
	shoot(pos)

func choose_direction_of_bullet():
	# chooses from numbers 1, 2, 3, and 4
	random_number = rng.randf_range(1, 4)
	if (random_number > 0 && random_number < 1):
		random_number = 1 #up
	if (random_number > 1 && random_number < 2):
		random_number = 2 #down
	if (random_number > 2 && random_number < 3):
		random_number = 3 #left
	if (random_number > 3 && random_number < 4):
		random_number = 4 #right

func shoot(pos):
	var bullet = BULLET.instantiate()
	add_child(bullet)
	if random_number == 1:
		get_tree().call_group("Bullet", "Change_direction_up")
	if random_number == 2:
		get_tree().call_group("Bullet", "Change_direction_down")
	if random_number == 3:
		get_tree().call_group("Bullet", "Change_direction_left")
	if random_number == 4:
		get_tree().call_group("Bullet", "Change_direction_right")

As of your code style, you can choose a random dir in this way:

func choose_random_dir():
    call_deferred("Change_direction_" + ["left", "right", "up", "down"].pick_random())

So whenever you call this function, it will call a random function of change direction (it can be up, left, etc). I suggest you to call it in the ready function.

Or if you just want to create a function that will just pick a random direction, then do like this:

func Change_direction_to_random():
    direction = ["Up", "Down", "Left", "Right"].pick_random()
    print(direction)
1 Like

Thanks!

1 Like