Hi,
At the moment i have a Custom GameState that has a Delegate returning void and taking 0 params.
What i am trying to accomplish is to be able to add functions to this delegate that will be fired when it is broadcasted. Same as Dispatchers in blueprints.
I admit i am trying something i’ve never worked with so i might be wrong from the start on how to do it.
Anyway this is the code i am actually trying to use.
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FChangeStateDelegate);
UCLASS()
class MYPROJECT_API AMyGameState : public AGameStateBase
{
GENERATED_BODY()
public:
AMyGameState();
UFUNCTION(BlueprintCallable)
void EnterSafeState() { machine->ChangeState((int)E_GameState::Safe); };
UFUNCTION(BlueprintCallable)
void EnterDangerState() { machine->ChangeState((int)E_GameState::Danger); };
UFUNCTION(BlueprintCallable)
void EnterAttackState() { machine->ChangeState((int)E_GameState::UnderAttack); };
//this is the template not working since cannot convert type void(T::*func)() to the type required for addDynamic
template<typename T>
void AddListenerToEnterSafeState(T* obj,void(T::* func)()) {
OnEnterSafeState.AddDynamic(obj, func);
};
//void AddListenerToEnterDangerState(TFunction<void()>* func_to_call);
//void AddListenerToEnterAttackState(TFunction<void()>* func_to_call);
private:
FChangeStateDelegate OnEnterSafeState;
FChangeStateDelegate OnEnterDangerState;
FChangeStateDelegate OnEnterAttackState;
TSharedPtr<StateMachine> machine = MakeShared<StateMachine>(this);
TSharedPtr<SafeGameState> safe = MakeShared<SafeGameState>(machine);
TSharedPtr<DangerGameState> danger = MakeShared<DangerGameState>(machine);
TSharedPtr<AttackGameState> attack = MakeShared<AttackGameState>(machine);
};
Any help would be really appreciated.
Thanks