i need help with buffers

shui.bdr

Member
so i'm just learning how networking works, and by using these videos
i'm trying to create a very simple game like the guy in the video does, but i keep having the error "Attempting to read from outside the buffer, returning 0" and i just can't understand why.
i've made two different executables, one is the server and the other is the client.
so i wrote this in the async - networking event of the server
GML:
type_event = ds_map_find_value(async_load, "type")
socket = ds_map_find_value(async_load, "socket")
buffer = ds_map_find_value(async_load, "buffer")
socket_id = ds_map_find_value(async_load, "id")

switch(type_event)
{
    case network_type_connect:
        ds_list_add(socket_list,  socket)
        
        var player_ = instance_create_layer(player_spawnx, player_spawny, layer, o_player);
        ds_map_add(socket_to_distanceid, socket, player_)
        
        buffer_seek(server_buffer, buffer_seek_start, 0)
        buffer_write(server_buffer, buffer_u8, network.player_connect);
        buffer_write(server_buffer, buffer_u8, socket)
        buffer_write(server_buffer, buffer_u16, player_.x)
        buffer_write(server_buffer, buffer_u16, player_.y)
        network_send_packet(socket, server_buffer, buffer_tell(server_buffer))
        
        break;
        
    case network_type_disconnect:
        ds_list_delete(socket_list, ds_list_find_index(socket_list, socket))
        
        with(ds_map_find_value(socket_to_distanceid, socket))
        {
            instance_destroy();
        }
        
        break;
    case network_type_data:
        
        buffer_seek(buffer, buffer_seek_start, 0);
        recieved_packet(buffer, socket_id)
        break;
}
this code just send a buffer to the client, giving it the coordinates he has to spawn the player and the socket.

then, the client recieves that packet

GML:
type_event = ds_map_find_value(async_load, "type")

switch(type_event)
{
    case network_type_data:
        buffer = ds_map_find_value(async_load, "buffer")
        buffer_seek(buffer, buffer_seek_start, 0)
        recieved_packet(buffer);
    
    
}
and runs the recieved_packet script:

GML:
buffer_seek(buffer, buffer_seek_start, 0);
msgid = buffer_read(buffer, buffer_u8);


switch(msgid)
{
    case network.player_connect:
        var socket_ = buffer_read(buffer, buffer_u8)
        var x_ = buffer_read(buffer, buffer_u16)
        var y_ = buffer_read(buffer, buffer_u16)
        
        var player_ = instance_create_layer(x_, y_, layer, o_player)
        
    case network.move:
        buffer_seek(buffer, buffer_seek_end, 0);
        o_player.x = buffer_read(buffer, buffer_u16);
        o_player.y = buffer_read(buffer, buffer_u16);
        break;
    
    
    
    
    
    
}
aaand the it gives me the error:

############################################################################################
ERROR in
action number 1
of Async Event: Networking
for object o_client:

Attempting to read from outside the buffer, returning 0
at gml_Script_recieved_packet (line 20) - o_player.x = buffer_read(buffer, buffer_u16);
############################################################################################
gml_Script_recieved_packet (line 20)
gml_Object_o_client_Other_68 (line 8) - recieved_packet(buffer);
 

Nidoking

Member
You're missing the break for the first case in that script.

But then, do you really intend to seek to the end of the buffer, then read beyond that? Is that actually what you meant to do?
 

shui.bdr

Member
You're missing the break for the first case in that script.

But then, do you really intend to seek to the end of the buffer, then read beyond that? Is that actually what you meant to do?
i just got crazy for 3 days trying understanding wich errors i've committed and it turns out that i'm just very dumb, thanks i can't believe i'm still doing this errors after 2 years of coding
 
Top