Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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

Force local client RPC to fire immediately? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges