diff --git a/Manual/contents/GameMaker_Language/GML_Reference/Buffers/buffer_read.htm b/Manual/contents/GameMaker_Language/GML_Reference/Buffers/buffer_read.htm index 4674f7dc2..8b880977b 100644 --- a/Manual/contents/GameMaker_Language/GML_Reference/Buffers/buffer_read.htm +++ b/Manual/contents/GameMaker_Language/GML_Reference/Buffers/buffer_read.htm @@ -15,7 +15,10 @@
This function can be used to read data from a previously created buffer. It will read the value at the buffer's current seek position.
+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.
+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:
If the function succeeds, it will return a value of the given type, however if it fails then it will cause a runner error.
@@ -48,8 +51,18 @@
var _cmd = buffer_read(buff, buffer_s16);
-The above code reads from the buffer stored in the variable buff a signed 16bit value into the local variable _cmd.
+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.