Okay so im trying to code a weapon and projectile managing system, for this i've made the weapons send a signal containing the information of the projectile such as how much it should spread or the amount of projectiles fired per shot(to make a shotgun for instance). The following code on the node is in charge off spawning said projectiles, it works just fine when firing single projectiles per shot but once i try to spawn more than one projectile per shot and iterate through the code all of the bullets spawned have the exact same direction and rotation, any ideas of why this may be? Thanks in advance!

extends Node

var rng = RandomNumberGenerator.new()

func handle_projectile_spawn(projectile, position, direction, projectile_amount, projectile_spread_angle):
	for i in projectile_amount:
		var spread_angle = deg2rad(projectile_spread_angle)/2
		direction = direction.angle()
		direction = rng.randf_range((direction+spread_angle), (direction-spread_angle))
		direction = Vector2(cos(direction), sin(direction))
		add_child(projectile)
		projectile.global_position = position
		projectile.set_direction(direction)
		
		print("bullet ", i, " fired, direction ", direction)

im thinking the code maybe overrides the changes made to previous projectiles but im not too sure, i'll maybe try first adding them to an array to later modify their characteristics more clearly, lets hope it helps πŸ™‚

Add:

func _ready():
  rng.randomize()

    Bilal tried it and still the same outcomeπŸ™ I'll try more stuff, lets hope something works

      gorrrroto okay little update, since i was working with the same bullet instance i presume its getting all confused, I'll try sending an array with all the bullet instances in the signal

      Or send a PackedScene parameter in the signal, and do the instancing in the loop.

        By default, the RNG seed always starts the same way (that way every time you run the app, it is predicable and has the same seed). You can reseed it by calling randomize or by giving it a seed manually. However, if call randomize in ready, that may work, though I don't know if that is shared between the same class (for example if you call it at the top of the script, it will act as a static variable and be the same for all instances). Probably your best bet is to call randomize in ready, and if that doesn't work you'll need to call it in the spawn function prior to getting the random values. Also, I don't see if ever creating the projectile instance. You should probably have the call to .instance() in that function (so it creates a new projectile, which would trigger the ready() function). If you are using a pool and reusing objects, then your only option is to reseed the RNG by calling randomize in that loop.

        DaveTheCoder for now im making the instancing loop before sending in the signal but i'll probably try sending the packed scene in the future, thanks for suggestion πŸ™‚