Does GDScript have invocation and callbacks?

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

I’m just learning my second programming language with godot, so applying my knowledge is kinda hard right now.

Does GDScript offer the same polymorphism as c#/++?

:bust_in_silhouette: Reply From: kurtsev0103

Yes. This can be implemented by using a FuncRef

  • First create a method that will be called with the “callback” (In different programming languages: Completion/Block/Closure/… etc.)

func completion_method():
	pass # To do something
    
func completion_method_with_params(p1: String, p2: int):
	pass # To do something 
  • Then create a method that will receive our “callback”

func some_method(completion: FuncRef, completion_with_params: FuncRef):
	# To do something
	completion.call_func()
	# To do something
	completion.call_funcv(["kurtsev", 0103])
	# To do something
  • And now create a reference to the “callback” and just pass it to the method

var completion = funcref(self, "completion_method")
var completion_with_params = funcref(self, "completion_method_with_params")
	
some_method(completion, completion_with_params)