There is a difference between a variable holding the value undefined and a variable not being initialized or declared. is_undefined only checks the first and requires the second. If you want the second use variable_instance_exists(instance_id, name); https://manual.yoyogames.com/GameMa...riable_Functions/variable_instance_exists.htm.I simply have this code:
GML:if is_undefined( database[i, oPlayers] ) { //stop }
and still the game crashes saying that is_undefined( database[i, oPlayers] ) is not set before reading it!
I acctually USE is_undefined to PREVENT this error! Why does it not work?
Thanks for the responses! Good to know this, gonna try it out right nowThere is a difference between a variable holding the value undefined and a variable not being initialized or declared. is_undefined only checks the first. If you want the second use variable_instance_exists(instance_id, name); https://manual.yoyogames.com/GameMa...riable_Functions/variable_instance_exists.htm.
Sadly also with if variable_instance_exists(instance_id, database[i, oPlayers]) { im getting the same error saying that the variable is not set before reading.variable_instance_exists(instance_id, name);
if (database[i, oPlayers] != -1)
. Otherwise, you could also write your own script that manually compares the value stored to all valid possibilities and returns true if there is a match and false if not.database[i, oPlayers]
instead of database[i, oPlayers.Name]
, etc.? oPlayers by itselfs means nothing much, it is just a namespace for the actual values you define in it.if (database[i, oPlayers] != -1)
So I also tried all of this... and even that errors saying database[i, oPlayers] is not set yet before reading.you could also write your own script that manually compares the value stored to all valid possibilities and returns true if there is a match and false if not.
That is true! I acctually only showed database[i, oPlayers] here on the forum because I did show only a small cutted part from the complete script.Why is the code writtendatabase[i, oPlayers]
instead ofdatabase[i, oPlayers.Name]
, etc.? oPlayers by itselfs means nothing much, it is just a namespace for the actual values you define in it.
// initialize enums for the rank and database array ( Name = Playername (a string), Value = last_online_time (an unixtime integer) )
enum oPlayers { Name = 0, Value = 1, Sorting = 2, Rank = 3, UpdatedAt = 4, UserProfileURL = 5, UserIconURL = 6, Key = 7};
database = 0; // a two dimensional array for the player database (name, last_online_time)
databaseSize = 0; // nr of players in the database array
online_players = 0;
alarm[0] = 1;
unix_timestamp(); //to get current_unix_time
//this loop go through all players of the database to count how many have shown activity in less than 100 seconds to set online_players
if databaseSize > 0{
online_players = 0;
elems = databaseSize;
for (i = 0; i < elems; i++) {
var name, last_online_time, last_activity;
name = database[i, oPlayers.Name];
last_online_time = database[i, oPlayers.Value];
last_activity = current_unix_time - last_online_time;
if last_activity < 100{
online_players +=1;
}
}
}
alarm[0]=120; //to keep online_players up to date
draw_text(509,5,string(online_players)+" players online in the game now");
//send to database
if (request == 1) {
if (getResultStatus()) {
// request successful.
// retrieve player database
valueslist = ds_map_find_value(ResponseMap, "data");
// this is the list with all players
// store it in the database array
database = 0;
entries = ds_list_size(valueslist);
databaseSize = entries;
for (i = 0; i < entries; i++) {
var map = ds_list_find_value(valueslist, i);
database[i, oPlayers.Name] = ds_map_find_value(map, "name");
database[i, oPlayers.Rank] = ds_map_find_value(map, "rank");
database[i, oPlayers.Value] = ds_map_find_value(map, "value");
database[i, oPlayers.UserProfileURL] = ds_map_find_value(map, "user_profile");
database[i, oPlayers.UserIconURL] = ds_map_find_value(map, "user_icon");
}
}
request = -1;
}
Ah I acctually had some typos while copy/pasting my code to the forum. It should be enum oPlayers and not enum RankResult.Did you actually meanRankResult.Name
andRankResult.Value
for these two lines?
GML:name = database[i, oPlayers.Name]; last_online_time = database[i, oPlayers.Value];
database[i, oPlayers.Name]
is accessible, it's actually a series of 4 checks:database
exists as an instance variable in the current instance (which is id
, NOT instance_id
)database
has the array typedatabase
has a height greater than i
database
has a width greater than oPlayers.Name
if (variable_instance_exists(id, "database") && is_array(database) && array_height_2d(database) > i && array_length_2d(database, i) > oPlayers.Name) {
/* database[i, oPlayers.Name] is accessible */
}
True. I just added that code in the HTTP event now.You didn't show the part where you actually set and populate the database array.
Maybe you could useTrue. I just added that code in the HTTP event now.
show_debug_message(json_encode(async_load))
or use breakpoints just before you pull something out of the data structure to verify whether the content you expect is actually present in the map, in the location where you attempt to extract it?correctGiven your signature, I'm assuming GMS 1.4 code here.
It... just... works! This code you provided with the 4 checks finaly removed any errors and the code can just keep running/updating online_players without any problems now!it's actually a series of 4 checks:
database
exists as an instance variable in the current instance (which isid
, NOTinstance_id
)database
has the array typedatabase
has a height greater thani
database
has a width greater thanoPlayers.Name
GML:if (variable_instance_exists(id, "database") && is_array(database) && array_height_2d(database) > i && array_length_2d(database, i) > oPlayers.Name) { /* database[i, oPlayers.Name] is accessible */ }
unix_timestamp(); //to get current_unix_time
//this loop go through all players of the database to count how many have shown activity in less than 100 seconds to set online_players
if databaseSize > 0{
online_players = 0;
elems = databaseSize;
for (i = 0; i < elems; i++) {
var name, last_online_time, last_activity;
stop=0;
if (variable_instance_exists(id, "database") && is_array(database) &&
array_height_2d(database) > i && array_length_2d(database, i) > oPlayers.Name) {
name = database[i, oPlayers.Name];
}else{ stop=1;}
if (variable_instance_exists(id, "database") && is_array(database) &&
array_height_2d(database) > i && array_length_2d(database, i) > oPlayers.Value) {
last_online_time = database[i, oPlayers.Value];
}else{ stop=1;}
if stop==0{
last_activity = current_unix_time - last_online_time;
if last_activity < 100{
online_players +=1;
}
}
}
}
alarm[0]=120; //to keep online_players up to date