0

Unreal Engine 4.23.0 C++

When running the function BeginPlay(), my member variable _initialTransforms nicely fills in everything as expected, but the moment I'm going out of the scope of the BeginPlay() function and into the DrawRobot(...,...) function _initialTransforms loses its values and just becomes an empty TArray again. I checked if I accidentally changed its values in another function, but these are the only references of _initialTransforms in the project. The class derives from 'AActor', and the object is created out of a possessed 'pawn'. The object doesn't get recreated since its constructor doesn't run twice. Does anyone have an idea why this happens? Thank you!

The reduced version of the code below.

Header file

protected:
    TArray<FMatrix> _initialTransforms;

cpp file

void ARobot::BeginPlay()
{
    Super::BeginPlay();

    TArray<float> zeroArray;
    zeroArray.Init(0, 6);
    Forward(zeroArray, _initialTransforms);
}

void ARobot::Forward(TArray<float> jointsDeg, TArray<FMatrix>& m)
{...
}

void ARobot::DrawRobot(TArray<float> jointsDeg)
{
    Forward(jointsDeg, _currentPose);

    FTransform baseTransform = FTransform(ToLeftHanded(_currentPose[0] * _initialTransforms[0].Inverse()));
... }
  • you pass the member by value. You never modify the member – 463035818_is_not_an_ai Nov 21 '19 at 16:19
  • With the risk of sounding stupid, doesn't the & in the function definition mean that I'm passing a reference? If not, how should I actually modify the member? I need to actually be able to modify the original. – Kevin Saey Nov 21 '19 at 16:58
  • sorry I though it is about `TArray jointsDeg`. Your code is really hard to grasp becuase it is only fragments. Please provide a [mcve] – 463035818_is_not_an_ai Nov 21 '19 at 17:21
  • I just tried it with some dummy values, instead of linking it through another 'pawn', and it actually works. Somehow by running the next line in the other pawn, I'm not pointing to the right object of my ARobot class, I guess. `SelectedRobot->GetDefaultObject()->DrawRobot(_joints);` – Kevin Saey Nov 21 '19 at 17:52
  • `GetDefaultObject` is going to return you a static default that won't exist any uworld. – George Nov 22 '19 at 00:51

1 Answers1

0

As George mentioned, GetDefaultObject doesn't return the actual object. The function below does, so now I can get and set the members of _spawnedRobot easily.

FActorSpawnParameters SpawnParams;
    _spawnedRobot = GetWorld()->SpawnActor<ARobot>(SelectedRobot, location, rotation, SpawnParams);