PDA

View Full Version : Associative array?


JkWhoSaysNi
08-26-2006, 03:42 AM
Hi. Are associative arrays possible in GS2?

What I mean arrays where the index is a string rather than an integer. E.g. myArray["index"]?

I tried it and it appeared not to work (Though diddnt give me an error in RC.) So are they possible? Thanks.

Admins
08-26-2006, 01:40 PM
You can just use subvariables, which is quite the same. Internally they are organized like an associative array, although you cannot access them as indexed array (use obj.getvarnames() to find out what variables are there, obj.savevarstoarray(sort) to transform it into an indexed array):

myarray.index or myarray.("index")

JkWhoSaysNi
08-26-2006, 04:13 PM
Thanks :)

JkWhoSaysNi
08-27-2006, 03:57 PM
ok, i have a few problems.

Heres what i'm trying to do:

for (pl: players) {
olplayers.players.(pl.account) = {pl.level,pl.x,pl.y};
}
player.chat = olplayers.getvarnames();


I'm trying to create an index with the name of the players account. It's showing up as "account" in getvarnames. Where the index should be the name of the player. How can I make it understand that pl.account is a variable that needs to be interpreted?

Skyld
08-27-2006, 05:14 PM
Try olplayers.players.(@ pl.account).

Admins
08-27-2006, 06:02 PM
Yes you need to tell that it is a string, by doing "" @ pl.account or just @ pl.account

JkWhoSaysNi
08-27-2006, 06:08 PM
Thanks that works but I cant use it how I want to.

I'm trying to script a map.

Instead of having each client checking the locations of each other player while they're viewing the map I intend to have a database NPC store the locations and get them every 0.5 seconds if there is someone viewing the map. Then, each player can get the data from the database NPC rather than getting the information themselves.

However, I have a problem. I cant seem to pass associative arrays as params from one npc to another.

Heres what I have so far:

MapDB NPC
public function pollInfo() {
playernames.clear();
oldplayers.clear();
for (pl: players) {
olplayers.(@ pl.account) = {pl.level,pl.x,pl.y};
playernames.add(pl.account);
}
}

public function getPlayerNames() {
return playernames;
}

public function getLocations() {
return olplayers;
}


part of Map NPC

function onActionServerside() {
//retrieve data from the mapdb npc
mapdb = findnpc("MapDB");
mapdb.pollInfo();
playernames = mapdb.getPlayerNames();
locations = mapdb.getLocations();
//send data back to the client
player.chat = locations; //Does not contain any account names, but it should.
triggeraction(0,0,"clientside","Map",playernames,locations);

}
//#CLIENTSIDE




Playernames gets returned correctly but locations is not. It loses the subvariables that have been set.

xXziroXx
08-27-2006, 06:14 PM
try

player.chat = locations;

instead of

player.chat = locations;

?

JkWhoSaysNi
08-27-2006, 06:20 PM
actually it should have been
player.chat = locations.getvarnames();

I changed it to see if playernames would get passed correctly. Then changed it back and forgot to add .getvarnames. But still, it doesn't contain any account names.

xXziroXx
08-27-2006, 06:42 PM
I dont see any OBVIOUS faults in it, but then im not really looking too closely at it.

Perhaps olplayers.(@pl.account) instead of olplayers.(@ pl.account)? No idea..

Admins
08-27-2006, 11:44 PM
That's not the problem. When you want to pass variables to the clientside or display it as string then you need to convert it into a string or array first (which is then converted to a string automatically). Use oldplayers.savevarstoarray(false) for that, or eventually directly work with an array in this case, like olplayers.add({pl.account,pl.level,pl.x,pl.y});