I am writing a networked game and I need to send lists between two clients. But I cannot find a way to attack a list to a PyDatagram.
you can use pythons pickle module which can convert almost anything into a string. (except things like vec3 which is custom-panda)
you can send those strings with the string datagramthingy.
datagram = PyDatagram() datagram.addString( pickle.dumps( yourList , 2) ) self.cWriter.send( datagram, self.Connection )
you can reconstruct the list on the other end with
if self.cReader.dataAvailable(): datagram = NetDatagram() if self.cReader.getData(datagram): pkg = PyDatagramIterator(datagram) yourList = pickle.loads( pkg.getString() )
if you’r trying to send lists which contain non-standart-python data-types python will complain. in most cases you can use standart stuff. like a list (x,y,z) instead of the vec3 position.
you can also compress those strings to reduce traffic.
aCompressedString = zlib.compress( anUncompressedString)
anUncompressedString = zlib.decompress( aCompressedString )
Thomas, why not using cPickle ? It’s a lot faster than pickle, right ?
I have a simple benchmark of them :
aList= for n in range(5000000): aList.append('ABCDE') myFile=open('abcde','w') #pickle.dump(aList,myFile) # 32 seconds cPickle.dump(aList,myFile) # 7 seconds myFile.close()
well i use it only 5 times per second to send a few dozen characters… so i wouldnt notice^^
thx for the hint in case i run into problems with 100000 players on my server wandering around at the same time i’ll remember it
Thanks that worked very well.