How to continue using `IsLocallyControlled()` after this lecture's refactor

If you heeded Sam’s warning a few lectures ago about using GetRemoteRole() and are using IsLocallyControlled() instead, you may have encountered an error during this lecture when you moved those method calls to either the movement component or the replicator component and tried accessing IsLocallyControlled() via GetOwner() (i.e. GetOwner()->IsLocallyControlled():

error C2039: 'IsLocallyControlled': is not a member of 'AActor'

This is because GetOwner() returns an AActor, not an APawn which is what defines IsLocallyControlled().

I added a helper function to both the movement and replicator components to redefine the use of IsLocallyControlled() within those controllers (I found this in a forum post here):

bool UGoKartMovementReplicator::IsLocallyControlled()
{
    auto* Owner = Cast<APawn>(GetOwner());

    if (!Owner)
    {
        return false;
    }

    return Owner->IsLocallyControlled();
}

After changing that I’m still able to avoid the quirkiness of GetRemoteRole() and things appear to be happy thus far. Note that this only works because we are confident that the owner of these components is an APawn. If someone were to try to use these components with a different owner class type it could potentially break. Let me know if you still have issues or have another approach!

1 Like

Thanks for sharing. Just FYI, you do not need auto*, just auto because it will make it a pointer automatically.

1 Like

Privacy & Terms