Hi! I’m trying to spawn a replicated actor through a Server UFUNCTION. I can change its properties inside the function that actually spawns it, but when I try to do anything outside it I get a null reference.
Here’s my code:
Survivor.h
UPROPERTY(BlueprintReadOnly, EditAnywhere, Category = "References", Replicated)
class AWeapon* CurrentWeapon;
UFUNCTION(WithValidation, Server, Reliable, BlueprintCallable)
void FInput(TSubclassOf<AWeapon> WeaponToSpawn);
Survivor.cpp
bool ASurvivor::FInput_Validate(TSubclassOf<AWeapon> WeaponToSpawn)
{
return true;
}
void ASurvivor::FInput_Implementation(TSubclassOf<AWeapon> WeaponToSpawn)
{
if (CurrentWeapon == nullptr && HasAuthority())
{
FAttachmentTransformRules TransformRules = FAttachmentTransformRules(EAttachmentRule::KeepRelative, EAttachmentRule::KeepRelative, EAttachmentRule::KeepRelative, true);
CurrentWeapon = GetWorld()->SpawnActor<AWeapon>(WeaponToSpawn);
CurrentWeapon->AttachToComponent(GetMesh(), TransformRules, FName(TEXT("weapon_AK47")));
D("finput sent");
}
}
This works fine. The problem is when I try to acess the CurrentWeapon after it has spawned.
Survivor Blueprint:
After the weapon is attached and I try to shoot:
Not sure why, this only happens outside the FInput function.
Hope someone can help! Thanks.