0 votes

Here is my code

extends Navigation2D

var speed = -0.2
var velocity = Vector2.ZERO

var spawnX = RandomNumberGenerator.new()

var score = 0

func ready():
pass

func process(delta):
move
local_x(speed)

func enemySpawn():
spawnX.randomize()
var xSpawn = spawnX.randfrange(-720.0, 1000.0)
var Enemy = load("res://Enemy/Enemy.tscn")
var enemy = Enemy.instance()
get
parent().add_child(enemy)

func onHurtboxareaentered(area):
score = score + 1
queue_free()
print(score)

Godot version Godot_v3.3.2
in Engine by (19 points)

Can you describe exact task?

p.s. randomize() should be called only once, at startup, like in _ready().
The same about var Enemy = load("res://Enemy/Enemy.tscn"), unless you load different scenes: you load it once and instance() and add_child() at the spawn moment.

I need to use the random numbers generated to make the x and y positions randomized.

Please indent your code by wrapping with <pre><code>my code</code></pre> and indent properly :)

Ok, I will keep that in mind for later

2 Answers

0 votes

You can use yield with timer and a loop. Note I reuse your rng. Guess you need some more logic to prevent multiple enemies?

...
var player_dead :bool = false
....
func start_spawning():
    while not player_dead:
        var random_delay : float = spawnX.randfrange(0.1, 2.0)
        yield(get_tree().create_timer(random_delay), "timeout")
        enemySpawn() # create one enemy
by (636 points)
edited by

I used your code and the game started to crash when the enemy was destroyed. I added randomized Y positions to the RNG.

extends Navigation2D


var speed = -0.2
var velocity = Vector2.ZERO

var spawnX = RandomNumberGenerator.new()
var spawnY = RandomNumberGenerator.new()

var score = 0

func ready():
    pass

func _process(delta):
    move_local_x(speed)

func enemySpawn():
    spawnX.randomize()
    spawnY.randomize()
    var xSpawn = spawnX.randf_range(720.0, 1000.0)
    var ySpawn = spawnY.randf_range(80.0, 290)
    
    var Enemy = load("res://Enemy/Enemy.tscn")
    var enemy = Enemy.instance()
    enemy.x = xSpawn
    enemy.y = ySpawn
    get_parent().add_child(enemy)


var player_dead :bool = false

func start_spawning():
    while not player_dead:
        var random_delay : float = spawnX.randfrange(0.1, 2.0)
        yield(get_tree().create_timer(random_delay), "timeout")

func _on_Hurtbox_area_entered(area):
    enemySpawn()
        start_spawning()
    score = score + 1
    queue_free()
    print(score)

I forgot to spawn enemies so updated my answer.


func _on_Hurtbox_area_entered(area):
    player_dead = false
    player_in_hurtbox = true
    start_spawning()
    score = score + 1
    # queue_free()
    print(score)

Your code is doing queue_free() when entering (guess that is the crash) ... that is not logical is it? Guess that is for _area_exited. My player_dead should be named player_in_hurtbox


func _on_Hurtbox_area_exited(area):
    # player_dead = true
    player_in_hurtbox = false
    score = score + 1
    queue_free()
    print(score)
0 votes

I need to use the random numbers generated to make the x and y positions randomized.

onready var randomGen : RandomNumberGenerator = RandomNumberGenerator.new()
onready var prototype : PackedScene  = load("res://scene.tscn")

func _ready() -> void:
  randomGen.randomize()

func spawn() -> void:
  var pos : Vector2 = Vector2(randomGen.randf_range(-100, 100), randomGen.randf_range(-100, 100))
  var spawnedItem : Node2D = prototype.instance()
  get_parent().add_child(spawnedItem)
  spawnedItem.position = pos
by (1,438 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.