Why aren't my Server functions being called?

Hello, i have a Item class (that derives from AActor) that has the following server function:

UFUNCTION(Server, Reliable, WithValidation)
virtual void ServerOnPickedUp(class ACharacter * Character);

When the client picks up the item i call this to tell the server that it was picked up by a character. Previously i had this on my character code and it worked, but now on AItem i call ServerOnPickedUp(Character); and when i’m running on the client it doesn’t call the server function. Do i have to activate some kind of option for this class to use server functions? Character server functions still work. Thank you.

RPCs only run on classes with NetConnections. You should override GetNetConnection on your item and have it return the characters netconnection if one exists.

Thank you for the answer. I call SetOwner before the Server RPCs, so after that point it has a valid connection, but it still doesn’t execute on the server.

Did you mark the class bReplicates = true?

Also, it wouldn’t hurt to view this set of slides if you haven’t already:

http://www.slideshare.net/JoeGraf1/ue4-networking

Yes bReplicates is true, everything was working when i had the logic on the character. So there must be some variable or function i have to override to say “This actor can do Server RPCs”.

Just checking here, but you spawn the actor on the server and set its owner then?

The item is on the the level itself so it’s spawned from the server automatically, and when the client presses F when looking at the item i set the owner and call the server function on the item. That never gets called and he never picks up the item. If i run on single player it works.

that ownership server side there won’t be a valid connection, which explains the Server RPCs not working

That’s why I asked if you changed the owner on the server. The server needs to know to listen for that actor on a given connection and that is only possible if the call happens on the server

The problem is that objects that are spawned from the level itself don’t have an owner, and unless something changes that ownership server side there won’t be a valid connection, which explains the Server RPCs not working. On the character i need to at least run a Server RPC on itself where i pass the item as a parameter and there i just set the ownership. After that the RPCs on the item just start working.

I answered my question, but thanks for helping me get there :slight_smile:

I did SetOwner but i was doing it on the client, silly me :stuck_out_tongue:

Can you tell me another thing? How can i call server functions and client rpc on a character that is controlled by AI? They don’t have an owner and have an AI Controller, which doesn’t have a connection.

You make the call on their corresponding PlayerState class. All players (AI included) will have a player state object on each client.

But the Server and Client RPCs i’m trying to call are from common behavior between the enemies and the player character, stuff like ServerPickupItem and so on. How can i call them from the player state?

The Unreal Architecture docs says:

A PlayerState is the state of a participant in the game, such as a human player or a bot that is simulating a player. Non-player AI that exists as part of the game would not have a PlayerState

I discovered the bWantsPlayerState variable from the Controller class so i set it to true on my AIController, so now it’s receiving client rpc’s on the player state. I did the following code to forward the RPC:

void ABasePlayerState::EquipWeapon(class AWeapon * Weapon)
{
	class AController * Controller = this->GetInstigatorController();

	if (Controller != nullptr)
	{
		class AAICharacter * Character = Cast<class AAICharacter>(Controller->GetPawn());

		if (Character != nullptr)
			Character->ClientEquipWeapon(Weapon); // NetMulticast RPC
	}
}

It appears to work, but is this the proper procedure? I have to use the PlayerState as a relay for RPC that i want my AI to do? This does not seem to be very practical, especially on cases where i just want to change the value of a variable on the server and make it replicate automatically, but without a connection that doesn’t work, so i would have to implement a setter on the PlayerState. Am i missing something?

This solved the issue for me, didn´t knew the owner was so important for RPC methods.

My sidde on this.If you possess anything server rpcs will work on that possessed thing.If I possess a car then server rpc will work on that car but not with my player.I hope you get it,That is the reason for the server calls not being called