TIL: Learn to Code in C++ by Developing Your First Game (Section 3, Lectures 84-85)

I spent a good chunk of time watching music videos on YouTube tonight. Generation Axe is playing in Vancouver on Wednesday. I have no tickets. This makes me mildly sad, but at the same time I I’m struggling to justify the cost of admission to myself. But at least I got some learnin’ in too.

Section: 3 – Building Escape – Your First Unreal / C++ Game

Lecture 84: Iterating over TArray with for

The documentation for TArrays can be found here.

We do two things in the video, we print the names of anything that is colliding, and we accumulate their mass. Both are fairly simple.

First, in order to get anything to respond to the trigger, it has to generate events on collisions. This was set on the player earlier, but not the chair and table. This is enabled in UE4 on the details of the actors. Once that’s done we use UE_Log(TempLog,Warning,TEXT("%s"),*CollidingActor->GetName()); in order to get the name of our CollidingActor(s). This of course has to be done inside of a FOR loop so that we can iterate through our TArray and get each individual Colliding Actor that we stored in the last video.

Inside the for loop, we also want to accumulate the mass… so we need to get the actor’s mass

We know the actors are stored as pointers. Searching the internet tells us that we can use GetMass() to get the mass from the UPrimitiveComponent. Unfortunately, the documentation is hard to read, and no matter how much time I spend searching on my own…. I can’t find the solution on my own without just having it handed to me from Ben… But it seems that while we have the actor, and we know the method we’re trying to access (and the component)… because we’ve got a variety of objects (the blueprint default pawn, vs the static mesh chair) we don’t know for sure what the name of the actual component will be that stores the mass. Therefore we need to find the component in order to getmass.

So we take the array of actors, we perform a for loop to iterate the array, and then we accumulate the mass by Actor->FindComponentByClass()->GetMass();

So the whole method now looks like:

float UOpenDoor::GetTotalMassofActorsOnPlate() {
    float TotalMass = 0.0f;

    // Find all the overlapping actors
    TArray OverlappingActors;
    PressurePlate->GetOverlappingActors(OUT OverlappingActors);

    // Iterate through them and accumulate their mass
    for (const auto& Actor : OverlappingActors) { // in this case we make it constant because the Actor doesn't change, we just receive it.
        TotalMass += Actor->FindComponentByClass()->GetMass();
        UE_LOG(LogTemp, Warning, TEXT("%s is on the pressure plate"), *Actor->GetName())
    }

    return TotalMass;
}

Assigning proper weights to the trigger’s parameter and the various objects should now let us open the door by placing objects in the trigger.

At some point this will need to be refactored so that we can look for Entering values, and exiting values, but not necessarily have to keep doing the forloop every single frame if nothing has changed.

Lecture 85: Debugging Game Issues

The pawn is a sphere. It rolls with physics when we drop physics objects on ourselves. You can give it rotation constraints to prevent it from rolling.