Hey need help in enemies spawning🙂

well i want a basic help in the work as you can see all the enemies and objects spawns at a specific position not nearby the player.. so i want solution of this problem where i want these will spawn randomly nearby the player ..

here in script this script handle the position and spawning of spawning items and enemies…

here i am using the follow node to make a following path and a follower to follow it and randomize the range (r n g ) so that system will randomly select the position on the following path and spawn the item or enemy after timer timeout..

if need more detailed info you can tell ..

Script:-

extends Node2D

var spawn = true
var spawn_count = 0 


@onready var enemy_spawn_timer = $timer_node/enemy_Timer
@onready var coins_spawn_timer = $timer_node/coins_Timer
@onready var bullets_spawn_timer = $timer_node/bullets_Timer
@onready var fule_spawn_timer = $timer_node/fule_Timer

@onready var enemy_spwan_location= $tank/Camera2D2/Path2D/PathFollow2D
@onready var enemy_spawn_location= $tank/Camera2D2/Path2D/PathFollow2D/enemy_spawn_point

@onready var coins_spwan_location= $tank/Camera2D2/Path2D/PathFollow2D2
@onready var coins_spawn_location= $tank/Camera2D2/Path2D/PathFollow2D2/coins_point

@onready var fule_spwan_location= $tank/Camera2D2/Path2D/PathFollow2D4
@onready var fule_spawn_location= $tank/Camera2D2/Path2D/PathFollow2D4/fule_spawn_point

@onready var bullets_spwan_location=$tank/Camera2D2/Path2D/PathFollow2D3
@onready var bullets_spawn_location=$tank/Camera2D2/Path2D/PathFollow2D3/bullet_point

																										# pre loading for spwan
var enemy_tank = preload("res://asserts/sceans/enemy.tscn")
var ammos = preload("res://asserts/sceans/ammos.tscn")
var fule  = preload( "res://asserts/sceans/fule.tscn")
var coins = preload("res://asserts/sceans/war_coins.tscn")
var player = preload("res://asserts/sceans/tank.tscn")



																										# keep ready items 
func _ready():
	fule_spawn_timer.wait_time = 1.55
	enemy_spawn_timer.wait_time = 10
	coins_spawn_timer.wait_time = 1.55
	bullets_spawn_timer.wait_time = 1.55
	randomize()
	fule_spawn_timer.start()
	enemy_spawn_timer.start()
	coins_spawn_timer.start()
	bullets_spawn_timer.start()



																									# sowan logics
func spawn_enemy():
	var enemy = enemy_tank.instantiate()
	# set position, etc.
	enemy.position = enemy_spawn_location.global_position
	get_parent().add_child(enemy)

func spawn_fule():
	var fule1 = fule.instantiate()
	fule1.position = fule_spawn_location.global_position
	get_parent().add_child(fule1)

func spawn_ammos():
	var ammos1 = ammos.instantiate()
	ammos1.position = bullets_spawn_location.global_position
	get_parent().add_child(ammos1)

func spawn_coins():
	var coins1 = coins.instantiate()
	coins1.position = coins_spawn_location.global_position
	get_parent().add_child(coins1)



																									# timers for continue spwans
func _on_enemy_timer_timeout() -> void:
	check_spwan_count()
	enemy_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		print(rng)
		enemy_spwan_location.progress = rng.randi_range(0, 15700)
		enemy_spwan_location.progress_ratio = rng.randi_range(0, 1)
		spawn_enemy()
		enemy_spawn_timer.start()
	elif spawn == false:
		enemy_spawn_timer.start()

func _on_coins_timer_timeout() -> void:
	check_spwan_count()
	coins_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		enemy_spwan_location.progress = rng.randi_range(0, 15700)
		enemy_spwan_location.progress_ratio = rng.randi_range(0, 1)
		spawn_coins()
		coins_spawn_timer.start()
	elif spawn == false:
		coins_spawn_timer.start()



func _on_fule_timer_timeout() -> void:
	check_spwan_count()
	fule_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		enemy_spwan_location.progress = rng.randi_range(0, 15700)
		enemy_spwan_location.progress_ratio = rng.randi_range(0, 1)
		spawn_fule()
		fule_spawn_timer.start()
	elif spawn == false:
		fule_spawn_timer.start()



