#Custom RPC target
1 messages · Page 1 of 1 (latest)
You use [Rpc(SendTo.SpecifiedInParams)] and pass in a RpcTarget with the IDs you want.
You would use RpcTarget.Group() then add the Server Client ID and the Owner Client Id to the list
Right, but how do I construst that RPC target? I'm not sure how to use these BaseRpcTargets shown in the screenshot
Just use RpcTarget.Group instead of Single as noted above
Great thanks, do you know why the following doesn't work, and does the server have a client id?
you have to initialize an array first
I have. It's because RpcTarget is a class, not an object, and Group is a non static method. I think it's because I'm referencing from a non networkbehaviour class, so just getting the one from the network manager
Oh I see yea. RpcTarget is part of NetworkBehaviour
this should work now. Is it okay to call an rpc on a network object that was just spawned on the network?
And will it matter if the owner is the host? The 2 ids would then be the same
player.Rpctarget should work as well
I'm just worried that it will call it twice on the host
yeah that's nicer
You can use [AllowTargetOverride = true)] and just send to one ID if the IDs are the same and you are worried about a repeat send.
Not sure if it would actually send twice though.
Addcard sounds like it should only be something run on the host anyways
Also keep in mind for card games, there is no hiding information from the host
Yeah I'm aware
I'm trying to write code that will work for both host and server
I'll have to test. My guess is that it won't, but I can always turn it into a set if needed
There's not much to consider when doing this. Just avoid using Host specific properties like IsHost and that's about it.
I want to add the true value of the card to both the server and owner, whilst an 'unkown' value is added to every other client
// Create players and deal cards
foreach (ulong clientId in NetworkManager.Singleton.ConnectedClientsIds)
{
// Spawn player on network
QUOP_Player player = NetworkManager.Singleton.SpawnManager.InstantiateAndSpawn(playerPrefab.GetComponent<NetworkObject>(), clientId, true).GetComponent<QUOP_Player>();
for (int i = 0; i < starting_cards; i++) // Deal starting hand
{
CardType card = deck.Draw();
ulong[] targets = { player.OwnerClientId, NetworkManager.ServerClientId };
// Send true card to server and owner
player.AddCardRpc(card, player.RpcTarget.Group(targets, RpcTargetUse.Temp));
// Send unkown to everyone else
player.AddCardRpc(CardType.UNKNOWN, player.RpcTarget.Not(targets, RpcTargetUse.Temp));
}
}
so I think that should now do I what I want
Hard to read, sending a screenshot instead
(This is all ran from the server)
What's stopping the owner of the object calling this rpc?
I only want the server to be able to run it
like is it possible to have a hacked client which runs this rpc?
You can use [RequireOwnership = true]
I mean once you have a hacked client, the cat is out of the bag. Not much you can do.
If you specify that only a server should be able to run an RPC, surely that helps matters?
I don't see any option to make is server only?
Yep! Anti-cheat is about heightening the barrier to entry for cheating, not full prevention. Any sort of potential prevention is always good.
If this NetworkBehaviour is spawning players, it should be owned by the server.
Meaning if you use [RequireOwnership = true], only the server can call it.
It's an rpc in a player object that adds a card to that players hand
the scipt that spawns the players and manages the game is just a MonoBehaviour
Ah, I see.
I would flip it around and make the game manager a network object
Best you can do in that case is probably just put if (!IsServer) return; at the top of the function.
Yeah maybe that's the way of going about it? I'll have to have a bit of a think
Wait that still wouldn't work as I need function to do stuff on clients
Or yeah, make the Game Manager a NetworkObject and make this RPC inside of the Game Manager. Technically something like a Game Manager should handle the networking and your player script should just just receive the result of the RPC and create the visual for the card.
I use cloud code/save to manage game state in my card game prototype
I might do network object for both player and game manager, and then a monoscript for display based on player. Player object does need to call some server RPCs to tell server what action it is doing
Thanks for help both of you anyhow, much appreciated! Last question, is there a problem calling an RPC on a network object right after it is spawned?