Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Punkjim420 · Jan 29, 2014 at 09:01 AM · dataonlinetransferplayers

C# cant figure out a way to send some info over network

i use C#

I cant figure out how to send info over a network, i have set up a network properly and can send chat data but i dont know how to attack somebody or "Get" somebodys name variable(string). you cant retrieve a string through 'stream.(ref value)', so how can i do this?

i dont fully understand RPCs and stream.serialize, but i have assumed it possible to target another player on another client, then check their name and health variables(Bob Hp: 100) and then if conditions were met, send an attack. by the way, yes i did read up on rpcs and serializing, and tutorials, and unity docs, no guide has an example i could use... sorry if my questions unclear, im a bit annoyed,(had this problem for several days)

Comment
Add comment · Show 4
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
avatar image Punkjim420 · Jan 29, 2014 at 09:04 AM 0
Share
 syncPlayerHp = playerHp;
 stream.Serialize (ref syncPlayerHp);

i have Health syncing between clients, using something like this,(stream.serialize), but how would you access it a specific instance of it when doing say..an attack on an enemy player thats controlling a seperate instance of a playerprefab on a non local client?

avatar image Punkjim420 · Jan 29, 2014 at 09:07 AM 0
Share

basically, im just using key input to increase/decress taget.playerHp, but it always changes the local playerHp rather than sending the message to the other(single or multiple) player(s).

this is because i tried using something like gameobject.name = playername, to make my player name Punkjim420, and if they attack me, theyd attack the game object called Punkjim420 by getting my name then setting it as their target, if i cant get and set, i cant script the other stuff..

avatar image nastasache · Jan 29, 2014 at 01:59 PM 2
Share

Just a direction (it's not quite simple): It's depends by players instantiation model (ex: Network.Instantiate() + serialize vs Instantiate() + RPCs). The 'owner' concept (who instantiate) of a piece is important. You have to look for NetworkView.viewID values to identify the right clone and look for correspondence NetworkPlayer (as owners of the pieces) - NetworkView. You may don't need to use stream.Serialize to attack, just find right owner (NetworkPlayer) of a NetworkView and send RPC to.

avatar image Punkjim420 · Jan 29, 2014 at 07:56 PM 0
Share

i'm going to the dentist in about 30 $$anonymous$$utes, after i return ill definitely try using your advice.

1 Reply

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

Answer by HuskyPanda213 · Jan 29, 2014 at 08:06 PM

RPC's are easy, just like send message.

They are sent to networkView of specified object(somegameobject.networkView.RPC), or just use networkView for the self object.

Think of it like this, no-one knows your name, an RPC is sent with your name(from you), in the receive call, everyone else gets your name, and then the yourname string is made into the sent name variable, You call the message by,

networkView.RPC("NameOfFunctionToCall",RPCMode.All, DataToSend); RPCModes are All(Sends to data to everyone) Other(to everyone but the sender), and Server(Just to server), also there are buffered calls.

To receive a call do this

[RPC] void NameOfFunctionToCall(DataToSend){ //Then do something in here. somethingtochange = DataToSend;// or equavelent. }

Keep in mind RPC's Cannot send every type of variable, but they can send unlimited amounts of a variable.

Variables that can be sent through RPC's

int float string NetworkPlayer NetworkViewID Vector3 Quaternion

Also, to sync your name, I guess you are a player gameobject, the RPC needs to be sent from you with name, and then received by appliying the local function variable as the name variable.

Comment
Add comment · Show 7 · 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
avatar image Punkjim420 · Jan 29, 2014 at 11:28 PM 0
Share

thank you for your reply, i just got back from the dentist, so my mouth hurts a lot, too much to code, ill definitely look into this as soon as i feel well again.

avatar image Punkjim420 · Jan 30, 2014 at 07:29 AM 0
Share

ok i tried this.. sing UnityEngine; using System.Collections;

 public class chat : $$anonymous$$onoBehaviour {
     
 
     private string chattext = "";
     private string line = "";
     private string playername = "";
 
     private void Update () {
         if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Return)){
             networkView.RPC ("UpdateChat",RPC$$anonymous$$ode.All,chattext,playername);
             chattext = "";
         }
         networkView.RPC ("UpdateName",RPC$$anonymous$$ode.All,playername);
 
         this.gameObject.name = playername;
     }
     void OnGUI(){
         playername = GUI.TextField (new Rect(0,1,200,20), playername);
         chattext = GUI.TextField (new Rect(0,Screen.height - 21,200,20), chattext);
         GUI.Label (new Rect(0,0,300,500),line);
         if (GUI.Button (new Rect (250, Screen.height - 20, 50, 20), "Clear")) {
                         line = "";
                 }
     }
     [RPC]void UpdateChat(string chattext, string playername){
         line = line + "\n" + playername + ": " + chattext;
         }
     [RPC]void UpdateName(string playername){
         playername = playername;
         Debug.Log (playername);
     }
 }

