Illegal Buffer Index error

R

Rivaldragon

Guest
I'm trying to get networking to work by following a tutorial online and have run in to a bit of trouble.

I'm trying to send the players movments to the server using this script called from the step event.

Code:
buffer_seek(buffer, buffer_seek_start, 0);

buffer_write(buffer, buffer_u8, KEY);
buffer_write(buffer, buffer_s16, argument0);
buffer_write(buffer, buffer_u8, argument1);

network_send_packet(socket,buffer,buffer_tell(buffer));
But I only get this error
ERROR in
action number 1
of Step Event0
for object obj_client:

Illegal Buffer Index
at gml_Script_scr_send_key (line 3) - buffer_seek(buffer, buffer_seek_start, 0);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_send_key (line 3)
called from - gml_Object_obj_client_StepNormalEvent_1 (line 11) - scr_send_key(_right, false);
If I call the script in the network event or create event it works without any problems, but in the tutorial he is using the step event so it must be something I have done wrong but I cant figure out what.

Create event
Code:
name = get_string("Name: ","");

socket = network_create_socket(network_socket_tcp);
buffer = buffer_create(16384, buffer_grow, 1);

global.socket = socket;

connect = network_connect(socket, global.ip, global.clientPort);

if (connect != 0)
{
    show_debug_message("Can not connect to server");
    buffer_delete(buffer);
    network_destroy(socket);
    network_destroy(connect);
    game_restart();
}

if(string_length(name) < 1)
{
    name = "Player";
}

myID = -1;
myTeamID = choose(RED_TEAM_ID,BLUE_TEAM_ID);

scr_get_server_type_request();
scr_spawn_player_request(myTeamID, name);

serverType = 0;

//Survive mode variables
waves = 0;
players = 0;

objectsToDraw = ds_list_create();

_right = 0;
_left = 1;
_up = 3;
_down = 4;
Step Event
Code:
if(keyboard_check(vk_right))
{
    scr_send_key(_right, true);
}
else
{
    scr_send_key(_right, false);
}
 

jo-thijs

Member
Hi and welcome to the GMC!

This here:
Code:
if (connect != 0)
should be:
Code:
if (connect >= 0)
This error causes your buffer to get deleted, which results in the error you get (I think).
 
R

Rivaldragon

Guest
Hi and welcome to the GMC!

This here:
Code:
if (connect != 0)
should be:
Code:
if (connect >= 0)
This error causes your buffer to get deleted, which results in the error you get (I think).
Tried to change it but same error, I only get the error after I press the right arrow and the script start, if I don't press anything everything works just fine.

Also thanks for the welcome :3
 
R

Rivaldragon

Guest
Oops, my bad.

I meant to say to change it to:
Code:
if (connect < 0)
Still get the same error.
I only get the error if I activate the script from the step event, can you maybe not send data from step event?
 
I really thought jo-thijs had found the cause of your problem.

Make sure the variable "buffer" isn't being overwritten anywhere.

For example make sure you're not overwriting "buffer" with the buffer in the async networking event.
 
R

Rivaldragon

Guest
Okay so this is really strange, if I create a new buffer first thing in the script then delete if after I have sent data using the regular buffer it works perfectly.

So if the code is like this it works
Code:
buffer2 = buffer_create(16384, buffer_grow, 1);

buffer_seek(buffer, buffer_seek_start, 0);

buffer_write(buffer, buffer_u8, KEY);
buffer_write(buffer, buffer_s16, argument0);
buffer_write(buffer, buffer_u8, argument1);

network_send_packet(socket,buffer,buffer_tell(buffer));
buffer_delete(buffer2);
But if it is like this it don't work

Code:
buffer_seek(buffer, buffer_seek_start, 0);

buffer_write(buffer, buffer_u8, KEY);
buffer_write(buffer, buffer_s16, argument0);
buffer_write(buffer, buffer_u8, argument1);

network_send_packet(socket,buffer,buffer_tell(buffer));
 
R

Rivaldragon

Guest
compare the value of buffer2 to buffer, if they both have the same value, then buffer is being deleted somewhere before that code can run.
You are completely right, found a place where I had removed the wrong buffer and that was causing the error.

Thank you so much everyone for your help, this would have driven me insane soone or later otherwise.
Is there some way I can mark this problem solved?
 
Top