buffer_read

This function reads a piece of data of the given type from the given buffer at the buffer's current seek position.

After the function has executed the seek position is advanced by the number of bytes read. The next buffer_read will be done at this new position and will read the next byte(s) of data.

Since the function only reads the contents starting from the buffer's current seek position, you must ensure this is set correctly before calling the function - otherwise, you will get either incorrect results or nothing at all being returned.

NOTE You can use buffer_peek to get a value anywhere in the buffer without changing the seek position.

The return value depends on the type of data that you are reading, which can be one of the following constants:

Buffer Data Type Constant
ConstantDescriptionDatatype Returned
buffer_u8An unsigned, 8bit integer. This is a positive value from 0 to 255.int32
buffer_s8A signed, 8bit integer. This can be a positive or negative value from -128 to 127 (0 is classed as positive).int32
buffer_u16An unsigned, 16bit integer. This is a positive value from 0 - 65,535.int32
buffer_s16A signed, 16bit integer. This can be a positive or negative value from -32,768 to 32,767 (0 is classed as positive).int32
buffer_u32An unsigned, 32bit integer. This is a positive value from 0 to 4,294,967,295.int64
buffer_s32A signed, 32bit integer. This can be a positive or negative value from -2,147,483,648 to 2,147,483,647 (0 is classed as positive).int32
buffer_u64An unsigned 64bit integer. This is a positive value from 0 to 18,446,744,073,709,551,615.int64
buffer_f16A 16bit float. This can be a positive or negative value within the range of +/- 65504.number (real)
buffer_f32A 32bit float. This can be a positive or negative value within the range of +/-16777216.number (real)
buffer_f64A 64bit float.number (real)
buffer_boolA boolean value, can only be either 1 or 0 (true or false). It is stored in a single byte (8bit)int32
buffer_stringA string of any size, including a final null terminating characterstring
buffer_textA string of any size, without the final null terminating characterstring

If the function succeeds, it will return a value of the given type, however if it fails then it will cause a runner error.

NOTE Using the incorrect data type for the data being read will result in erroneous values being returned.

NOTE Reading a buffer_s32 or buffer_u32 on HTML5 returns the value as a Real, which is a 64-bit double, as int32 is not supported on that platform.

 

Syntax:

buffer_read(buffer, type)

ArgumentTypeDescription
bufferBufferThe buffer to read from.
typeBuffer Data Type ConstantThe type of data to be read from the buffer (see the list of constants above).

 

Returns:

Real, Boolean or String

 

Example:

buffer = buffer_create(10240, buffer_grow, 1);

// buffer_seek(buffer, buffer_seek_start, 0);
buffer_write(buffer, buffer_string, "Hello World");

buffer_seek(buffer, buffer_seek_start, 0);
result = buffer_read(buffer, buffer_string);

show_debug_message("Result = " + result);

The above code creates a buffer, writes a string to it and reads it back.

First a new grow buffer with an initial size of 10240 bytes is created using buffer_create. At this point you can explicitly call buffer_seek to set the seek position to 0, but this isn't necessary since a newly created buffer's seek position is 0. Next the string "Hello World" is written to the buffer with a call to buffer_write. This advances the seek position by 12 bytes: 11 bytes for the characters of the string followed by a final null byte. After this, the string is read back from the buffer. To read the correct data, the seek position is first set back to 0 with a call to buffer_seek. The data is then read into a variable result using buffer_read, which is shown in a debug message.