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.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.classdb; 24 import godot.reference; 25 /** 26 Reference to a function in an object. 27 28 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. 29 However, by creating a $(D FuncRef) using the $(D @GDScript.funcref) function, a reference to a function in a given object can be created, passed around and called. 30 */ 31 @GodotBaseClass struct FuncRef 32 { 33 package(godot) enum string _GODOT_internal_name = "FuncRef"; 34 public: 35 @nogc nothrow: 36 union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; } 37 alias _GODOT_base this; 38 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 39 package(godot) __gshared bool _classBindingInitialized = false; 40 package(godot) static struct GDNativeClassBinding 41 { 42 __gshared: 43 @GodotName("call_func") GodotMethod!(Variant, GodotVarArgs) callFunc; 44 @GodotName("call_funcv") GodotMethod!(Variant, Array) callFuncv; 45 @GodotName("get_function") GodotMethod!(String) getFunction; 46 @GodotName("is_valid") GodotMethod!(bool) isValid; 47 @GodotName("set_function") GodotMethod!(void, String) setFunction; 48 @GodotName("set_instance") GodotMethod!(void, GodotObject) setInstance; 49 } 50 /// 51 pragma(inline, true) bool opEquals(in FuncRef other) const 52 { return _godot_object.ptr is other._godot_object.ptr; } 53 /// 54 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 55 { _godot_object.ptr = n; return null; } 56 /// 57 pragma(inline, true) bool opEquals(typeof(null) n) const 58 { return _godot_object.ptr is n; } 59 /// 60 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 61 mixin baseCasts; 62 /// Construct a new instance of FuncRef. 63 /// Note: use `memnew!FuncRef` instead. 64 static FuncRef _new() 65 { 66 static godot_class_constructor constructor; 67 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("FuncRef"); 68 if(constructor is null) return typeof(this).init; 69 return cast(FuncRef)(constructor()); 70 } 71 @disable new(size_t s); 72 /** 73 Calls the referenced function previously set in $(D _function) or $(D @GDScript.funcref). 74 */ 75 Variant callFunc(VarArgs...)(VarArgs varArgs) 76 { 77 Array _GODOT_args = Array.make(); 78 foreach(vai, VA; VarArgs) 79 { 80 _GODOT_args.append(varArgs[vai]); 81 } 82 String _GODOT_method_name = String("call_func"); 83 return this.callv(_GODOT_method_name, _GODOT_args); 84 } 85 /** 86 Calls the referenced function previously set in $(D _function) or $(D @GDScript.funcref). Contrarily to $(D callFunc), this method does not support a variable number of arguments but expects all parameters to be passed via a single $(D Array). 87 */ 88 Variant callFuncv(in Array arg_array) 89 { 90 checkClassBinding!(typeof(this))(); 91 return ptrcall!(Variant)(GDNativeClassBinding.callFuncv, _godot_object, arg_array); 92 } 93 /** 94 95 */ 96 String getFunction() 97 { 98 checkClassBinding!(typeof(this))(); 99 return ptrcall!(String)(GDNativeClassBinding.getFunction, _godot_object); 100 } 101 /** 102 Returns whether the object still exists and has the function assigned. 103 */ 104 bool isValid() const 105 { 106 checkClassBinding!(typeof(this))(); 107 return ptrcall!(bool)(GDNativeClassBinding.isValid, _godot_object); 108 } 109 /** 110 111 */ 112 void setFunction(in String name) 113 { 114 checkClassBinding!(typeof(this))(); 115 ptrcall!(void)(GDNativeClassBinding.setFunction, _godot_object, name); 116 } 117 /** 118 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). 119 */ 120 void setInstance(GodotObject instance) 121 { 122 checkClassBinding!(typeof(this))(); 123 ptrcall!(void)(GDNativeClassBinding.setInstance, _godot_object, instance); 124 } 125 /** 126 The name of the referenced function. 127 */ 128 @property String _function() 129 { 130 return getFunction(); 131 } 132 /// ditto 133 @property void _function(String v) 134 { 135 setFunction(v); 136 } 137 }