func _on_bullets_timer_timeout() -> void:
	check_spwan_count()
	bullets_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		enemy_spwan_location.progress = rng.randi_range(0, 15700)
		enemy_spwan_location.progress_ratio = rng.randi_range(0, 1)
		spawn_ammos()
		bullets_spawn_timer.start()
	elif spawn == false:
		bullets_spawn_timer.start()



																					# check spwan_count 
func check_spwan_count():
	if spawn_count >= 500:
		spawn = false
	else :
		spawn = true

#k																									 # area checkers

func _on_area_2d_body_entered(body: Node2D) -> void:
	spawn = true
	
	if body.has_method("enemy_tank"):
		spawn = false
	elif body.has_method("_tank_player"):
		spawn = false
	elif body.has_method("war_coins"):
		spawn = false
	elif body.has_method("ammos"):
		spawn = false
	elif body.has_method("fule"):
		spawn = false
	elif !body.has_method("wing"):
		spawn = true

Please format your code snippet as preformatted text by using triple backticks ``` at the beginning and end of the snippet. Otherwise it’s very hard to read.

2 Likes

Is that ok ?? .. :blush:

1 Like

Perfect!

First of all, there is a lot of “spwan” in your code instead of “spawn”. I’d recommend you correct all of these, as these might lead to errors later on when you inevitably mistype a variable or function name.

Second of all, you’re randomizing the progress, and then progress_ratio. This is the same thing, you should do either one or another, but not both. Also, you’re randomizing the progress_ratio as int range, meaning it will be either 0 or 1, never something in between. It will never be 0.1, 0.2, 0.512 etc. This is probably why your enemies spawn in the same spot every time.

You should do just this:

		enemy_spwan_location.progress_ratio = rng.randf()

This simply randomizes the number between 0.0 and 1.0 as float, so it can be anything in between like 0.1, 0.2, etc.

This should work assuming all other pieces in your code work correctly.

1 Like

Ohhh :open_mouth: ok.. thnks bro :blush:.. i will try this. Later.. i am outside right now. But Thanks for solving problems :smiley:

1 Like

Well bro I tried your way but the problem is same

extends Node2D

var spawn = true
var spawn_count = 0 


@onready var enemy_spawn_timer = $timer_node/enemy_Timer
@onready var coins_spawn_timer = $timer_node/coins_Timer
@onready var bullets_spawn_timer = $timer_node/bullets_Timer
@onready var fule_spawn_timer = $timer_node/fule_Timer

@onready var enemy_spawn_location= $tank/Camera2D2/Path2D/enemy_PathFollow2D
@onready var enemy_spawn_point= $tank/Camera2D2/Path2D/enemy_PathFollow2D/enemy_spawn_point

@onready var coins_spawn_location= $tank/Camera2D2/Path2D/coins_PathFollow2D2
@onready var coins_spawn_point= $tank/Camera2D2/Path2D/coins_PathFollow2D2/coins_point

@onready var fule_spawn_location= $tank/Camera2D2/Path2D/fule_PathFollow2D4
@onready var fule_spawn_point= $tank/Camera2D2/Path2D/fule_PathFollow2D4/fule_spawn_point

@onready var bullets_spawn_location=$tank/Camera2D2/Path2D/bullet_PathFollow2D3
@onready var bullets_spawn_point=$tank/Camera2D2/Path2D/bullet_PathFollow2D3/bullet_point

																										# pre loading for spawn
var enemy_tank = preload("res://asserts/sceans/enemy.tscn")
var ammos = preload("res://asserts/sceans/ammos.tscn")
var fule  = preload( "res://asserts/sceans/fule.tscn")
var coins = preload("res://asserts/sceans/war_coins.tscn")
var player = preload("res://asserts/sceans/tank.tscn")



																										# keep ready items 
func _ready():
	fule_spawn_timer.wait_time = 1.55
	enemy_spawn_timer.wait_time = 10
	coins_spawn_timer.wait_time = 1.55
	bullets_spawn_timer.wait_time = 1.55
	randomize()
	fule_spawn_timer.start()
	enemy_spawn_timer.start()
	coins_spawn_timer.start()
	bullets_spawn_timer.start()



																									# sowan logics
func spawn_enemy():
	var enemy = enemy_tank.instantiate()
	# set position, etc.
	enemy.position = enemy_spawn_point.global_position
	get_parent().add_child(enemy)

func spawn_fule():
	var fule1 = fule.instantiate()
	fule1.position = fule_spawn_point.global_position
	get_parent().add_child(fule1)

func spawn_ammos():
	var ammos1 = ammos.instantiate()
	ammos1.position = bullets_spawn_point.global_position
	get_parent().add_child(ammos1)

func spawn_coins():
	var coins1 = coins.instantiate()
	coins1.position = coins_spawn_point.global_position
	get_parent().add_child(coins1)



																									# timers for continue spawns
func _on_enemy_timer_timeout() -> void:
	check_spawn_count()
	enemy_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		print(rng)
		enemy_spawn_location.progress_ratio = rng.randf()
		spawn_enemy()
		enemy_spawn_timer.start()
	elif spawn == false:
		enemy_spawn_timer.start()

func _on_coins_timer_timeout() -> void:
	check_spawn_count()
	coins_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		enemy_spawn_location.progress_ratio = rng.randf()
		spawn_coins()
		coins_spawn_timer.start()
	elif spawn == false:
		coins_spawn_timer.start()



func _on_fule_timer_timeout() -> void:
	check_spawn_count()
	fule_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		enemy_spawn_location.progress_ratio = rng.randf()
		spawn_fule()
		fule_spawn_timer.start()
	elif spawn == false:
		fule_spawn_timer.start()



func _on_bullets_timer_timeout() -> void:
	check_spawn_count()
	bullets_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		enemy_spawn_location.progress_ratio = rng.randf()
		spawn_ammos()
		bullets_spawn_timer.start()
	elif spawn == false:
		bullets_spawn_timer.start()



																					# check spawn_count 
func check_spawn_count():
	if spawn_count >= 500:
		spawn = false
	else :
		spawn = true

#k																									 # area checkers

func _on_area_2d_body_entered(body: Node2D) -> void:
	spawn = true
	
	if body.has_method("enemy_tank"):
		spawn = false
	elif body.has_method("_tank_player"):
		spawn = false
	elif body.has_method("war_coins"):
		spawn = false
	elif body.has_method("ammos"):
		spawn = false
	elif body.has_method("fule"):
		spawn = false
	elif !body.has_method("wing"):
		spawn = true

Well the result is same there are spawning on same position. Yea enemy is spawning on different locations..

I tried with offsets but it’s not working..

extends Node2D

var spawn = true
var spawn_count = 0 


@onready var enemy_spawn_timer = $timer_node/enemy_Timer
@onready var coins_spawn_timer = $timer_node/coins_Timer
@onready var bullets_spawn_timer = $timer_node/bullets_Timer
@onready var fule_spawn_timer = $timer_node/fule_Timer

@onready var enemy_spawn_location= $tank/Camera2D2/Path2D/enemy_PathFollow2D
@onready var enemy_spawn_point= $tank/Camera2D2/Path2D/enemy_PathFollow2D/enemy_spawn_point

@onready var coins_spawn_location= $tank/Camera2D2/Path2D/coins_PathFollow2D2
@onready var coins_spawn_point= $tank/Camera2D2/Path2D/coins_PathFollow2D2/coins_point

@onready var fule_spawn_location= $tank/Camera2D2/Path2D/fule_PathFollow2D4
@onready var fule_spawn_point= $tank/Camera2D2/Path2D/fule_PathFollow2D4/fule_spawn_point

@onready var bullets_spawn_location=$tank/Camera2D2/Path2D/bullet_PathFollow2D3
@onready var bullets_spawn_point=$tank/Camera2D2/Path2D/bullet_PathFollow2D3/bullet_point

																										# pre loading for spawn
var enemy_tank = preload("res://asserts/sceans/enemy.tscn")
var ammos = preload("res://asserts/sceans/ammos.tscn")
var fule  = preload( "res://asserts/sceans/fule.tscn")
var coins = preload("res://asserts/sceans/war_coins.tscn")
var player = preload("res://asserts/sceans/tank.tscn")



																										# keep ready items 
func _ready():
	fule_spawn_timer.wait_time = 1.55
	enemy_spawn_timer.wait_time = 10
	coins_spawn_timer.wait_time = 1.55
	bullets_spawn_timer.wait_time = 1.55
	randomize()
	fule_spawn_timer.start()
	enemy_spawn_timer.start()
	coins_spawn_timer.start()
	bullets_spawn_timer.start()



																									# sowan logics
func spawn_enemy():
	var enemy = enemy_tank.instantiate()
	# set position, etc.
	enemy.position = enemy_spawn_point.global_position
	get_parent().add_child(enemy)

func spawn_fule():
	var fule1 = fule.instantiate()
	fule1.position = fule_spawn_point.global_position
	get_parent().add_child(fule1)

func spawn_ammos():
	var ammos1 = ammos.instantiate()
	ammos1.position = bullets_spawn_point.global_position
	get_parent().add_child(ammos1)

func spawn_coins():
	var coins1 = coins.instantiate()
	coins1.position = coins_spawn_point.global_position
	get_parent().add_child(coins1)



																									# timers for continue spawns
func _on_enemy_timer_timeout() -> void:
	check_spawn_count()
	enemy_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		print(rng)
		enemy_spawn_location.progress_ratio = rng.randf()
		spawn_enemy()
		enemy_spawn_timer.start()
	elif spawn == false:
		enemy_spawn_timer.start()

func _on_coins_timer_timeout() -> void:
	check_spawn_count()
	coins_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		enemy_spawn_location.progress_ratio = rng.randf()
		spawn_coins()
		coins_spawn_timer.start()
	elif spawn == false:
		coins_spawn_timer.start()



func _on_fule_timer_timeout() -> void:
	check_spawn_count()
	fule_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		enemy_spawn_location.progress_ratio = rng.randf()
		spawn_fule()
		fule_spawn_timer.start()
	elif spawn == false:
		fule_spawn_timer.start()



func _on_bullets_timer_timeout() -> void:
	check_spawn_count()
	bullets_spawn_timer.stop()
	if spawn == true:
		spawn_count += 1
		var rng = RandomNumberGenerator.new()
		rng.randomize()
		enemy_spawn_location.progress_ratio = rng.randf()
		spawn_ammos()
		bullets_spawn_timer.start()
	elif spawn == false:
		bullets_spawn_timer.start()



																					# check spawn_count 
func check_spawn_count():
	if spawn_count >= 500:
		spawn = false
	else :
		spawn = true

#k																									 # area checkers

func _on_area_2d_body_entered(body: Node2D) -> void:
	spawn = true
	
	if body.has_method("enemy_tank"):
		spawn = false
	elif body.has_method("_tank_player"):
		spawn = false
	elif body.has_method("war_coins"):
		spawn = false
	elif body.has_method("ammos"):
		spawn = false
	elif body.has_method("fule"):
		spawn = false
	elif !body.has_method("wing"):
		spawn = true

Should I seprate up the node 2d path for each items or it can work on same pathway??:slightly_smiling_face::slightly_smiling_face:.. BTW thnks for helping time :grinning_face_with_smiling_eyes::grinning_face_with_smiling_eyes:

Btw whenever i print the rng values it shows. -982689315729 somethings. Is that could be a problem??:thinking:

Too much code repetition. Make a generalized spawner scene and use it for each type of object.

3 Likes

Ummm :thinking:.. yea it’s also a good Idea :light_bulb:.. i can try this.. but if i common up the spawner and then connect objects ( enemies and other things) . Then spawner will produce a one random value at a time because all are connected to one spawner so they all get same random values.. :face_with_monocle:.. an. I don’t want this..

No, you can have multiple spawners but they’ll use the same code whose functionality is configured through parameters. Build the spawner as a reusable component.

1 Like

Ohhh ok so you mean same codes but different parameters.. that’s also good way..i will try this :smiley: :smiley:

Can you just show the part of the code that spawns the enemy? there’s no reason for posting the whole code.It makes it hard to read it

1 Like

Well….

func spawn_enemy():
var enemy = enemy_tank.instantiate()
# set position, etc.
enemy.position = enemy_spawn_point.global_position
get_parent().add_child(enemy)

These codes spawn the enemies…

With current codes… enemies are spawning at different places… but the other objects like “ fule, ammos, coins “. These all spawns at same place…

It means the spawn pos is wrong,can you print the enemy_spawn_point.global_position?
like this :

print(enemy_spawn_point.global_position)
1 Like

Ok… i Will but you have to wait bro :sweat_smile:.. actually my computer time is over now i can touch it Tommorow :sweat_smile:.. so today I can only take advices and solution.. and test These all Tommorow :sweat_smile:.

. Thanks for advice .. i will check position of objects

1 Like

Wel guys thank all of you for replying and helping.. and i got solution. It was only problem in naming I was using “ enemy” everywhere instead of fule, ammos and coins.. that’s why it was not working. Now i fixed it and it’s perfect now.. thnks :smiley::smiley::blush::blush::blush::blush: