-
Notifications
You must be signed in to change notification settings - Fork 43
Delegates
You can declare UE delegate types by creating a typedef to one of the following classes:
Event
Delegate
MulticastDelegate
DynamicDelegate
DynamicMulticastDelegate
The first type parameter should refer to the typedef itself - so that the name and metadata can be found. The second type parameter is the function signature of the delegate, e.g.:
typedef FMyCustomEvent = unreal.DynamicMulticastDelegate<FMyCustomEvent, Int->Int->Void>;
To specify that the bound functions for the delegate should be const
, use :thisConst
metadata:
// only binds to C++ functions with the signature "void foo() const"
@:thisConst typedef FConstNotify = unreal.Delegate<FConstNotify, Void->Void>;
For DynamicMulticastDelegates that can be bound in Blueprints, you can use :uParamName
to specify the name of each argument as it will appear in Blueprints. If not specified, a default name will be chosen:
// uParamNames are applied in the same order as the arguments
@:uParamName("Instigator") @:uParamName("DamageCauser") @:uParamName("DamageDealt")
typedef FDiedEvent = unreal.DynamicMulticastDelegate<FDiedEvent, APawn->AActor->Int->Void>;
@:uclass class ACharacter extends AActor {
// can be bound to in blueprints with parameter names
@:uproperty(BlueprintAssignable, Category=Pawn)
var DiedEvent:FDiedEvent;
}
Depending on the delegate type, there are different APIs:
-
BindLambda(fn:Function):Void
-BindLambda
takes any Haxe function. The only way to unbind to it is to callUnbind()
-
BindUObject(obj:unreal.UObject, method:unreal.MethodPointer<unreal.UObject, Function>):Void
-BindUObject
takes an object, and a C++ method pointer related to that object.unreal.MethodPointer
is a special type that represents a C++ method pointer. It only exists on functions exposed to Unreal, like a@:ufunction
that is defined inStatic
, or a function that contains the@:uexpose
metadata. In order to get it, one must callunreal.MethodPointer.fromMethod(obj.functionName)
-
Unbind()
- Unbinds the current bound function -
IsBound():Bool
- Checks if the delegate is bound -
GetUObject()
- ifBindUObject
was used, returns theUObject
that was used -
Execute(arg1, arg2, arg3, ... argN):ReturnType
- Executes the delegate -
ExecuteIfBound(args:...):Void
- Executes the delegate if it's bound
Warning: Binding functions using
BindUObject
requires the function to be exposed to C++. This can be a small issue if you're compiling with cppia - it means that any change to the signature of that function will trigger a full C++ compilation. In order to avoid that, you can consider using a pattern likedelegate.AddLambda(function(arg1, arg2, ..., argN) if (!obj.isValid()) delegate.Unbind(); else obj.someFunction(arg1, arg2, ..., argN)
In order to bind to DynamicDelegate
types, using unreal.CoreAPI;
must be set along side with the import statements
-
BindDynamic(expression):Void
- DynamicDelegates can only be bound to@:ufunction
types.BindDynamic
is a macro that ensures this happens. An example of a way to call it isdelegate.BindDynamic(obj.someUFunction)
-
Unbind()
,IsBound()
,GetUObject()
,Execute(...)
,ExecuteIfBound(...)
- Same asDelegate<Function>
-
IsBound():Bool
- Returns whether there are any functions bound -
Clear():Void
- Clears all delegates bound to delegate -
AddLambda(fn:Function):unreal.FDelegateHandle
- Adds a Haxe function. Works likeDelegate
'sBindLambda
, and the same remarks apply to it -
AddUObject(obj:UObject, fn:unreal.MethodPointer<UObject, Function>):unreal.FDelegateHandle
- Adds a UObject-derived method pointer. See the remarks aboutDelegate
'sBindUObject
- the same remarks apply to it -
AddUniqueUObject(expr):Void
- Works likeAddUObject
, but can bind any function - even functions that are only defined in Haxe (without the@:ufunction
or@:uexpose
metadata). This is the same as binding a function thoughAddLambda
, but before calling it checking if an object is still valid - and if it's not, unbind, like so:
var handle = null;
// equivalent to delegate.AddUniqueUObject(obj.someFunction)
handle = delegate.AddLambda(function(arg1, arg2, ... argN) {
if (!obj.isValid()) {
delegate.Remove(handle);
} else {
obj.someFunction(arg1, arg2, ... argN);
}
});
-
IsBoundToObject(obj:UObject):Bool
- Returns whether there is a delegate bound toobj
-
Remove(handle:unreal.FDelegateHandle):Void
- Removes a bound function by using itsFDelegateHandle
-
Broadcast(arg1, arg2, ... argN):Void
- Broadcasts the arguments to all multicast delegates
Like DynamicDelegate
, in order to bind to DynamicMulticastDelegate
, using unreal.CoreAPI;
is needed
-
AddDynamic(expr):Void
- Adds a@:ufunction
of a UObject-derived object to the invocation list. Can be called ilkedelegate.AddDynamic(obj.someUFunction)
-
AddUniqueDynamic(expr):Void
- Same asAddDynamic
, but only if a delegate with the same signature does not exist already in the invocation list -
RemoveDynamic(expr):Void
- Removes a@:ufunction
of a UObject-derived object from the invovation list. Can be called likedelegate.RemoveDynamic(obj.someUFunction)
-
IsAlreadyBound(expr):Bool
- Returns whether a@:ufunction
of a UObject-derived object is already bound to this delegate. Can be called likedelegate.IsAlreadyBound(obj.someUFunction)
-
IsBound():Bool
,Clear():Void
- Same asMulticastDelegate<Function>