But it is not working, in the inspector the names never update but my own, (the character i control in unity others are controlled from built clients)

is there something im not doing the way you had said to. An this is my chat script not the player, but later i can call this script and get the variable from within player.cs when needed

avatar image Punkjim420 · Jan 30, 2014 at 07:31 AM 0
Share

by the way, the chat works, the name of others gets put into the chat but on my client the names of other clients do not update.

avatar image Punkjim420 · Jan 30, 2014 at 08:50 AM 0
Share
 using System.Collections;
 
 public class chat : $$anonymous$$onoBehaviour {
     
 
     private string chattext = "";
     private string line = "";
     private string playername = "";
 
     private void Update () {
         if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Return)){
             networkView.RPC ("UpdateChat",RPC$$anonymous$$ode.All,chattext,playername);
             chattext = "";
         }
         networkView.RPC ("UpdateName",RPC$$anonymous$$ode.All,playername);
 
         this.gameObject.name = playername;
     }
     void OnGUI(){
         playername = GUI.TextField (new Rect(0,1,200,20), playername);
         chattext = GUI.TextField (new Rect(0,Screen.height - 21,200,20), chattext);
         GUI.Label (new Rect(0,0,300,500),line);
         if (GUI.Button (new Rect (250, Screen.height - 20, 50, 20), "Clear")) {
                         line = "";
                 }
     }
     [RPC]void UpdateChat(string chattext, string playername){
         line = line + "\n" + playername + ": " + chattext;
         }
     [RPC]void UpdateName(string tempName){
         if (!networkView.is$$anonymous$$ine) {
             playername = tempName;
         }
     }
 }

This works but it shows the entered names in other clients behind their entered names..alt text

nameoverlap.png (23.6 kB)
avatar image Punkjim420 · Jan 30, 2014 at 09:14 AM 0
Share

using UnityEngine; using System.Collections;

public class chat : $$anonymous$$onoBehaviour {

 private string chattext = "";
 private string line = "";
 private string playername = "";

 private void Update () {
     if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Return)){
         networkView.RPC ("UpdateChat",RPC$$anonymous$$ode.All,chattext,playername);
         chattext = "";
     }
     networkView.RPC ("UpdateName",RPC$$anonymous$$ode.All,playername);

     this.gameObject.name = playername;
 }
 void OnGUI(){
     if (networkView.is$$anonymous$$ine) {
         playername = GUI.TextField (new Rect(0,1,200,20), playername);
     }
     chattext = GUI.TextField (new Rect(0,Screen.height - 21,200,20), chattext);
     GUI.Box (new Rect(0,Screen.height - 171,300,150),line);
     if (GUI.Button (new Rect (250, Screen.height - 20, 50, 20), "Clear")) {
                     line = "";
             }
 }
 [RPC]void UpdateChat(string chattext, string playername){
     line = line + "\n" + playername + ": " + chattext;
     }
 [RPC]void UpdateName(string tempName){
     if (!networkView.is$$anonymous$$ine) {
         playername = tempName;
     }
 }

}

this worked. i simply change the ongui to make it only show my name.

Show more comments

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

20 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

Related Questions

Lan Network 2 Answers

Sending newly created objets using Photon 0 Answers

Why can't my fifth/5th player join? Unity Networking 0 Answers

How to setup a lan system 1 Answer

Unity globally online data storage 3 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