Variable not being returned correctly

4.2.2.stable

so ive been trying to get my code to return a variable, and have been looking on forums for some time, but i cannot find why this is happening. here is my code:
in sword script:

var d
signal getdirection

func _process(delta):
getdirection.emit(d)
print (d) #shows null always
if d== null:
pass
else:
if d>0:
anim.flip_h=true
elif d<0:
anim.flip_h=false

player script:
func _on_sword_getdirection(d):
d=direction
print (d) #shows either -1, 0, or 1 depending on the direction that the player is facing
return d

the sword, while it is its own scene, is listed under the player scene, and the _on_sword_getdirection() signal is connected from the sword to the player. I am very new to godot, and trying to make a fairly simple game. thanks!

In sword.gd, the variable d is never set to anything. So when you emit(d) it sends null to the _on_sword_getdirection method.

I think you want the signal to go from the player to the sword, so swap your logic around and put the signal in player and make a func in sword to accept it (and d) and then change the local d variable so that _process will work as you want.

HTH :slight_smile:

PS - Try the </> tool to format code!

ohhhh that makes sense. Thanks!
I also think that i will make this only happen when the sword swings to make it a little more efficient. I dont have time to try it now, but i will update when I can.

1 Like