Gun-Refactoring VR nightmare

Not sure if it’s because I’m on a later version of UE4 or because I have an Oculus setup (and UE4 detected it and compensated for it), but my FirstPersonCharacter .h and .cpp are very different from Sam’s version.

There’s a ton of code in there supporting motion controls for the gun. Even worse, Sam’s trick of moving the OnFire() binding into the BeginPlay() function didn’t work on my end - resulted in compile-time error.

I ended up just giving up and copying the complete files from the lesson’s cpp on the course github. Lost the VR support, but at least it works now with the gun as an actor. Bummer!

That’s fine for now. You could try and persist further at a later point to see if you can make the more general techniques work in the VR file.

1 Like

Hi,

You can still do the refactoring, you just need to do the VR components as well.

from the constructor you want to move the FP_Gun bits, the FP_MuzzleLocation bits as per Sam’s video. You do however also wawnt to move the motion controller bits, VR_Gun bits and the VR_MuzzleLocation bits. Also move the using motion controllers bool too.

The problem with the BeginPlay is that you need to reference the Mesh1P to activate the correct gun FP or VR. Just create a public function in AGun to either turn on VR or not which accepts the Mesh as a pointer.

void AGun::SetVROrNoVR(USkeletalMeshComponent* Mesh1P)
{
    // Show or hide the two versions of the gun based on whether or not we're using motion controllers.
    if (bUsingMotionControllers)
    {
        VR_Gun->SetHiddenInGame(false, true);
        Mesh1P->SetHiddenInGame(true, true);
    }
    else
    {
        VR_Gun->SetHiddenInGame(true, true);
        Mesh1P->SetHiddenInGame(false, true);
    }
}

Then just call it from the BeginPlay of the FirstPersonCharacter like this:

Gun = GetWorld()->SpawnActor<AGun>(GunBlueprint);
Gun->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint"));
Gun->SetVROrNoVR(Mesh1P);
Gun->AnimInstance = Mesh1P->GetAnimInstance();

I hope this helps you out. Unreal 4.16 has very different source due to the inclusion of VR and IWYU, but you should still be able to use what you have learnt up to now to still make it work with that version.

All the best,

Justin

1 Like

Privacy & Terms