Character limit for 255 bytes

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Character limit for 255 bytes

Postby decompile » Wed Oct 19, 2016 12:23 pm

Hey,

I have a short question regarding how much characters 255 bytes are.

I googled it, and they said 1 character is 1 byte, so I added a security check for my message function:

Syntax: Select all

message = message[:255]


I tried to send a message with 252 characters, but ingame im receiving:

Code: Select all

DLL_MessageEnd:  Refusing to send user message SayText2 of 256 bytes to client, user message size limit is 255 bytes
User avatar
satoon101
Project Leader
Posts: 2698
Joined: Sat Jul 07, 2012 1:59 am

Re: Character limit for 255 bytes

Postby satoon101 » Wed Oct 19, 2016 1:26 pm

Are there any non-ascii characters in the string? Each of those would take up more than 1 byte. As an example:

Syntax: Select all

>>> x = chr(258)
>>> x
'Ă'
>>> x.encode('utf-8')
b'\xc4\x82'
>>> len(x.encode('utf-8'))
2
>>> 's'.encode('utf-8')
b's'
>>> len('s'.encode('utf-8'))
1
Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Character limit for 255 bytes

Postby decompile » Wed Oct 19, 2016 1:53 pm

Oh ye, could be possible due to color codes. Is there any other way to limit the message to 255 bytes?
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Character limit for 255 bytes

Postby Ayuto » Wed Oct 19, 2016 9:21 pm

You could use something like this:

Syntax: Select all

def limit_msg(msg, size):
# Use 'ignore' for the same reason like we used it here:
# https://github.com/Source-Python-Dev-Te ... /issues/27
return msg.encode('utf-8')[:size].decode('utf-8', 'ignore')

# Should print ab
print(limit_msg('abc', 2))

# Should print a
print(limit_msg('aä', 2))
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Character limit for 255 bytes

Postby decompile » Wed Mar 03, 2021 6:27 pm

Sorry to bump an older thread of mine.

But how can I apply these to a TranslationStrings instance?

It throws me an:

Code: Select all

AttributeError: 'TranslationStrings' object has no attribute 'encode'


I'm using TranslationStrings to tokenize player names and other stuff, so its very dynamic in length.

The best would be if it could split the message in 2 parts if its too long, without cutting through a word. Like: "This is too lo" "ng" to "This is too " "long"
User avatar
L'In20Cible
Project Leader
Posts: 1534
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Character limit for 255 bytes

Postby L'In20Cible » Fri Mar 05, 2021 11:10 am

decompile wrote:Sorry to bump an older thread of mine.

But how can I apply these to a TranslationStrings instance?

It throws me an:

Code: Select all

AttributeError: 'TranslationStrings' object has no attribute 'encode'


I'm using TranslationStrings to tokenize player names and other stuff, so its very dynamic in length.

The best would be if it could split the message in 2 parts if its too long, without cutting through a word. Like: "This is too lo" "ng" to "This is too " "long"

The easiest way would probably be to subclass SayText2 and truncate the localized string in a _send override. Something like that:

Syntax: Select all

class MySayText2(SayText2):
def _send(self, player_indexes, translated_kwargs):
translated_kwargs.message = truncate_or_whatever(translated_kwargs.message)
super()._send(player_indexes, translated_kwargs)


And then use that class to send your messages as normal. Another way without subclassing would be to hook that message and truncate the string in there before sending it.
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Character limit for 255 bytes

Postby decompile » Fri Mar 05, 2021 3:04 pm

Thank you.

I will try to play around with it. I was trying to find how SayText2 gets the string out of a TranslationStrings class instance but didn't had a success, but looks like its just TranslationStrings.message to return the string.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 17 guests