Why is my bullet "void"?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Orkimede

Hello, i’m coding a basic game.
My problem is the following: on one hand, long ranged weapon works wells. I have followed this tutorial, and everything i get from here is on point. (link of said tutorial: Topdown Tank Battle: Part 1 · KCC Blog)
On the other, close combat weapon do not produce anything.

What i did:

if ($ArmeEquipee.cac):
		var b = $ArmeEquipee.balle.instance()
		b.start(positionGlobale, dir)
		emit_signal('shoot', $ArmeEquipee.balle, positionGlobale, dir)

So, this is the code used when any character attack. The signal is linked to the main scene were the following piece of code generate my bullet!

func _gen_projo(bullet, _position, _direction):
    var b = bullet.instance()
    b.start(_position, _direction)

To precise what is a weapon in my code: it is just a bunch of variable that will fixe the rate of fire, the scattering, the type of bullet used and if it’s a CC weapon or a long range one. The code that “shoot” is written above, in the caracter.
Lastly, i tried to consider a CC weapon, as a long range one, and it works. (cac = true, for CC weapon, false for LR)

extends Node2D
var can_shoot = true

var balle
var gun_cooldown
var num
var dispersion
var cac

func _ready():
	$TpsRecharge.wait_time = gun_cooldown 
	pass # Replace with function body.

func init():

func _on_TpsRecharge_timeout():

Conclusion / question:
Why the same code, with the same parameter, only work when a signal transmit the information between two scenes?

Not sure if it is the cause, but you have different parents depending on how you generate it. In one case the bullet is child of the main scene and in the other it is child of the weapon that just shot it. And depending on how you are setting the start position, your bullet is starting somewhere else from where you expect it to be.

tastyshrimp | 2019-12-15 00:41

I do have different parent, it’s the purpose of the two type of code. On one hand, i want cc “bullet” to be dependant of the caracter. On the other, i want my long rang bullet (normal one) to be independant. I don’t think the cc bullet will be a child of the weapon, but rather of the character. Anyway, i will check other possible location of the cc bullet, thank for the advice.

Orkimede | 2019-12-16 09:08