- Home /
The question is answered, right answer was accepted
Using RPC to send a List
I want to send my player list from my server to all players, but RPC doesn't support lists/arrays. what is the best way to get around this. keep in mind that the list couldn't simply add a player to each player's list each time, because when a player leaves the game, the list would get all out of order and it would add players that are already there to new players list.
Answer by whydoidoit · Sep 16, 2012 at 09:31 PM
You can use BinaryFormatter and Convert to store your list in a string - send it to the recipient and then get it back again.
I can't (for some reason I don't understand) find my previous answer to a similar question but I guess in JS it would look like this:
#pragma strict
import System.Runtime.Serialization.Formatters.Binary;
import System.Runtime.Serialization;
import System.IO;
var list = new List.<SomeClass>();
function Start () {
var o = new MemoryStream(); //Create something to hold the data
var bf = new BinaryFormatter(); //Create a formatter
bf.Serialize(o, list); //Save the list
var data = Convert.ToBase64String(o.GetBuffer()); //Convert the data to a string
//Reading it back in
var ins = new MemoryStream(Convert.FromBase64String(data)); //Create an input stream from the string
//Read back the data
var x : List.<SomeClass> = bf.Deserialize(ins);
print(x.Count);
}
You would send the string in data and use the second part to read it back again.
i'll convert this to C# and get back to you if it works.
It's a shame about needing to convert it to a string - but no chance of sending the byte array - oh well...
On reflection - you could probably use Encoding.ASCII.GetString() and .GetBytes() which would make the transmission format smaller - I tend to use Convert because that works to web services.
i dont think the transmition format matters in my case since there are only so many players per game server, and the data is only sent when a player join/leaves/gets kicked. here is a video demonstrating the server system: http://www.youtube.com/watch?v=6L5$$anonymous$$fa0wQWQ
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Dynamic Enemy Transform List 1 Answer
How can you do calculations on two lists? 1 Answer
Data overriding itself? 2 Answers