Can PyDatagram take a string?


#1

Hello, i’m transitioning from Python 2 to Python 3 with the latest development build and was wondering if there was a way for PyDatagram to take a string or to convert the string into a readable format for PyDatagram. If this is in the wrong place sorry, haven’t actually used this site in a bit lol


#2

It’s not clear what you are trying to do. Yes, you can add a string to a datagram in various ways, as described in the documentation:
https://www.panda3d.org/reference/python/panda3d.core.Datagram

As you can see, there are various methods to add a string. To add raw bytes, you can also add a bytes object, either by directly appending it to the raw data or as a length-prefixed blob.

To give more specific guidance I’ll need to have more specific information on what you are trying to do.


#3

i’m upgrading a Toontown source, i found a workaround just a bit ago, but i’m sure its not the best…

        try:
            _string = bytes(string, 'utf-8')
            dg = PyDatagram(_string)
            dgi = PyDatagramIterator(dg)
        except:
            import codecs
            _string = bytes(codecs.decode(string, 'utf-8'), 'utf-8')
            dg = PyDatagram(_string)
            dgi = PyDatagramIterator(dg)

the weird thing is it works initially, then it stops when i hit the playground. probably something to do with Toontown more than Panda at this point tho since it obviously wasn’t meant to run on Python 3.x. Basically what happens in Python 3.x is the above will return

b’t&\x01\x01\x01\x0b\x06\x00\x06\x00\x0b\x04\x00\x04\x04’

wheras in Python 2.x it returns something like this
image


#4

Sorry, we can’t help you with that. If you have a question involving source code you own, feel free to open a new topic.


closed #5