View Single Post
  #1  
Old 06-22-2011, 09:30 PM
Mark Sir Link Mark Sir Link is offline
Kevin Azite
Mark Sir Link's Avatar
Join Date: Sep 2005
Posts: 1,489
Mark Sir Link is just really niceMark Sir Link is just really nice
Send a message via AIM to Mark Sir Link
Issue with joining player to class, using DBNPC functions.

Issue: After joining a player to a class, calling functions defined in the class only works within a with statement. Trying to execute them by other means causes the class itself to never be invoked. Example given:

I'm not sure what is causing this behavior in which class calls work fine inside of a with statement, but do not work outside of it.

PHP Code:
with(findplayer("Mark Sir Link")){
  
addhat3(922);  //successfully manipulates SQL row
  
echo(hashat3(922)); //returns true
}
findplayer("Mark Sir Link").addhat3(921); //does not call class
echo(findplayer("Mark Sir Link").hashat3(921)); //returns false
} 
class:
PHP Code:
function onCreated(){
  
this.sqlcontrol = findnpc("SQL-Control");
}
function 
hashat3(temp.i){
  
client.showhats = gethats3();
  return 
this.sqlcontrol.playerHasHat(player, i);
}

function 
addhat3(temp.i){
  
this.sqlcontrol.playerAddHat(player, i);
  
client.showhats = gethats3();
}

function 
removehat3(temp.i){
  
this.sqlcontrol.playerRemoveHat(player, i);
  
client.showhats = gethats3();
}

function 
gethats3(){
  return 
this.sqlcontrol.playerGetHats(player);
} 
DB-NPC:
PHP Code:
public function playerHasHat(temp.pl, temp.i){
  return (
i in playerGetHats(pl));
}

public function 
playerGetHats(temp.pl){
  
temp.query = "SELECT censored FROM censored WHERE account='%s'";
  
temp.ret = reqsql2("censored", format(query, pl.account), true);
  return 
ret[0].hats.tokenize(",");
}

public function 
playerRemoveHat(temp.pl, temp.i){
  
temp.hats = playerGetHats(pl);
  
temp.hats.remove(i);
  
temp.query = "UPDATE censored SET censored='%2$s' WHERE account ='%1$s'";
  
reqsql2("censored", format2(query, {pl.account, hats}), false);
  
savelog2("hatdbSQL_log.txt","hat" @ i @ " removed from " @ pl.account);
}

public function 
playerAddHat(temp.pl, temp.i){
  
temp.hats = playerGetHats(pl);
  
temp.hats.add(i);
  
temp.query = "UPDATE censored SET censored='%2$s' WHERE account ='%1$s'";
  
reqsql2("censored", format2(query, {pl.account, hats}), false);
  
savelog2("hatdbSQL_log.txt","hat" @ i @ " added to " @ pl.account);
}



function 
reqsql2(temp.db, temp.query, temp.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.rows;
} 
Reply With Quote