The function GetWheels() returns always 0

I have some troubles with tank movements and using debug find out that function TArray<ASprungWheel*> UTankTrack::GetWheels() const always return 0!
Here is my implementation:

TArray<ASprungWheel*> UTankTrack::GetWheels() const{ //-- error was here
    TArray<ASprungWheel*> ResultArray;
    TArray<USceneComponent*> Children;
    GetChildrenComponents(true, Children);
    int i(0);
    for (USceneComponent* Child : Children)
    {

        auto SpawnPointChild = Cast<USpawnPoint>(Child);
        if (!SpawnPointChild) continue;

        AActor* SpawnedChild = SpawnPointChild->GetSpawnedActor();
        //UE_LOG(LogTemp, Warning, TEXT("Actors names %s"),*SpawnedChild->GetName()); // if get name = crash?!

        //if(!ensure( *SpawnedChild->GetName() )){ continue;}
        auto name = Child->GetName();//*SpawnedChild->GetName();
         UE_LOG(LogTemp, Warning, TEXT("Track child %s"),*name);

        //auto SprungWheel = Cast<ASprungWheel>(SpawnedChild);
        //if (!SprungWheel) continue;

         ASprungWheel* SprungWheel = Cast<ASprungWheel>(Child);
         if (!SprungWheel) continue;

i++;
        ResultArray.Add(SprungWheel);
    }
    UE_LOG(LogTemp, Warning, TEXT("get wheels must return %i"),i);
        return ResultArray;
}

the resulting output is:

LogTemp: Warning: get wheels must return 0
LogTemp: Warning: Track child SpawnPoint3
LogTemp: Warning: Track child SpawnPoint2
LogTemp: Warning: get wheels must return 0
LogTemp: Warning: Track child SpawnPoint
LogTemp: Warning: Track child SpawnPoint1
LogTemp: Warning: get wheels must return 0
LogTemp: Warning: Track child SpawnPoint3
LogTemp: Warning: Track child SpawnPoint2
LogTemp: Warning: get wheels must return 0
LogTemp: Warning: Track child SpawnPoint
LogTemp: Warning: Track child SpawnPoint1
LogTemp: Warning: get wheels must return 0

When you have this kind of problem then it has gone off at some point causing the return to have less than its supposed to.

You have:

for (USceneComponent* Child : Children)
...
ASprungWheel* SprungWheel = Cast<ASprungWheel>(Child);

But its supposed to be SpawnedChild returned from GetSpawnedActor():

AActor* SpawnedChild = SpawnPointChild->GetSpawnedActor();
auto SprungWheel = Cast<ASprungWheel>(SpawnedChild);

So it could be that casting Child is causing the following continue to get called (you did not place a log there to see) or Add() is adding in something without what’s necessary after the function is returned. Either one borks it.

https://github.com/UnrealCourse/04_BattleTank/blob/master/BattleTank/Source/BattleTank/Private/TankTrack.cpp

This is also one of the problems of using continue or break in for loops in that it can be difficult without a lot of logs or debugging to know what’s happening.

Farther debug shows that function SpawnPointChild->GetSpawnedActor();
get called and everytime return zero. Seems that I forgot to change code in SpawnPoint.cpp in BeginPlay() method - there was re-initialization of global variable. So i fixed that and all works fine)

1 Like

Privacy & Terms