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 Trouble with buffers

Discussion in 'Programming' started by Rackover, Dec 19, 2017.

Tags:
  1. Rackover

    Rackover Member

    Joined:
    Sep 3, 2016
    Posts:
    71
    Hi, i'm getting trouble with the buffers. I've read the documentation but to no avail.
    I only made a little test, to experiment, but it doesn't even work and I do not understand why.

    Here is the code :

    Code:
    msgBuffer = buffer_create(1024, buffer_fixed, 1);
    
    buffer_poke(msgBuffer, 0, buffer_string, "A");
    buffer_poke(msgBuffer, 1, buffer_string, "B");
    buffer_poke(msgBuffer, 2, buffer_string, "C");
    buffer_save(msgBuffer, "savedBuffer.bin");
    
    show_debug_message(buffer_peek(msgBuffer, 0, buffer_string));
    show_debug_message(buffer_peek(msgBuffer, 1, buffer_string));
    show_debug_message(buffer_peek(msgBuffer, 2, buffer_string));
    
    show_debug_message(buffer_peek(msgBuffer, 0, buffer_u8));
    show_debug_message(buffer_peek(msgBuffer, 1, buffer_u8));
    show_debug_message(buffer_peek(msgBuffer, 2, buffer_u8));

    I expect it to debug something like :

    But the output is :

    Why can't it read back the strings ?
     
  2. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,250
    I had originally wrote here something about strings being more than 1 byte in size, implying that you are overwriting parts of your strings.

    However, doing this simple test seems to reveal that buffer_peek does not work correctly with string types. The first show_message will show "0", the second one "A".

    Tested with GMS1.4.1772 on windows platform.

    b = buffer_create(1024,buffer_fixed,1);
    buffer_poke(b,0,buffer_string,"A");
    var _str = buffer_peek(b,0,buffer_string);
    show_message(_str);
    buffer_seek(b,buffer_seek_start,0);
    var _str = buffer_read(b,buffer_string);
    show_message(_str);
     
    Last edited: Dec 19, 2017
  3. Rackover

    Rackover Member

    Joined:
    Sep 3, 2016
    Posts:
    71
    These strings are too short to exceed a byte, and also I checked with HxD and the .bin is correctly written with the corresponding characters at the right place. Are you sure buffer_peek is incapable of reading strings ? The doc says otherwise.
     
  4. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,250
    I don't see how that test could be made any more conclusive.

    Starting at position zero, buffer_read gets correct result, but peeking at position zero does not.

    I would say, you have discovered a bug.
     
  5. Rackover

    Rackover Member

    Joined:
    Sep 3, 2016
    Posts:
    71
  6. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,250
    I read the manual entry for buffer_peek as well. But I don't see what we could possibly be doing wrong at this point.

    I can imagine using buffer_peek to read strings is an uncommon thing to do anyway, since strings are usually random length. So maybe it wasn't even tested.

    edit: and while I've been working on this, I discovered something else. While you can use buffer_peek to read out bits of strings as buffer_u8, you can't do the same thing with buffer_read. Weird.
     
    Last edited: Dec 19, 2017
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,449
    The manual isn't perfect. It was written by Nocturne, or someone else by now - who knows. And a lot of the manual uses stuff from previous entries just copy and pasted. Just because the manual says something should work doesn't mean it necessarily does work. There are lots of entries in the manual that need to be changed and that simply haven't because they don't feel it's worth their time.

    @flyingsaucerinvasion 's code shows that peeking is indeed bugged. Plain and simple.
     
    Wraithious likes this.
  8. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,250
    if you haven't done so already, you should file a bug report.
     
  9. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,524
    1 byte for the char and one for the null point termination.
    So you need to poke:
    1
    3
    5
    Not
    1
    2
    3
     
  10. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,250
    The thing is, we couldn't get peek to work with strings no matter where we peeked, nor where or how the string was written.
     
  11. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,524
    Seems buffer_string don't work with peek so that is a bug. But it write the correct data to the buffer:

    Code:
    msgBuffer = buffer_create(10, buffer_grow, 1);
    for (var i=0; i<buffer_get_size(msgBuffer); i+=1)
    {
        buffer_poke(msgBuffer, i, buffer_u8, 1);
    }
    buffer_poke(msgBuffer, 0, buffer_string, "A");
    buffer_poke(msgBuffer, 2, buffer_string, "B");
    buffer_poke(msgBuffer, 4, buffer_string, "C");
    for (var i=0; i<buffer_get_size(msgBuffer); i+=1)
    {
        show_debug_message(buffer_peek(msgBuffer, i, buffer_u8));
    }
    
    Code:
    65
    0
    66
    0
    67
    0
    1
    1
    1
    1
    
     
  12. Rackover

    Rackover Member

    Joined:
    Sep 3, 2016
    Posts:
    71
    Hi,
    No, I didn't fill a bug report, sorry. Can someone here do it ? I do not know how to do it and the only moment I come to this forum is when I need help (and here I have returned, because I need help for something else). Or maybe can you tell me very quick how to do it ?
    Thank you !
     
  13. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,524

Share This Page