• 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 Socket ID not being stored correctly...

N

ntp488

Guest
I am working on a project which uses a client-server relationship to transmit data back and forth to multiple clients. The server is supposed to create a ds_grid and store all of the socket ids for newly connected clients in the first row - row 0 - here (ignore the list):
Code:
switch type_event {
    case network_type_connect:
        var socketid = async_load[? "socket"]
        ds_list_add(self.socket_list, socketid)
        //check if client data array needs to add room for new client
        var gridWidth = ds_grid_width(self.clientDataArray)
        var gridHeight = ds_grid_height(self.clientDataArray)
        if (ds_list_size(self.socket_list) > gridWidth) {
            ds_grid_resize(self.clientDataArray, gridWidth + 1, gridHeight)
        }
        //add new client socketid to end of data array in first y position
        ds_grid_add(self.clientDataArray, ds_grid_width(self.clientDataArray) - 1, 0, socketid)
        break
When a client sends data, this is how I have chosen to handle it:
Code:
case network_type_data:
        //handle the data
        var buff = async_load[? "buffer"]
        var socketid = async_load[? "socket"]
        buffer_seek(buff, buffer_seek_start, 0)
        netScript_ReceivedPacket(buff, socketid)
        break
The script runs and checks the message id, before it gets to here:
Code:
case 2:
        //read in player position and animation - store in ds_grid
        var xpos = buffer_read(buffer, buffer_u32)
        var ypos = buffer_read(buffer, buffer_u32)

        var currentAnimation = buffer_read(buffer, buffer_string)
       
        //find location of client in grid - update
        var gridX = ds_grid_value_x(serverObj.clientDataArray, 0, 0, ds_grid_width(serverObj.clientDataArray), 1, argument[1])

        show_debug_message(string(gridX))
        ds_grid_set(serverObj.clientDataArray, gridX, 1, xpos)
        ds_grid_set(serverObj.clientDataArray, gridX, 2, ypos)
        ds_grid_set(serverObj.clientDataArray, gridX, 3, currentAnimation)

        break
However, at this point the console warns me that I am attempting to access elements [-1, 1], [-1, 2], and [-1, 3] which are all obviously out of bounds. The debug message shows "undefined". Am I simply misunderstanding how I can use the socket ID?
Download Broken
*note: problem occurs in server project, not the game project
 

FrostyCat

Redemption Seeker
No, you just aren't inserting them properly.

First, you started the grid with a height of 3, yet you are trying to set index 3 on the second dimension. This is clearly an out-of-bounds condition.

Second, you didn't account for disconnections in a sufficient manner. You just removed them from the list without making any adjustments to the grid. You will overwrite the last column over and over again until at least that many clients reconnect, making it possible to not find existing clients.

Quite frankly I think you should just scrap the grid system, because the grid won't shrink back when clients disconnect. What you should do is a nested map structure like this:
Code:
{
  "0": {
    "x": 576,
    "y": 293,
    "currentAnimation": "abc"
  },
  "2": {
    "x": 555,
    "y": 133,
    "currentAnimation": "def"
  }
}
The first key is the socket number in string form, and inside each key is another map containing properties. You can then freely add or remove clients as you please, as long as you destroy the inner map upon removal. No counter-adjustments or manual resizing required.
 
N

ntp488

Guest
I changed the dimensions to [1, 4] and the problem still occurs, providing me with -1 in the x position and saying that the index is out of bounds.

Concerning switching to the ds_map - I may try that while I look for a solution to this problem. However, for now this is driving me up a wall. I don't understand why I keep getting -1 for the x.
 

FrostyCat

Redemption Seeker
Here's a tracethrough that should make the problem quite obvious:
  • Start your server from scratch.
  • Connect 2 clients, A and B. Your list expands to size 2 and the grid expands to width 2, according to the connect-type Networking event code.
  • Disconnect client A. Your list is now at size 1 and grid at width 2, according to the disconnect-type Networking event code.
  • Another client, C, arrives. Since your list is size 1 < grid's width 2, you didn't expand the grid. You then write over B's column with C.
  • Now try looking for B. You overwrote it the step before, only A and C are in your grid now.
The only way to fix this under the grid system is to fix the disconnect-type Networking event code. This means finding the disconnecting client's column and moving over other columns to its right back by one slot, then downsizing your grid by 1.

Sounds messy? It sure is to me. That's why I recommended the nested map method: you don't need to handle any of this resizing nonsense with that.

Edit: The second step has to disconnect A, not B.
 
Last edited:

Roa

Member
I don't understand the point of this entirely. The only 2 things you need for networking are a map and list for the server, and a map for clients. I wouldn't try anything else. Sorting data into other data_structures should come AFTER you already have established common methods to backup what you plan to do.
 

FrostyCat

Redemption Seeker
I don't understand the point of this entirely. The only 2 things you need for networking are a map and list for the server, and a map for clients. I wouldn't try anything else. Sorting data into other data_structures should come AFTER you already have established common methods to backup what you plan to do.
My server-side map is the equivalent of your server-side list, but with sublinear single-entry lookup time because it's a map.
 
N

ntp488

Guest
Here's a tracethrough that should make the problem quite obvious:
  • Start your server from scratch.
  • Connect 2 clients, A and B. Your list expands to size 2 and the grid expands to width 2, according to the connect-type Networking event code.
  • Disconnect client A. Your list is now at size 1 and grid at width 2, according to the disconnect-type Networking event code.
  • Another client, C, arrives. Since your list is size 1 < grid's width 2, you didn't expand the grid. You then write over B's column with C.
  • Now try looking for B. You overwrote it the step before, only A and C are in your grid now.
The only way to fix this under the grid system is to fix the disconnect-type Networking event code. This means finding the disconnecting client's column and moving over other columns to its right back by one slot, then downsizing your grid by 1.

Sounds messy? It sure is to me. That's why I recommended the nested map method: you don't need to handle any of this resizing nonsense with that.

Edit: The second step has to disconnect A, not B.
Right, I understand that is a problem. I get this error before anyone disconnects though. One client connects, index goes out of bounds - no disconnections.
*edit: I will fix the disconnection issue(I noticed the error before I got that far), but that's most definitely not what is causing the problem that I am speaking about.
 
Top