- Home /
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)
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?
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..
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.
i'm going to the dentist in about 30 $$anonymous$$utes, after i return ill definitely try using your advice.
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.
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.
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
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.
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..
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.
Your answer
Follow this Question
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