Is there a way to return a value from a function without exiting it?

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

So sort of a weird question but is there a way to return a value in a function without exiting the function. I know that ‘return’ is a way to do that, but I don’t think there’s a way to make it not exit the function.

:bust_in_silhouette: Reply From: rakkarage
func modifyValue(ref value):
    value += 10

var myValue = 5
modifyValue(myValue)
print(myValue)  # Output: 15

Thanks! This was surprisingly simpler than I thought

Maximm | 2023-05-18 21:05

:bust_in_silhouette: Reply From: godot_dev_

You could also use signals to accomplish something similar to what @rakkarage is suggesting in their answer, as follows:


#yourscript.gd
signal incremented

func _ready():
     connect("incremented",self,"_on_incremented")

#this function increments a value and prints it
func printerIncrement(arg1):
    var arg2 = arg1 +1
    emit_signal("incremented",arg2)
    print(str(arg2))

func _on_incremented(arg3):
    #do some work on the incremented value
    #this emulates the value being returned
    #without exiting the printerIncrement function
    pass