Hello.
I’ve been trying to solve this issue for a while, but without any success.
THE PROBLEM
When the weapon with which my character is shooting gets out of ammo, I want my character to automatically switch weapon.
So, in the** AWeapon::Shoot** function everytime a shot is performed there is an if-statement that checks whether the weapon got out of ammo.
If yes, the function AMyCharacter::ForceWeaponSwitch() is called on the **Owner **of that weapon (which is stored in a variable)
void AWeapon::Shoot()
{
if (Role < ROLE_Authority)
ServerShoot();
else
{
FireWeapon();
DecreaseAmmo();
// Check if out-of-ammo. If yes, switch weapon
if (MyOwner->GetAmmo(WeaponConfig.PrimaryAmmo) == 0)
{
StopFire();
MyOwner->ForceWeaponSwitch();
return;
}
}
}
As you can see the function is executed on the Server via a **ServerRPC **call (ServerShoot)
**MyOwner **is the **AMyCharacter **which **owns **the weapon
And this is the AMyCharacter::ForceWeaponSwitch function:
void AMyCharacter::ForceWeaponSwitch()
{
**EquipWeapon**(EWeaponType::Pistol);
}
########################
// The following function just uses the **FindWeapon()** function to cycle through my **TArray **of **AWeapon*** and, once found, sets it as the new **CurrentWeapon**
void AMyCharacter::EquipWeapon(EWeaponType toEquip)
{
AWeapon* theWeapon = FindWeapon(toEquip);
if (theWeapon && theWeapon->GetIsEnabled() && GetAmmo(theWeapon->GetPrimaryAmmo()) != 0)
{
*// this is the crucial step!!!*
**CurrentWeapon = theWeapon;**
}
}
THE RESULT
The result? Nothing.
Or better: nothing on the client!
On the Server window, when my Weapon goes out of ammo the WeaponSwitch succeffully happens.
On the Client window, it doesn’t.
"ARE YOU REPLICATING PROPERLY?"
These items are replicated in this issue:
- AMyCharacter class
- AWeapon class
- AMyCharacter -> CurrentWeapon
- AMyCharacter -> WeaponInventory
- AWeapon -> MyOwner (replicated via OnRepNotify function)
FUN-FACTS!
-
The **ForceWeaponSwitch() **function is succeffully called and executed by the ServerAuthority when the weapon gets out of ammo
-
If in the ForceWeaponSwitch() function I change another variable (for example: bCanMove = false) that variable gets changed succeffully! Why isn’t it working with my CurrentWeapon?
Any help?