• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ZoeySuzu · Sep 10, 2018 at 08:13 PM · networkingmultiplayerrpcclienthost

Requesting data from server

Hi all, I've recently been experimenting with the networking system and have been wondering what the best way for a client to retrieve specific data from the host-server.


Here is some context: A host player has a list of cards (Serializable) and the client need to draw cards from this shared deck to their unique hands and can then send the data back to be used by the host.


So far I understand that it is possible to use RPC calls and can effectively use them to pass card data to the server, I can do the same to call request function on the server, however I'm not quite sure how to get the server to pass the data back to the specific client.


I am considering sending some form of ID via the request to draw a Card but I've been wondering if there was a simpler solution to this problem.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ZoeySuzu · Sep 11, 2018 at 05:47 PM

Okay after working on it throughout the day I realised I was going about it the wrong way.

The way I solved it was by using the NetworkMessage class.

I did this by creating my own derivation of the MessageBase class :

 using UnityEngine.Networking;
 
 public class MessageData : MessageBase
 {
     public short id;
     public MessageData() { id = 0; }
     public MessageData(CardData data){id = data.id;}
 }

And the used this class in my client and host code to transfer the data to each other using Handlers. Here's the client side:

 private void OnMatchJoined(){
 //Other code here
  myClient = new NetworkClient();
  myClient.RegisterHandler(ZMsgType.DrawCard, OnReceiveCard);
 }
 
     public void RequestCard()
     {
         MessageData msg = new MessageData();
         myClient.Send(myMsgType.RequestCard, msg);
     }
     public void SendCardData(CardData cd)
     {
         MessageData msg = new MessageData(cd);
         myClient.Send(myMsgType.UseCard, msg);
     }
 
     private void OnReceiveCard(NetworkMessage netMsg)
     {
         MessageData msg = netMsg.ReadMessage<MessageData>();
          //Find cardData from prefab and add to hand
         CardData data = deck.deckData.cardList[msg.id-1];
         hand.AddCard(cd);
     }

And here's the Host side:

 public void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
     {
 //Other code here
             NetworkServer.RegisterHandler(myMsgType.RequestCard, OnRequestCard);
             NetworkServer.RegisterHandler(myMsgType.UseCard, OnSentCardData);
 }
 
     private void OnRequestCard(NetworkMessage netMsg)
     {
         CardData data = deck.DrawCard();
         MessageData msg = new MessageData(data);
         NetworkServer.SendToClient(netMsg.conn.connectionId, myMsgType.DrawCard, msg);
     }
 
     private void OnSentCardData(NetworkMessage netMsg)
     {
         //Game Logic here
     }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

176 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

clientrpc function gives null reference after client disconnects from host 0 Answers

Multiplayer problem 1 Answer

Using .SetActive() over Unity Network for multiplayer. (Enable weapons on clients) 1 Answer

Networking sending info only one direction 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges