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.
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.
Ummm .. yea it’s also a good Idea .. 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.. .. 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.
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…
Ok… i Will but you have to wait bro .. actually my computer time is over now i can touch it Tommorow .. so today I can only take advices and solution.. and test These all Tommorow .
. Thanks for advice .. i will check position of objects
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