1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GML Can you store a 4 byte integer in a buffer aligned to 1 byte

Discussion in 'Programming' started by emicarra, Jan 11, 2020.

  1. emicarra

    emicarra Member

    Joined:
    Jul 2, 2017
    Posts:
    60
    Hello, i have a little online chat and i'm creating a buffer to send a string (1 byte) and a colour value (4 bytes).

    Since i have a 4 bytes (buffer_u32) colour value, do i have to use alignment 4 in my buffer? Or can i set it to 1? Because im sending a string in the same buffer, and i think if i send a 1 byte string in a buffer with alignment 4 i'm just wasting space and making it heavier.

    I'm confused because in the game maker manual, it says buffer_u32 (4 bytes) should be aligned to 4 bytes, but in an image that appears more at the bottom, it shows 2 byte data aliged to 1 byte.
    Maybe i'm confused and that's not how it works
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,878
    You should set the alignment to 1. All buffers designed to be sent over the network should be 1-aligned, as that will be how it's received on the other end.

    The instruction in the Manual about aligning u32 to 4 bytes is only for when data always go in 4 bytes at a time, or when that alignment is mandatory for formatting reasons (e.g. writing to a binary format where that is warranted). In a mixed scenario (very common for online games), any alignment other than 1 is usually counterproductive.
     
    emicarra likes this.
  3. emicarra

    emicarra Member

    Joined:
    Jul 2, 2017
    Posts:
    60
    Thanks for the clarification!
     
  4. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    1,106
    If the alignment is higher than 1, it will make empty 0 bytes in between for every buffer_write call you do that isn't the alignment length, so if you had an alignment of 4, each 1 byte write would put 3 0's after it. It doesn't apply if you write more bytes than the alignment. But it's best to just avoid using the alignment ei setting it to one
     

Share This Page