0 votes

I am very new to Godot and generally making games but I have this tiny problem.

I am making an Asteroids type game and the basics of it are. I destroy an Asteroid and it splits into multiple smaller versions with random vectors.

This works perfectly fine until I try to run it in a browser. In the browser, the vectors for each separate instance are exactly the same every single time.

Here is my code for spawning them

var ma = med_asteroid.instance()
var ma2 = med_asteroid.instance()
get_parent().call_deferred("add_child", ma)
get_parent().call_deferred("add_child", ma2)

Here is the spawn code

func spawn(pos):
    var rng = RandomNumberGenerator.new()

    position = pos
    rotation = rng.randf_range(PI,-PI)
    speed = rng.randi_range(ba_speed_min, ba_speed_max)

    velocity = Vector2(speed, 0).rotated(rotation)

Thank you.

in Engine by (15 points)

Are the position and rotation variables local to the spawn() function? Or are they globally accessible?

They come along with KinematicBody2D I believe, They are not variables I have created. They are different per instance of the object.

I do not know godot that well but I think they are from the Node2D transforms.

1 Answer

0 votes
Best answer

Try to add a parameter on spawn function like:

   func spawn(pos, count):
    var rng = RandomNumberGenerator.new() * count

Create a count var or take it from anywhere just to be sure that the browser do not create the same number on the randomizer. Javascript is no a Top-Down language, everything is running together, that may be causing this.
Or if you are hard coding the spawns, just do like:


You need to correct the final number maybe, I dont know how everything is working on your code, but try to add something different for each spawn.

by (59 points)
selected by

Awesome, you are onto the right Idea.

I got an error from using

func spawn(pos, count):
    var rng = RandomNumberGenerator.new() * count

However, if I do use the count idea here.

 rotation = rng.randf_range(PI,-PI) * count

It seems to work now, thank you.

Great! I'm happy to help.

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 Frequently asked questions and 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 [email protected] with your username.