After finding out that there was a 223 character limit on attributes, and a limited amount of them available for use, I decided to write my own system for handling cross-client communications in the same way that attributes work.
This basically lets you set variables like "player.test = 10; player.shareVar("test");" on the client, then on any OTHER client, use findPlayer or whatever other method to get that player's object, and get the value of the same variable! "player.chat = findPlayer("Hezzy002").test;" will print 10, just as it was set!
The variables are stored permanently in an SQL database as well, and are reproduced on login.
Installation:
1) Create a new SQL DB named crossplayer
2) Add the crap below to your server, make sure if you change the weapon or DBNPC names you change any references to that name.
DBNPC
Crossplayer-Control
PHP Code:
public function addSharedVariable(target, flagName, value) {
if (flagName in target.getStaticVarNames()) {
echo(target @ " tried to modify a protected variable (" @ flagName @ ")");
return;
}
target.(@flagName) = value;
reqsql2("crossplayer",
"INSERT OR REPLACE INTO '" @ target.account @ "' (name, value)
VALUES ('" @ flagName @ "', '" @ value @ "')", false);
temp.packet = null;
temp.packet.add(target.account);
temp.packet.add(flagName);
temp.packet.add(value);
for (pl : allplayers) {
if (pl == target) continue;
pl.triggerclient("gui", "-System/Client/Crossplayer", temp.packet);
}
}
public function shareVars(target) {
reqsql2("crossplayer",
"CREATE TABLE IF NOT EXISTS '" @ target.account @ "' (
name TEXT PRIMARY KEY,
value TEXT
)", false);
for (pl : allplayers) {
if (pl.levelname == null) continue;
temp.req = reqsql2("crossplayer", "SELECT * FROM '" @ pl.account @ "'", true);
temp.vars = temp.req.rows;
temp.packet = null;
temp.packet.add(pl.account);
for (temp.v : temp.vars) {
temp.toks = temp.v.tokenize();
temp.packet.add(temp.toks[0]);
temp.packet.add(temp.toks[1]);
}
target.triggerclient("gui", "-System/Client/Crossplayer", temp.packet);
}
temp.table = reqsql2("crossplayer", "SELECT name FROM sqlite_master WHERE type='table' AND name='" @ target.account @ "'", true);
if (temp.table != null) {
//Send my own to everyone else
temp.req = reqsql2("crossplayer", "SELECT * FROM '" @ target.account @ "'", true);
temp.vars = temp.req.rows;
temp.packet = null;
temp.packet.add(target.account);
for (temp.v : temp.vars) {
temp.toks = temp.v.tokenize();
target.(@temp.toks[0]) = temp.toks[1];
temp.packet.add(temp.toks[0]);
temp.packet.add(temp.toks[1]);
}
for (pl : allplayers) {
if (pl == target) continue;
pl.triggerclient("gui", "-System/Client/Crossplayer", temp.packet);
}
}
}
function reqsql2(db, query, waitfor){
temp.req = requestsql2(db, query, waitfor);
if(req.error != ""){
echo(req.error, query);
}
if(!waitfor){
return;
}
if(!req.completed){
waitfor(req, "onReceiveData", 60);
}
return req;
}
Weapon NPC (A buffer to handle triggerclient's)
-System/Client/Crossplayer
PHP Code:
function onActionServerside(varName, value) {
FindNPC("Crossplayer-Control").addSharedVariable(player, varName, value);
}
//#CLIENTSIDE
function onActionClientside(temp.packet) {
temp.target = findPlayer(temp.packet[0]);
temp.max = temp.packet.size();
temp.i = 1;
while (temp.i < temp.max) {
temp.target.(@temp.packet[temp.i++]) = temp.packet[temp.i++];
}
}
Join this simple class to the player
Crossplayer-Class
PHP Code:
//#CLIENTSIDE
public function shareVar(varName) {
triggerserver("gui", "-System/Client/Crossplayer", varName, player.(@varName));
}
3) Ensure everyone is joined to the player class and is given the weapon NPC.
QUICK EXAMPLE:
PHP Code:
//#CLIENTSIDE
function onPlayerChats() {
if (player.chat.tokenize()[0] == "/setsharedvar") {
player.(@player.chat.tokenize()[1]) = player.chat.tokenize()[2];
player.shareVar(player.chat.tokenize()[1]);
} else if (player.chat.tokenize()[0] == "/readsharedvar") {
player.chat = findPlayer(player.chat.tokenize()[1]).(@player.chat.tokenize()[2]);
}
}
Saying "/setsharedvar foo bar" will set player.foo to "bar". Then, saying on any other client, "/readsharedvar Hezzy002 foo" will show you the value of Hezzy002's player.foo value.
Thanks! It's helped me alot in development on my server, and I hope it helps out you guys too
*This is my first contribution, so please point out anything that I've done wrong. It's not a complicated script but I find it very useful. I'm not sure if there's more optimization that can be done or not, I did some optimization where I form it like a packet to handle multiple, instead of spamming the account name over the server and wasting bandwidth.
Let me know if you're going to use this for something, it'd be nice to know that something I wrote is floating out there being useful to other people!