Search code examples
c++unreal-engine4

UE4.9: Move character (not set location)


I am sending commands from an external application that I have written. This external application is sending the key pressed on the keyboard via UDP to UE4. When UE4 gets the message (the key that was pressed, eg: 'w'), I'd like to make the character walk forward (there is only 1 character).

while (true)
{

    int32 bytes_read = 0;

    if (Socket->Recv(data, sizeof(data), bytes_read)){
        data[bytes_read] = 0;

       //Should I use this, instead of SetActorLocation?
        APlayerController* playerController = World->GetFirstPlayerController();

        ACharacter* myCharacter = UGameplayStatics::GetPlayerCharacter(World, 0);
        FVector characterLocation = myCharacter->GetActorLocation();
        FVector newCharacterLocation = characterLocation + FVector(10, 0, 0);

        //This just moves the character, but does not walk and no collision is included
        myCharacter->SetActorLocation(newCharacterLocation); 

        //FPlatformProcess::Sleep(0.1);
    }
}

Note: the loop is executing on another thread

SetActorLocation() is not working for me as it moves the character, but is essentially just 'teleporting'. I want to instruct the character to move as if I were playing the game and controlling the character in a normal way.

I'd like to control the character via my external application. What should I do to make the character move, as if I was playing the game myself and pressed, eg: 'w', to move forward?


Solution

  • I figured it out...

      while (true)
        {
            int32 bytes_read = 0;
            if (Socket->Recv(data, sizeof(data), bytes_read)){
                data[bytes_read] = 0;
                ACharacter* myCharacter = UGameplayStatics::GetPlayerCharacter(World, 0);
                myCharacter->AddMovementInput(FVector(10, 0, 0), 5.0);
            }
        }