RPC in ActorComponent issue

I have a problem with rpc in actor component. I have Test() function, Server_Test() function and Multicast_Test() function in my actor component class.

	void Test();
	UFUNCTION(Server,Reliable)
	void Server_Test();
	void Server_Test_Implementation();

	UFUNCTION(NetMulticast,Reliable)
	void Multicast_Test();
	void Multicast_Test_Implementation();

Here is the constructor:

UBaseActorComponent::UBaseActorComponent()
{
	PrimaryComponentTick.bCanEverTick = false;
	SetIsReplicated(true);
	SetIsReplicatedByDefault(true);
}

I call Test() function in the BeginPlay(). Here is the implementation:

void UBaseActorComponent::BeginPlay()
{
	Super::BeginPlay();
	Test();
}

void UBaseActorComponent::Test()
{
	if(GetOwner()->HasAuthority())
	{
		UE_LOG(LogTemp,Error,TEXT("HasAuthority"));
	}

	else if(!(GetOwner()->HasAuthority()))
	{
		UE_LOG(LogTemp,Error,TEXT("CLIENT"));
		Server_Test();
	}
}

void UBaseActorComponent::Server_Test_Implementation()
{
	UE_LOG(LogTemp,Error,TEXT("Server Test Called %s - %s "),*GetName(),*GetOwner()->GetName());
	Multicast_Test();
}

void UBaseActorComponent::Multicast_Test_Implementation()
{
	UE_LOG(LogTemp,Error,TEXT("Multicast Test Called %s - %s "),*GetName(),*GetOwner()->GetName());
}

In blueprint, set replicated also marked as ticked. I start the game in Listen Server with two players:
editor

And this is the output log:

1 - Why HasAuthority and CLIENT log printed twice?
2- Why am i getting Server_Test will not be processed error?
3- After the error, how is replication functions log printed?
4- Are Game Sessions logs important?

Apparently, i am doing something wrong. What should i do?
(I have to make those RPC’s in actor component.)

Thanks a lot in advance.

It must only be void Server_Test(); in .h and void Server_Test_Implementation(); in .cpp. You mustn’t duplicate this function.

Result is Compiler error :frowning:

Server_Test_Implementation is not a member of UBaseActorComponent.

Just to be sure. You need to have:
.h:

UFUNCTION(Server, Reliable)
void Server_Test();

.cpp:
void Server_Test_Implementation();

Yes, same. this causing compiler error.

Ok, so it only happens in the actor component?
If you do the same in the character or in player controller, does it work?

P.S. Note that server functions won’t work in just any random class you create. It should compile, but won’t work.
Server functions will work in controllers, pawn/characters possessed by controllers, and if I’m not mistaken, Game State and Player State, but I’m not really sure about the last two.
You can’t call a server function in an actor placed in the level.

1 Like

Thanks a lot for answers. I understand. My research tells me exactly what you said. Is there a way for process a rpc from not possessed vehicle ? 3 different players have to control different sections of same vehicle. There are plenty of static meshes, and i use on clicked events. How can i accoomplished this?

You’ll need to get reference to a class that can call RPC functions and work through that.

For instance, in one of my projects I handle server function in the player controller, and these functions have actors as parameters. You send the pointer over to the server side of the controller, and there you can do whatever you want with it.

1 Like

Thx a lot. I am gonna try that. There are a lot meshes and and a lot of on clicked events for every clickable meshes. Is this means in the server side of player controller, there are lots of functions with rpc?

And just one thing: I am getting this warning 3 times for start a game for 2 players: " LogOnlineSession: Warning: OSS: No game present to join for session (GameSession)"
Why is that happen?

Looks like you added your component to the Character class.

You are launching game with two players, one who is the server and one who is a client.

When first viewport (server player) begins play, both his character and the character of the other player log “Has Authority”.

When second viewport (client player) begins play, both his character and the character of the first player log “CLIENT”. This viewport only owns his own character though, so the Server RPC only works there. When client tries to call Server_Test on the character of other player it doesn’t work.

The working RPC is routed to viewport 1 (server player), which then calls Multicast, which executes on both viewports.

Thanks for the reply. For testing purposes, i added component to possesed character and logs are like in the screenshot. But i need my component in another class. When i delete compoent character and add to desired class, Server_Teset and Multicast_Test functions does not work because class has no owner.

Multicast should work without owner, if it’s called from server, as long as the actor is replicated.

Components obey the same rules as actors when it comes to replication. You cannot do server RPC in component if you (the calling client) do not own the actor.

If your goal is to place actors in the Level, with a component to handle some sort of replicated interaction, you should probably either use an interface (directly implemented in the actor), or use the component as an interface. Execute the Server RPC in player controller/character class where it works, passing the actor as parameter, then in server function call your interface/component function via the passed actor parameter.

2 Likes

How can i use component as an interface? I have a lot of meshes to interact. (One component for each mesh, and in vehicle actor, i add those components) And i use MeshOnClicked events for interaction in the component.
Or is there a way or is it possible to own a non possesed actor for RPC? Thanks again.

Yes, Ownership only has an effect on replication while “Possess” is a pawn concept that ensures that you only ever Possess one at a time while also keeping ownership updated.

If I set an owner for actor and do not possessed it, will RPC functions in components work?
If it is gonna work, how can I set ownership correctly?

Yes if you set the Owner from the Server and wait for the ownership to be updated on the Client then it will work.

1 Like

It works. Thanks a lot.