Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 08-04-2013, 02:56 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Offline Player Manipulation II

Here's a neat function I wrote to help reduce the amount of duplication involved with determining if a player is online and if not load their account, and if they don't exist bail on them. It also properly destroys loaded from HDD accounts.

Basically it's like doing:

PHP Code:
with (findplayer("account")) { // except it will load the player if they're offline
  
if (doesntexist) return;
  
// your code

The main functions (currently tweaked for use in onRCChat()):

PHP Code:
// Shortcut for processing that doesn't need to be done to offline accounts
function processOnlinePlayer(temp.pltemp.func) {
  
processPlayer(temp.pltemp.functrue);
}

function 
processPlayer(temp.pltemp.functemp.nohd) {
  
temp.op   player.communityname player.communityname player.account;
  
temp.pobj findplayer(temp.pl);
  
temp.onhd temp.pobj == NULL;
  if (
temp.nohd && temp.onhd) {
    echo(
format("Unable to find player %s, %s"temp.pltemp.op));
    return;
  }
  
temp.pobj temp.onhd ? new TServerPlayer(@temp.pl) : temp.pobj;
  
with (temp.pobj) {
    if (
player.onlinetime 0) { 
      
temp.save = (@temp.func)(playertemp.op);
    } else {
      if (
temp.op) {
        echo(
format("Unable to find player %s, %s"temp.pltemp.op));
      }
    }
  }
  if (
temp.onhd) {
    if (
temp.save) {
      
temp.pobj.saveaccount();
    }
    
temp.pobj.destroy();
  }

Examples:

PHP Code:
function onCreated() {
  
temp.func = function (temp.pl) {
    echo(
player.account);
    echo(
temp.pl.onlinetime);
    return 
false;
  };
  
processPlayer("fowlplay4"temp.func);
}

// Echos
// fowlplay4
// 8727432 
PHP Code:
function onCreated() {
  
// It passes two parameters to the function:
  // The player object
  // The original player's account (I.e. The player.account when called in onRCChat() first instance)
  
temp.func = function (temp.pltemp.op) {
    
temp.pl.head "head0.png";
    
temp.pl.attr[1] = "";
    
temp.pl.body "body1.png";
    if (
temp.op) echo(format("%s has reset %s's look!"temp.optemp.pl.account));
    return 
true// Necessary to save offline players.
  
};
  
processPlayer("fowlplay4"temp.func);

You can even nest it in the function call too:

PHP Code:
function onCreated() {
  
processPlayer("fowlplay4"temp.= function (temp.pl) {
    echo(
player.account);
    echo(
temp.pl.onlinetime);
  });
}

// Echos
// fowlplay4
// 8727432 
Or use defined functions:

PHP Code:
function onCreated() {
  
processPlayer("fowlplay4"this.test);
}

function 
test(temp.pltemp.op) {
  echo(
player.account);
  echo(
temp.pl.onlinetime);

__________________
Quote:

Last edited by fowlplay4; 08-04-2013 at 03:38 AM..
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 08:04 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.