1 /** 2 Reference to a function in an object. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.funcref; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.meta; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.object; 22 import godot.classdb; 23 import godot.reference; 24 /** 25 Reference to a function in an object. 26 27 In GDScript, functions are not $(I first-class objects). This means it is impossible to store them directly as variables, return them from another function, or pass them as arguments. 28 However, by creating a `FuncRef` using the $(D @GDScript.funcref) function, a reference to a function in a given object can be created, passed around and called. 29 */ 30 @GodotBaseClass struct FuncRef 31 { 32 enum string _GODOT_internal_name = "FuncRef"; 33 public: 34 @nogc nothrow: 35 union { godot_object _godot_object; Reference _GODOT_base; } 36 alias _GODOT_base this; 37 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 38 package(godot) __gshared bool _classBindingInitialized = false; 39 package(godot) static struct _classBinding 40 { 41 __gshared: 42 @GodotName("call_func") GodotMethod!(Variant, GodotVarArgs) callFunc; 43 @GodotName("set_instance") GodotMethod!(void, GodotObject) setInstance; 44 @GodotName("set_function") GodotMethod!(void, String) setFunction; 45 } 46 bool opEquals(in FuncRef other) const { return _godot_object.ptr is other._godot_object.ptr; } 47 FuncRef opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 48 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 49 mixin baseCasts; 50 static FuncRef _new() 51 { 52 static godot_class_constructor constructor; 53 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("FuncRef"); 54 if(constructor is null) return typeof(this).init; 55 return cast(FuncRef)(constructor()); 56 } 57 @disable new(size_t s); 58 /** 59 Calls the referenced function previously set by $(D setFunction) or $(D @GDScript.funcref). 60 */ 61 Variant callFunc(VarArgs...)(VarArgs varArgs) 62 { 63 Array _GODOT_args = Array.empty_array; 64 foreach(vai, VA; VarArgs) 65 { 66 _GODOT_args.append(varArgs[vai]); 67 } 68 String _GODOT_method_name = String("call_func"); 69 return this.callv(_GODOT_method_name, _GODOT_args); 70 } 71 /** 72 The object containing the referenced function. This object must be of a type actually inheriting from $(D GodotObject), not a built-in type such as $(D long), $(D Vector2) or $(D Dictionary). 73 */ 74 void setInstance(GodotObject instance) 75 { 76 checkClassBinding!(typeof(this))(); 77 ptrcall!(void)(_classBinding.setInstance, _godot_object, instance); 78 } 79 /** 80 The name of the referenced function to call on the object, without parentheses or any parameters. 81 */ 82 void setFunction(StringArg0)(in StringArg0 name) 83 { 84 checkClassBinding!(typeof(this))(); 85 ptrcall!(void)(_classBinding.setFunction, _godot_object, name); 86 } 87 }