#Custom RPC target

1 messages · Page 1 of 1 (latest)

viral gyro
#

Reading the docs but I'm still a little confused on how to actually use the custom targets since it doesn't provide an example. I'd like to send an RPC to both the server and owner of an object (host might also be owner). It's for a hidden roles game. Any help would be much appreciated, thank you!

mortal hollow
tame summit
#

You would use RpcTarget.Group() then add the Server Client ID and the Owner Client Id to the list

viral gyro
#

Right, but how do I construst that RPC target? I'm not sure how to use these BaseRpcTargets shown in the screenshot

viral gyro
tame summit
#

you have to initialize an array first

tame summit
#

might be easier to use a NativeList

#

or cache it if its just a 2 player game

viral gyro
# tame summit 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

tame summit
#

Oh I see yea. RpcTarget is part of NetworkBehaviour

viral gyro
#

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

tame summit
#

player.Rpctarget should work as well

viral gyro
#

I'm just worried that it will call it twice on the host

viral gyro
mortal hollow
#

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.

tame summit
#

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

viral gyro
#

I'm trying to write code that will work for both host and server

viral gyro
mortal hollow
viral gyro
#
            // 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?

mortal hollow
viral gyro
#

If you specify that only a server should be able to run an RPC, surely that helps matters?

viral gyro
mortal hollow
mortal hollow
#

Meaning if you use [RequireOwnership = true], only the server can call it.

viral gyro
#

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

mortal hollow
#

Ah, I see.

tame summit
#

I would flip it around and make the game manager a network object

mortal hollow
#

Best you can do in that case is probably just put if (!IsServer) return; at the top of the function.

viral gyro
viral gyro
mortal hollow
#

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.

tame summit
#

I use cloud code/save to manage game state in my card game prototype

viral gyro
#

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?