Actor wont go hidden in game, Client

I spawn my primary and secondary weapon in game, i set the secondary to hidden on begin and that works. I have a weapon switching system so when the player presses 1 or 2 it will switch between the weapons toggling the primary or secondary hidden/visible. the problem is when switching between weapons the primary weapon does not go hidden but the secondary weapon works fine.

I have tried doing it on the client and server, just getting into replication.



void AFpShooterCharacter::Server_ChangeWeapon_Implementation()
{
    if (CurrentWeapon == nullptr) { return; }
    if (bCanChange) {

        if (PrimaryAnimInstanceClass && SecondaryAnimInstanceClass != NULL) {

            //If the current weapon is the Primary Gun it will switch to the Secondary Gun

            if (CurrentWeapon == PrimaryWeapon) {
                PrimaryWeapon->SetActorHiddenInGame(true);
                SecondaryWeapon->SetActorHiddenInGame(false);
                Mesh1P->SetAnimInstanceClass(SecondaryAnimInstanceClass);
                CurrentWeapon = SecondaryWeapon;

                //If the current weapon is the Secondary Gun it will switch to the Primary Gun
            }

            else if (CurrentWeapon == SecondaryWeapon) {
                PrimaryWeapon->SetActorHiddenInGame(false);
                SecondaryWeapon->SetActorHiddenInGame(true);
                Mesh1P->SetAnimInstanceClass(PrimaryAnimInstanceClass);
                CurrentWeapon = PrimaryWeapon;

            }

            bCanChange = false;
            GetWorldTimerManager().SetTimer(TimerHandle_ChangeWeapon, this, &AFpShooterCharacter::SetCanChange, 1.0f);
        } else {
            UE_LOG(LogTemp, Error, TEXT("Missing a weapon Anim class for the player character"))
        }
    }
}


Gif : https:///635643d9d0ec9f2d52e26460a3a2b13f

Hidden In Game doesn’t replicate. Use Set Visibility instead.

Hidden In Game does replicate:



void AActor::SetActorHiddenInGame( bool bNewHidden )
{
    if (bHidden != bNewHidden)
    {
        bHidden = bNewHidden;
        MarkComponentsRenderStateDirty();
    }
}

    DOREPLIFETIME( AActor, bHidden );


Set Visibility only works on components, so isn’t any use here. It’s hard to debug why this isn’t working without the code in front of you unfortunately, but your best bet is to output visibility to the screen on client and server and try to find out if and why it’s not being updated.

Looking at the way it works, it’ll only set it to hidden properly if it’s not already hidden…



void AActor::PostNetReceive()
{
    if (!bNetCheckedInitialPhysicsState)
    {
        // Initially we need to sync the state regardless of whether bRepPhysics has "changed" since it may not currently match IsSimulatingPhysics().
        SyncReplicatedPhysicsSimulation();
        SavedbRepPhysics = ReplicatedMovement.bRepPhysics;
        bNetCheckedInitialPhysicsState = true;
    }

    ExchangeB( bHidden, SavedbHidden );
    Exchange ( Owner, SavedOwner );

    if (bHidden != SavedbHidden)
    {
        SetActorHiddenInGame(SavedbHidden);
    }
    if (Owner != SavedOwner)
    {
        SetOwner(SavedOwner);
    }
}


Solution

SetHiddenInGame replicates at the Actor level, but it doesnt if you change it at the **component **level instead.

So you can get the client to do whatever you want if you use USceneComponent::SetHiddenInGame instead of the Actor equivalent.

:heart:

Rama

Thanks for all these solutions. I managed to fix it the problem was when i started as a client two guns would spawn (gun, gun 1) so it was hiding the gun but because two spawned the other just sat there. i fixed by checking authority.