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

emicarra

Member
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
 

FrostyCat

Redemption Seeker
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
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

Member
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.
Thanks for the clarification!
 

Joe Ellis

Member
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
 
Top