Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 07-10-2010, 12:25 PM
Jiroxys7 Jiroxys7 is offline
Hazard to Graal
Jiroxys7's Avatar
Join Date: Apr 2009
Posts: 343
Jiroxys7 will become famous soon enough
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?
__________________
MY POSTS ARE PRONE TO EDITS!
Reply With Quote
  #2  
Old 07-10-2010, 12:28 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
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.
Reply With Quote
  #3  
Old 07-10-2010, 12:56 PM
Jiroxys7 Jiroxys7 is offline
Hazard to Graal
Jiroxys7's Avatar
Join Date: Apr 2009
Posts: 343
Jiroxys7 will become famous soon enough
so if i cant use in what should i use?
__________________
MY POSTS ARE PRONE TO EDITS!
Reply With Quote
  #4  
Old 07-10-2010, 01:17 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
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.
__________________
Reply With Quote
  #5  
Old 07-10-2010, 02:54 PM
Jiroxys7 Jiroxys7 is offline
Hazard to Graal
Jiroxys7's Avatar
Join Date: Apr 2009
Posts: 343
Jiroxys7 will become famous soon enough
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;
__________________
MY POSTS ARE PRONE TO EDITS!
Reply With Quote
  #6  
Old 07-10-2010, 03:56 PM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
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.
Reply With Quote
  #7  
Old 07-10-2010, 04:04 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by Loriel View Post
It tends to be a good idea not to use ? :.
Hm. Why?
Reply With Quote
  #8  
Old 07-10-2010, 04:23 PM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
Quote:
Originally Posted by Crow View Post
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;

Reply With Quote
  #9  
Old 07-10-2010, 06:06 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by Loriel View Post
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.
Reply With Quote
  #10  
Old 07-10-2010, 07:25 PM
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
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.
__________________
Quote:
Reply With Quote
  #11  
Old 07-10-2010, 09:24 PM
Jiroxys7 Jiroxys7 is offline
Hazard to Graal
Jiroxys7's Avatar
Join Date: Apr 2009
Posts: 343
Jiroxys7 will become famous soon enough
Quote:
Originally Posted by fowlplay4 View Post
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?
__________________
MY POSTS ARE PRONE TO EDITS!
Reply With Quote
  #12  
Old 07-10-2010, 09:40 PM
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
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.
__________________
Quote:
Reply With Quote
  #13  
Old 07-11-2010, 01:39 AM
ffcmike ffcmike is offline
Banned
Join Date: Jul 2004
Location: London
Posts: 2,029
ffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond repute
Send a message via AIM to ffcmike Send a message via MSN to ffcmike
Quote:
Originally Posted by Jiroxys7 View Post
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,};

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 01:21 AM.


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