BMatjasic
Member
Hello everyone! I am currently working on a networking engine, which I've been pretty successful, until the recent update I've made.
I am trying to store a map inside global.sessionList, which is stored and later found by any of the search scripts I've made. But when I've created another list and add another map in it, it just can't be found and I get an error that this data structure does not exist.
How do I do it:
How I create the list inside the map:
NOTE: global.sessionList is the pointer to the main list which is created at create event.
How I add a map to that created list:
And the script that gives me an error:
Thanks in advance, any help would be appreciated![/PHP]
I am trying to store a map inside global.sessionList, which is stored and later found by any of the search scripts I've made. But when I've created another list and add another map in it, it just can't be found and I get an error that this data structure does not exist.
How do I do it:
How I create the list inside the map:
NOTE: global.sessionList is the pointer to the main list which is created at create event.
Code:
var playerList = ds_list_create();//Here we will create a list which will hold information about players INSIDE the session
var Session = ds_map_create(); //Let's create a session map and pass all the information we need to it
//Adding needed information to the session
ds_map_add(Session, "socket", Socket); //add socket to session database NOTE: If you don't want to set master socket, just leave socket argument -1
ds_map_add(Session, "name", client_find_info(Socket, "name")); //add name of the room owner
ds_map_add(Session, "max_players", MaxPlayers); //add ammount of max players
ds_map_add_list(Session, "playerList", playerList); //Create list of players
ds_map_add(Session, "id", ds_list_size(global.sessionList)); //Add the id of this session
ds_list_add(global.sessionList, Session); //adding session to the current session list
How I add a map to that created list:
Code:
var Socket = argument0; //Pass socket argument
var SessionJoin = argument1; //Pass session argument
if(session_exists(SessionJoin) == true) {
for(var i = 0; i < ds_list_size(global.sessionList); i ++) { //query for session data
var sessionMap = ds_list_find_value(global.sessionList, i);
var sessionId = ds_map_find_value(sessionMap, "id");
if(sessionId == SessionJoin) { //Checks whether we found the correct session
var playerList = ds_map_find_value(sessionMap, "playerList");
var maxPlayers = ds_map_find_value(sessionMap, "max_players");
//Now we need to encode a map with players who are in this session and send those info over
if(ds_list_size(playerList) < maxPlayers) { //whether there is a free slot
var playerName = client_find_info(Socket, "name");
//Add values to a map
var pMap = ds_map_create(); //Creates a dictionary for a player
ds_map_add(pMap, "name", playerName);
ds_map_add(pMap, "socket", Socket);
ds_map_add(pMap, "x", -1);
ds_map_add(pMap, "y", -1);
ds_list_add(playerList, pMap);
//ds_list_mark_as_map(playerList, ds_list_size(playerList) - 1);
show_debug_message("List: "+string(ds_map_find_value(sessionMap, "playerList")));
var def = ds_map_create();
ds_map_add_list(def, "default", playerList);
var json_playerList = json_encode(def);
ds_map_destroy(def);
And the script that gives me an error:
Code:
var SessionId = argument0; // Pass the socket argument
for(var i = 0; i < ds_list_size(global.sessionList); i ++) {
var sessionMap = ds_list_find_value(global.sessionList, i);
if(SessionId == ds_map_find_value(sessionMap, "id")) {
return sessionMap;
}
}
Last edited: