• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Networking error is sending false data or not sending data

K

KetoGames

Guest
Hello all!

I have been working with GameMaker's networking systems, and have been running into an issue that's seemingly very hard to replicate. Basically, there are enemies that spawn around the edge of the screen. On the server side, they are created, and then replicated in the client side (where an x and y coordinate are sent). However, once in a while, there will be an enemy that will spawn on whichever screen is NOT currently being played on. I'm using TCP and hosting locally (with my internal IP address), using a resizable buffer, and working on a reliable connection. Here are some code snippets:

Server side, sending enemy data (triggered on an enemy spawn):

var xx = x;
var yy = y;

with objGov
{
buffer_seek(buff, buffer_seek_start, 0);

buffer_write(buff, buffer_u8, 4);

buffer_write(buff, buffer_u32, xx);
buffer_write(buff, buffer_u32, yy);

for (var i = 0; i < ds_list_size(socketlist); i++;)
{
network_send_packet(ds_list_find_value(socketlist, i), buff, buffer_tell(buff));
}
}

Client side, decoding incoming requests, triggered on a network event:

/// scrReceivePacket (buffer)

var buffer = argument[0]; // "1, x, y"
var message_id = buffer_read(buffer, buffer_u8); // "x, y"

switch(message_id)
{
case 1:
var px = buffer_read(buffer, buffer_u32);
var py = buffer_read(buffer, buffer_u32);
var mx = buffer_read(buffer, buffer_u32);
var my = buffer_read(buffer, buffer_u32);
objPlayerConnected.x = px;
objPlayerConnected.y = py;
objPlayerConnected.image_angle = point_direction(px, py, mx, my);
break;

case 2:
var ml = buffer_read(buffer, buffer_bool);
var mr = buffer_read(buffer, buffer_bool);
objPlayerConnected.mouseLeft = ml;
objPlayerConnected.mouseRight = mr;
break;

case 3:
room_goto(rmStandard);
break;

case 4:
var ex = buffer_read(buffer, buffer_u32);
var ey = buffer_read(buffer, buffer_u32);
instance_create(ex, ey, objEnemy);
break;

case 5:
var bx = buffer_read(buffer, buffer_u32);
var by = buffer_read(buffer, buffer_u32);
var bd = buffer_read(buffer, buffer_u32);
bul = instance_create(bx, by, objEnemyBullet);

with bul
{
direction = bd;
}

break;
}

The first three cases (player location, client mouse activity, and a game starting signal) are all working great.

Any help is much appreciated! If there's anything more that you need then I'd be glad to provide it. Thank you!
 
S

Sake_v2

Guest
As apparently I don't see any problem with your code, I would guess one of the 3 things are happening:

1) You're sending too many packets for a TCP connection? I doubt this is the case since its an enemy creation problem;
2) Somewhere else in your project you're affecting these objects x and y locations;
3) Use buffer_f32 for player/objects coordinates. Objects can be in coordinates like 0.5;3.5 (x;y) and the object can be getting stuck because the coordinate is not exact.
 
J

jaydee

Guest
I'm not sure if this is causing the error or not, but you're not handling this situation and it will become an issue down the line:

TCP is a stream based protocol, which ensures packets will arrive in order. This ultimately means that even if you send packets individually, they may arrive concated together. It is therefore necessary to check if multiple packets have arrived when reading the stream.

The usual and easy way to implement this is through a while loop:
Code:
while(bytes read < buffer size)
{
 // Read packet
}

I just read through your code a bit more in depth, and I'd also encourage you not to use the unsigned integer format for positional coordinates:
Code:
var px = buffer_read(buffer, buffer_u32);
var py = buffer_read(buffer, buffer_u32);
var mx = buffer_read(buffer, buffer_u32);
var my = buffer_read(buffer, buffer_u32);
objPlayerConnected.x = px;
objPlayerConnected.y = py;
objPlayerConnected.image_angle = point_direction(px, py, mx, my);
If an object spawned at say (-10, -4), this would cause unexpected behaviour, as the unsigned integer type cannot handle negatives. I'd suggest (as above) you use floating point numbers:

var py = buffer_read(buffer, buffer_f32);

*Note - don't forget to change the server code encoding the buffers if you make this change as well!
 

The-any-Key

Member
So you mean the client may spawn enemies the server don't have? If so it sounds like your client spawn local enemies. Make sure all server code only run on the server and not on clients too.
 
K

KetoGames

Guest
I'm not sure if this is causing the error or not, but you're not handling this situation and it will become an issue down the line:

TCP is a stream based protocol, which ensures packets will arrive in order. This ultimately means that even if you send packets individually, they may arrive concated together. It is therefore necessary to check if multiple packets have arrived when reading the stream.

The usual and easy way to implement this is through a while loop:
Code:
while(bytes read < buffer size)
{
 // Read packet
}

I just read through your code a bit more in depth, and I'd also encourage you not to use the unsigned integer format for positional coordinates:


If an object spawned at say (-10, -4), this would cause unexpected behaviour, as the unsigned integer type cannot handle negatives. I'd suggest (as above) you use floating point numbers:

var py = buffer_read(buffer, buffer_f32);

*Note - don't forget to change the server code encoding the buffers if you make this change as well!

This makes much more sense; if another object is sent (ex. player position) on the same frame as the enemy spawn, there's likely an issue there. Also, the enemies are spawned on the very edge of the play field (with coordinates of 0 or room_height/room_width), so that could also be my issue. I'll test this when I get home, thank you a ton!
 
Top