I'm new to unreal engine and c++. I have a class in which I define a delegate with one parameter and a return type:
DECLARE_DELEGATE_RetVal_OneParam(bool, FInteractionValidatorDelegate, APlayerController*)
I've added a property containing this delegate:
FInteractionValidatorDelegate Validator;
And in another class I bind the delegate:
SomeComponent->Validator.BindUObject(this, &AInteractable::IsValid)
This all works fine but I don't want to expose the delegate publicly thus I want to encapsulate it by adding a BindValidator()
method to my component. What is the best method of doing this ?
you could write a function like this :
void yourComponent::BindValidator(UObject * obj, void(*func)(bool))
{
Validator.BindUObject(obj, func);
}
but I recommend against doing this, first because passing functions' pointers will make errors coming from this hard to find (delegates are already enough obfuscation, with lots of macros and generated functions), also you will not be able to expose this to blueprints.
In UE4's way of doing things you could just pass a delegate as a FName
like so :
void yourComponent::BindValidator(UObject * obj, FName functionName)
{
Validator.BindUFunction(obj, functionName);
}
Here it's the UE4 doing the functions' declaration matching.
Hope this helps!