Search code examples
c++unreal-engine4itween

UE4 iTween C++ throws "this was nullptr"


I'm trying to use iTween (via C++, not BP) to rotate an actor to face another, but it throwing an exception in Actor.h that says:

enter image description here

I'm using the following code to start the tween:

AActor* actorToRotate = Cast<AActor>(this);

if (actorToRotate != nullptr && CharacterToAttack != nullptr)
{

    FRotator rotationFrom = actorToRotate->GetActorRotation();
    FRotator rotationTo = CharacterToAttack->GetActorRotation();
    FName tweenName = TEXT("turret");

    AiTweenEvent* TurretTween = UiTween::ActorRotateFromToSimple(tweenName, actorToRotate, rotationFrom, rotationTo, CoordinateSpace::world, false, 2.0f, easeInAndOutQuartic);

}

Rather than using the actorToRotate variable I've tried using this in ActorRotateFromToSimple() but I get the same error.

if (CharacterToAttack != nullptr)
{

    FRotator rotationFrom = GetActorRotation();
    FRotator rotationTo = CharacterToAttack->GetActorRotation();
    FName tweenName = TEXT("turret");

    AiTweenEvent* TurretTween = UiTween::ActorRotateFromToSimple(tweenName, this, rotationFrom, rotationTo, CoordinateSpace::world, false, 2.0f, easeInAndOutQuartic);

}

If anyone smarter than me shed some light onto this issue it would be greatly appreciated.

Additional information I think might be important:

  1. actorToRotate is custom type of ATDWeapon that extends from AActor
  2. CharacterToAttack is custom type of ATDAICharacter that extends from ATDCharacter
  3. The function that executes this code is called by GetWorldTimerManager().SetTimer()
  4. I've added #include "iTween/iTween.h" to the top of my TDWeapon.cpp file

Solution

  • Ah, the problem wasn't in the code. I was using "simulate in editor" in the UE4 Editor rather than "play in editor".

    It appears that AiTweenEvent* UiTween::SpawnEvent(AiTAux* aux) needs a player controller and uses GetWorldLocal()->GetFirstPlayerController()->GetPawn()->GetTransform() to get it's transform. In my instance, "simulate in editor" doesn't spawn a player so GetPawn() returns nullptr which GetTransform() doesn't like.

    Awesome.