Kehom / GodotAddonPack

A collection of pure GDScript addons for Godot
http://kehomsforge.com
MIT License
189 stars 15 forks source link

[EncDecBuffer] Support for string #21

Closed yuraj11 closed 4 years ago

yuraj11 commented 4 years ago

Is there any reason why EncDecBuffer does not support strings by default? I have come up with this:

static func write_string_utf8(encdecbuffer: EncDecBuffer, string: String) -> void:
    var bytes := string.to_utf8()
    encdecbuffer.write_uint(bytes.size())
    for i in bytes.size():
        encdecbuffer.write_byte(bytes[i])

static func read_string_utf8(encdecbuffer: EncDecBuffer) -> String:
    var bytes := PoolByteArray()
    var size := encdecbuffer.read_uint()
    for i in size:
        bytes.append(encdecbuffer.read_byte())
    return bytes.get_string_from_utf8()

(I am not using strings in snapshots but in custom player_data properties to optimize and send things like player name, skin etc. to be send in one packet)

Kehom commented 4 years ago

The EncDecBuffer was meant primarily to strip out extra data attached to variables, which is part of the Variant. This extra data basically specifies which type is stored within the variant, and uses 4 bytes. I honestly didn't see any reason to encode/decode Strings in this system. The thing is, sending the full variant data occasionally is not a problem. Sending this data at every single frame can be a problem as it will quickly escalate to very big numbers. The custom player data is sent only during initial synchronization then only when changed. So, as long as changes are not too common (how often depends very much on the project and the desired "budget"), it should not be a problem.

Now you actually touched one point that I do intend to work at a later moment. Currently each custom property will be sent in a different packet and this is something I do want to fix.

In any case, this can indeed be added into the feature set of the addon. If you want (and feel comfortable with the idea), feel free to open a pull request.

yuraj11 commented 4 years ago

Thanks for the info. That was the limitation for me because I wanted to send multiple configs at once when player connects.