Having some trouble with a multiplayer tutorial

Hi! I’m working on a game for a class project. I’m new to C++ and Unreal Engine, and I’ve been doing fine so far using tutorials, but now I’ve run into something that’s stumped me.

I’ve been using the following information to attempt to make a multiplayer where two players can use the same keyboard (one with WASD, one with arrow keys).

There’s a bit of code that isn’t working so far, though:

int32 id = GetLocalPlayer()->GetControllerId();

The compiler doesn’t appear to recognize GetLocalPlayer(), and when I try other things, I get a “nonstatic member function must be relative to a specific object” error, and when I make it relative to a specific object it isn’t an int32 and I get an error in the next line. I’ve looked at everything on google that’s even remotely related but none of the solutions seem to apply (or if they do apply, they’re above my current level of understanding and not specific enough for me to know how to implement them).

The one problem I can think of is that I’ve included Engine.h in my Avatar class rather than “the main header file” as the tutorial recommends, but I have no idea what it’s referring to with regards to the main header file, I wasn’t able to find an answer on google, and I wasn’t able to find the “EngineMinimal.h” I was supposed to replace anywhere in the project.

Is there a simple solution to this problem, or should I find a different way to implement local multiplayer on one keyboard? Using gamepads isn’t an option.

I really appreciate any help – it won’t affect my grade if I can’t figure out this multiplayer, but I think it’s essential to the game and I’m determined to figure it out if it’s the last thing I do. Thank you!

Your main header file is named the same as your project. As an example, if your project is named “Shooter”, your main header would be Shooter.h.

Depending on how you set your project up, EngineMinimal.h may be included; though it could be Engine.h.

Thank you for your quick response! That is helpful, and I was able to find the main header file. However, it looks like Engine.h is already included, so that can’t be the problem.

Here’s the code in the SetupPlayerInputComponent section that is giving me an error:

int32 id = GetLocalPlayer()->GetControllerId();
if (id == 0)
{
InputComponent->BindAxis(“Forward_P1”, this, &APlayerAvatar::MoveForward);
InputComponent->BindAxis(“Strafe_P1”, this, &APlayerAvatar::TurnRight);
}
else if (id == 1)
{
InputComponent->BindAxis(“Forward_P2”, this, &APlayerAvatar::MoveForward);
InputComponent->BindAxis(“Strafe_P2”, this, &APlayerAvatar::TurnRight);
}

“GetLocalPlayer()” is giving me the error ‘identifier “GetLocalPlayer” is undefined.’ However, when I go to the definition, it exists in PlayerController.h. Is there a reason I’m running into this problem? Apologies if this is a really simple C++ issue; I’m very new to this.

Thanks again for your help!

Are you calling SetupPlayerInputComponent from the Character?

Yes, I am. Everything else about SetupPlayerInputComponent is working perfectly, and I don’t have any issues in single player at all. It’s only when I have that line with GetLocalPlayer() that I get an error.

I don’t believe the Pawn/Character has the GetLocalPlayer( ) function.

You will need to get the local player from the characters player controller, like:



APlayerController *PC = Cast<APlayerController>(GetController( ));
if(PC)
{
    ULocalPlayer *LocalPlayer = PC->GetLocalPlayer( );
    if(LocalPlayer)
   {
       int32 id = LocalPlayer->GetControllerId( );
       if(id == 0)
       {
       }
       else if(id == 1)
       {
        }
    }
}


It works! Thank you so much, Kyle! You’re a lifesaver.