- Home /
Networking Question: Spawning selectively / on only one client.
Hello everyone,
I am currently learning networking in Unity for a university project and I slowly feel like I am getting a good grasp of the basics.
I am implementing a turn based digital Trading Card Game. My idea was to handle the decks server side until a player actually needs to see the cards. Now instead of spawning the cards for all players and hiding them for the opponents when they are drawn I want to implement it like this (for anti-cheating and whatever reasons, imo this makes more sense):
[Server]
void OnCardDraw() {
card.MoveToHand();
NetworkServer.Spawn(card) //FOR OWNER OF CARD ONLY
}
[Server]
void OnCardPlay() {
card.MoveToField();
NetworkServer.Spawn(card) //FOR OTHER PLAYER
}
So now my question is: Is this possible? If yes, how? If not, how would you handle this so the other client gets the smallest possible amount of information about the card. For now I don't care if the opponent can see my hand with the backs of my cards.
I'm really hitting a wall here
Thanks a lot in advance! I really appreciate it!
Answer by EpiFouloux · Jun 15, 2016 at 08:23 AM
here are the basics :
[Command] // called from a client, executed on Server
[ClientRpc] // Called from server, executed on clients
bool isLocalPlayer // is it the local player ?
so when playing a card, here what it should look like :
public class player : NetworkBahaviour
{
void Update()
{
if (!isLocalPlayer)
return ;
// you are on the local player
// handle input
play(); // place conditions of course
}
void play()
{
Cmd_play_a_card(card_id);
}
[Command]
private void Cmd_play_a_card(int id)
{
// this is executed on the server version of this player instance
Rpc_move_card(id)
}
[ClientRpc]
private void Rpc_move_card(int id)
{
if (isLocalPlayer)
moveToHand(id);
else
moveToField(id);
}
}
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Players Not Showing 0 Answers
Is it possible to create network objects from editor 0 Answers