How do you get a return?

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

I have a button that emits the signal “roll_a_dice(0, 20)” and a number gets chosen from 0 to 20, but how do I get the “return roll” at the bottom of the “roll_a_dice()” signal?

:bust_in_silhouette: Reply From: rahsut

In the function, couldn’t you just store the number chosen in a variable and then write
return rolledNumber or something like that?

While signal callbacks can return a value, that returned value won’t be passed back to the signals emitter. So no, that’s no solution for this problem.

njamster | 2020-08-22 11:56

:bust_in_silhouette: Reply From: njamster

how do I get the “return roll” at the bottom of the “roll_a_dice()” signal?

You don’t! If your signal callback returns a value, that value won’t reach the emitter. If you want to do that, don’t use a signal but call the diceroll-function directly.

You can work around that as described here, e.g.:

signal roll_a_dice(min_val, max_val)

func _ready():
	randomize()
	connect("roll_a_dice", self, "_on_roll_a_dice")

	var roll = []
	emit_signal("roll_a_dice", 0, 20, roll)
	print(roll)

func _on_roll_a_dice(min_val, max_val, roll):
	roll.append(randi() % max_val + min_val)

But unless you have very good reasons for it, I wouldn’t recommend that.