Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Retrieving a subarray? (https://forums.graalonline.com/forums/showthread.php?t=134259781)

Jiroxys7 07-10-2010 12:25 PM

Retrieving a subarray?
 
Assuming I even know what I'm talking about, I've run into a problem where I cant seem to get the npcserver to check arrays within an array.

for example:
PHP Code:

function onCreated(){
  
this.somearray = {temp.otherarray1,temp.otherarray2,};
  
temp.otherarray1 = {"a","b","c",};
  
temp.otherarray2 = {"1","2","3",};
}

function 
doCheckStuff(){
  if(
"b" in this.somearray){
    
player.chat "It's there!";
  }
  else{
    
player.chat "Couldn't find it...";
  }


So when I call doCheckStuff(), it either won't want to check the entire array. or itll list it like -if i decided to send it to rc- something like this:
PHP Code:

NPC-Server (Server): "a,b,c","1,2,3" 

(Except, of course, when i use it with strings, dozens of additional quotation marks are thrown around seemingly haphazardly in what sendtorc returns.)

I could do something like if("b" in this.somearray[0]) but then theres the obvious problem that it wont check this.somearray[1]. Any advice?

Crow 07-10-2010 12:28 PM

When using the in operator, Graal is checking whether the specified element is inside the specified array. Since the specified array only contains two other arrays, "b" can't be found using the in operator.

Jiroxys7 07-10-2010 12:56 PM

so if i cant use in what should i use?

cbk1994 07-10-2010 01:17 PM

The only way I can think of...

PHP Code:

function arrayContains(array, part) {
  for (
temp.arrayPart : array) {
    if (
part in arrayPart) {
      return 
true;
    }
  }


If you have multi-dimensional arrays with more than one depth, though, you'll need to adjust that script.

Jiroxys7 07-10-2010 02:54 PM

Okay, I've now laid out the arrays in this manner:

Array 1 contains two arrays (Array 2 and Array 3). One contains valid equip types. the other contains valid armor types.

Array 2 contains the multiple main weapon groups (sword axe etc). And contains the various weapon subgroups. (short sword, long sword etc. would be under the sword group.) (the subgroups are arrays)

Array 3 contains the multiple main armor groups (armor, accessories, etc). In this array are more arrays containing the various armor subgroups.

I've somehow managed to end up with this:
PHP Code:

function doCheckType(array, equiptype){
  for (
temp.array[0] : array)
    if(
equiptype in array[0]){player.chat "Weapon!";break;}
    elseif(
equiptype in array[1]){player.chat "Armor!";break;}
      else{
        
player.chat "Item Error!";
        
sendtorc(player.account SPC "has encountered an item error! Invalid item type:" SPC equiptype[0] @".");break;
      }
   
player.chat = array;//for testing


One of my main problems is that once i do this once, for some reason, it wrecks my arrays. in which i mean that it makes everything null after the first use. One of my problems here is that I likely am still having trouble figuring out this for loop thing (I have no clue what ":" means in that. and might might as well add: I have no clue what some of these other symbols mean like "?" when used in this way. And theres no real list anywhere that I know of.)

edit:Actually, I could probably figure the rest of this out myself if i could explicitly understand how for loops work. Since it's absolutely impossible to search for this myself, what with the fact that 3 letter words are ignored by the search feature, and the graalbible nets you hundreds of unrelated results, does anyone have like a link on a tutorial on this or something? ยบ_o;

Loriel 07-10-2010 03:56 PM

for (a: b) { ... } means that b should be an array, and the {} block is going to be executed once for every element in b, with a refering to each element in turn.

If b was an array of two sub-arrays, then the {} block would be executed twice, once with a as the first subarray and once with a as the second subarray.

: is magical as part of the for thing here, the : in ? : is something different: (a ? b : c) is an expression that is going to return b if a is true and c if a is not true. It tends to be a good idea not to use ? :.

Hope this helps.

Crow 07-10-2010 04:04 PM

Quote:

Originally Posted by Loriel (Post 1586858)
It tends to be a good idea not to use ? :.

Hm. Why?

Loriel 07-10-2010 04:23 PM

Quote:

Originally Posted by Crow (Post 1586859)
Hm. Why?

Mostly because people get confused by it. Also then you do not get tempted to write code like I do.

NPC Code:
int bytesCount = c >= 0xfc ? 5 :
c >= 0xf8 ? 4 :
c >= 0xf0 ? 3 :
c >= 0xe0 ? 2 :
c >= 0xc0 ? 1 : 0;


Crow 07-10-2010 06:06 PM

Quote:

Originally Posted by Loriel (Post 1586860)
Mostly because people get confused by it.

To be honest, I don't care if others get confused by my code. I like using this method; in my opinion, it improves readability.

fowlplay4 07-10-2010 07:25 PM

Since arrays are strings, you can use variable.pos("b") if you know what kind of data you're receiving each time.

You should try a different approach instead of having multi-dimensional arrays.

Jiroxys7 07-10-2010 09:24 PM

Quote:

Originally Posted by fowlplay4 (Post 1586882)
Since arrays are strings, you can use variable.pos("b") if you know what kind of data you're receiving each time.

You should try a different approach instead of having multi-dimensional arrays.

Well around the time I got this working, I realized that I would just have to define what each number represents, which would add MUCH more code than I probably need. What would you recommend I use instead of multi-dimensional arrays?

fowlplay4 07-10-2010 09:40 PM

Well if you're making an equipment system there's quite a few ways you can store the information:

PHP Code:

enum SLOT
{
  
HEAD,
  
CHEST,
  
WEAPON,
  
FEET
}

function 
onCreated() {
  
this.equipment = {
    
"Helmet of Awesome",
    
"Golden Armorplate",
    
"Best. Sword. Ever.",
    
"Bunny Slippers"
  
};
  echo(
getHeadItem());
  
setHeadItem("Helmet of Change");
  echo(
getHeadItem());
}

function 
setHeadItem(item) {
  
this.equipment[SLOT.HEAD] = item;
}

function 
getHeadItem() {
  return 
this.equipment[SLOT.HEAD];
}

function 
getChestItem() {
  return 
this.equipment[SLOT.CHEST];
}

function 
getWeaponItem() {
  return 
this.equipment[SLOT.WEAPON];
}

function 
getFeetItem() {
  return 
this.equipment[SLOT.FEET];


enum is explained here.

or simply:

PHP Code:

function onCreated() {
  
this.equipment.head "Helmet of Laziness";
  
// etc.
  
echo(getHeadItem());
}

function 
getHeadItem() {
  return 
this.equipment.head;


Item systems that uses IDs instead of strings for their items would store the IDs in each slot instead.

Of course the examples above will have to be modified to meet your needs, and proper flags.

ffcmike 07-11-2010 01:39 AM

Quote:

Originally Posted by Jiroxys7 (Post 1586842)
Assuming I even know what I'm talking about, I've run into a problem where I cant seem to get the npcserver to check arrays within an array.

for example:
PHP Code:

function onCreated(){
  
this.somearray = {temp.otherarray1,temp.otherarray2,};
  
temp.otherarray1 = {"a","b","c",};
  
temp.otherarray2 = {"1","2","3",};



I know this is just an example so this isn't really a solution but that ought to be:

PHP Code:

function onCreated(){
  
temp.otherarray1 = {"a","b","c",};
  
temp.otherarray2 = {"1","2","3",};
  
this.somearray = {temp.otherarray1,temp.otherarray2,};




All times are GMT +2. The time now is 01:29 AM.

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