I want to get my custom game mode inside my custom received_notify. I tried everything I can but finally failed. If any one has any idea about it, please let me know.
Thank you very much!
=======================================================================
My code is as follows:
bool UAttackAnimNotify::Received_Notify(USkeletalMeshComponent * MeshComp, UAnimSequenceBase * Animation) const
{
// This is used to get the player character in the game world
FName tmp = MeshComp->GetAnimInstance()->Montage_GetCurrentSection();
auto a = MeshComp->GetWorld();
AGameMode* b = UGameplayStatics::GetGameMode(a);
AMainGameMode* c = Cast<AMainGameMode>(b);
return true;
}
I found that we can get a correctly, but b is null whatever I did. That is so strangeā¦
Hey Noah,
I checked your code and it is working. Can you give me a little bit more info you think significant. I wonder what engine version you use.
I add my code (UE4.6).
// JumpAnimNotify.h
#pragma once
#include "JumpAnimNotify.generated.h"
UCLASS()
class UJumpAnimNotify : public UAnimNotify
{
GENERATED_UCLASS_BODY()
bool Received_Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation) const override;
};
// JumpAnimNotify.cpp
#include "MyGame.h"
UJumpAnimNotify::UJumpAnimNotify(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{}
bool UJumpAnimNotify::Received_Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation) const
{
auto a = MeshComp->GetWorld();
AGameMode* b = UGameplayStatics::GetGameMode(a);
return true;
}
Thanks for answering. It turned out that I tried to get game mode in the editor instead of playing game enviroment.
While in the game editor, the world we get does not contain Gamemode.
As a result, we should check the gamemode we get before using it:
FName tmp = MeshComp->GetAnimInstance()->Montage_GetCurrentSection();
auto a = GWorld.GetReference();
AGameMode* b = UGameplayStatics::GetGameMode(a);
AMainGameMode* c = Cast<AMainGameMode>(b);
if (c != NULL)
{
// todo
return true;
}
else
{
return false;
}
It turned out that I tried to get game mode in the editor instead of playing game enviroment.
While in the game editor, the world we get does not contain Gamemode.
As a result, we should check the gamemode we get before using it:
FName tmp = MeshComp->GetAnimInstance()->Montage_GetCurrentSection();
auto a = GWorld.GetReference();
AGameMode* b = UGameplayStatics::GetGameMode(a);
AMainGameMode* c = Cast<AMainGameMode>(b);
if (c != NULL)
{
// todo
return true;
}
else
{
return false;
}