But I can’t get it to work. I can’t call RPC from the UObject. So I wonder, is it still possible to do this? My code looks kinda similar, UObject is exactly the same, and I’m calling its RPC from the Pawn, which is its outer
Yes, it works just fine. Check you followed the tutorial completely.
As I said in the article though, I really don’t recommend it. If you already have a Pawn as the outer, just use an Actor Component. Components should be the preference if they are an option, there’s little to no benefit replicating the object.
I sit on it like for 2 hours yet, and I can’t figure out, what am I doing wrong:
I have such code:
But when I try to call TestUse its getting called on the server, and TestUse2 it not executed at all. I set owner to my pawn, I set object reference as replicated, I put it into DOREPLIFETIME, but without any satysfying results
UCLASS()
class CCO_API UExtendedObject : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(Client, Reliable)
void TestUse();
UFUNCTION(Server, Reliable)
void TestUse2();
// Allows the Object to get a valid UWorld from it's outer.
virtual UWorld* GetWorld() const override
{
if (const UObject* MyOuter = GetOuter())
{
return MyOuter->GetWorld();
}
return nullptr;
}
UFUNCTION(BlueprintPure, Category = "My Object")
AActor* GetOwningActor() const
{
return GetTypedOuter<AActor>();
}
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override
{
// Add any Blueprint properties
// This is not required if you do not want the class to be "Blueprintable"
if (const UBlueprintGeneratedClass* BPClass = Cast<UBlueprintGeneratedClass>(GetClass()))
{
BPClass->GetLifetimeBlueprintReplicationList(OutLifetimeProps);
}
}
virtual bool IsSupportedForNetworking() const override
{
return true;
}
virtual int32 GetFunctionCallspace(UFunction* Function, FFrame* Stack) override
{
check(GetOuter() != nullptr);
return GetOuter()->GetFunctionCallspace(Function, Stack);
}
// Call "Remote" (aka, RPC) functions through the actors NetDriver
virtual bool CallRemoteFunction(UFunction* Function, void* Parms, struct FOutParmRec* OutParms, FFrame* Stack) override
{
check(!HasAnyFlags(RF_ClassDefaultObject));
AActor* Owner = GetOwningActor();
UNetDriver* NetDriver = Owner->GetNetDriver();
if (NetDriver)
{
NetDriver->ProcessRemoteFunction(Owner, Function, Parms, OutParms, Stack, this);
return true;
}
return false;
}
/*
* Optional
* Since this is a replicated object, typically only the Server should create and destroy these
* Provide a custom destroy function to ensure these conditions are met.
*/
UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly, Category = "My Object")
void Destroy()
{
if (!IsPendingKill())
{
checkf(GetOwningActor()->HasAuthority() == true, TEXT("Destroy:: Object does not have authority to destroy itself!"));
OnDestroyed();
MarkPendingKill();
}
}
protected:
virtual void OnDestroyed()
{
// Notify Owner etc.
}
};