Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   allplayers global var (https://forums.graalonline.com/forums/showthread.php?t=134259800)

xAndrewx 07-12-2010 12:41 AM

allplayers global var
 
Having a bit of a problem...

I'm choosing a random player that isn't the player on another client, but its not optimizing as it should, any ideas?
HTML Code:

function onActionGrab() {
  temp.list = allplayers;
  echo(temp.list);
  while (temp.pl == null) {
    temp.pl = temp.list[int(random(0, temp.list.size()))];
   
    echo("testing" SPC temp.pl);
    echo("times" SPC temp.pl.computerfirstlogin SPC player.computerfirstlogin);
   
    if (temp.pl.computerfirstlogin = player.computerfirstlogin) {
      temp.list.remove(temp.pl);
      echo("REMOVED" SPC temp.pl);
      temp.pl = null;
    }
  }
 
  echo("found" SPC temp.pl);   
}

gives this result
HTML Code:

allplayers
testing Graal707396
times 1250035986 1250035986
REMOVED Graal707396
testing Graal707396
times 1250035986 1250035986
REMOVED Graal707396
testing Graal707396
times 1250035986 1250035986
REMOVED Graal707396
testing WanDaMan
times 1250035986 1250035986
REMOVED WanDaMan
testing Sp_lit
times 1262628732 1250035986
found Sp_lit

when really it shouldn't be checking the same account more than once.

fowlplay4 07-12-2010 12:57 AM

if (temp.pl.computerfirstlogin = player.computerfirstlogin) {

==

Skyld 07-12-2010 01:06 AM

You can't remove from the allplayers array (which I wonder if it is trying to do instead of copying the array) so instead use it read-only, it's also more efficient this way:
PHP Code:

function onActionGrab()
{
  while (
true)
  {
    
temp.pl allplayers[int(random(0allplayers.size()))];
    
    if (
temp.pl.computerfirstlogin == player.computerfirstlogin)
    {
      continue;
    }

    break;
  }
  
  echo(
"found" SPC temp.pl);    



xAndrewx 07-12-2010 01:15 AM

I was trying to replicate an array to save memory, skyld pointed it out that the loop will only ever run more than once if it matches someone using two accounts. Maximum loop size depending on how many clients are open and if they're even hit by the random number.

Thanks guys ^^

cbk1994 07-13-2010 06:41 AM

Note that this is incorrect code:

PHP Code:

echo("found" SPC temp.pl); 

temp.pl is the player object, instead you should do this:

PHP Code:

echo(temp.pl.account); 

I mention this because you will get unexpected results if you try to use that object as a string (e.g. using it in a variable name or sending it in a trigger).


All times are GMT +2. The time now is 07:46 PM.

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