• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Buffer Alignment Explanation?

TheouAegis

Member
Consider a buffer 10 bytes long

0 1 2 3 4 5 6 7 8 9

If you write a value of $FF using 2 byte alignment, the buffer would be

$FF $00 2 3 4 5 6 7 8 9

If you then wrote a value of $80, it would be

$FF $00 $80 $00 4 5 6 7 8 9

So after two writes the buffer would be at byte 4, not byte 2.
 
D

DankMemes

Guest
Didn’t really get that
Where did 1 go?
Why us there an extra 0?
Why is there an extra $

Is the 00 the two bite alignment?
Maybe im not getting what alignment means?
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Essentially, positions of values in a buffer are multipliers of alignment.
So, if alignment is 2 but you wrote a 1-byte value (like buffer_u8 or buffer_bool), GM will add another empty byte so that the next one would be aligned accordingly;
Similarly, if alignment is 4 and you wrote a 1-byte value, it'd have to add 3 more bytes to align the next one.
As you might imagine, this is a fairly exotic thing to desire and is only used if you are interfacing with something that uses alignment (e.g. executable file formats commonly have file sections aligned to multipliers of 4).
 

NightFrost

Member
Alignment is important because buffers have an internal pointer. That is, a buffer remembers where your position is, and every read and write advances that pointer by the set alignment. If you've for example used PHP its arrays also feature an internal pointer that advances on reads and writes. The behavior is advantageous because it lets you iterate across the data structure without having to loop with a counter. Consider PHP's foreach:
Code:
// PHP code
foreach($my_array as $value){
    echo $value;
}
This will go over every position of array my_array , get the value into value variable and display it. It works thanks to internal pointer advancing on every read the looping command performs. Sadly, in GML no other data structure features pointers besides buffers.
 
K

Kasra_n

Guest
Essentially, positions of values in a buffer are multipliers of alignment.
So, if alignment is 2 but you wrote a 1-byte value (like buffer_u8 or buffer_bool), GM will add another empty byte so that the next one would be aligned accordingly;
Similarly, if alignment is 4 and you wrote a 1-byte value, it'd have to add 3 more bytes to align the next one.
As you might imagine, this is a fairly exotic thing to desire and is only used if you are interfacing with something that uses alignment (e.g. executable file formats commonly have file sections aligned to multipliers of 4).
yello i like the way you describing, your posts are allways usefull
 
Top