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 05-23-2010, 07:17 AM
Jiroxys7 Jiroxys7 is offline
Hazard to Graal
Jiroxys7's Avatar
Join Date: Apr 2009
Posts: 343
Jiroxys7 will become famous soon enough
Question about checking an array.

So i'm learning about how all this works. and ive stumbled across a problem.
lets say i'm storing a variable in an array, and i want the script to check to see if they already have that variable in the array before adding it again. (to prevent duplicates)

i've tried a few things such as using if("varthing": in client.varname) or whatever and strcontains wasnt the answer either. what should i be using to check?
__________________
MY POSTS ARE PRONE TO EDITS!
Reply With Quote
  #2  
Old 05-23-2010, 07:27 AM
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
PHP Code:
temp.myArray = {"test1""test2""test3"};

if (
"test1" in myArray) {
  
// it is in the array
}

if (! (
"test4" in myArray)) {
  
// not in the array, add it
  
myArray.add("test4");

Keep in mind that for checking if an object is in an array you must enclose the "in" statement in parenthesis. Otherwise the "not" symbol (exclamation point) is taken to indicate that the variable should be "not" what it is.

Correct:
PHP Code:
if (! (obj in array)) { 
Incorrect:
PHP Code:
if (! obj in array) { 
__________________
Reply With Quote
  #3  
Old 05-23-2010, 07:58 AM
Jiroxys7 Jiroxys7 is offline
Hazard to Graal
Jiroxys7's Avatar
Join Date: Apr 2009
Posts: 343
Jiroxys7 will become famous soon enough
Thanks again, cbk!
__________________
MY POSTS ARE PRONE TO EDITS!
Reply With Quote
  #4  
Old 05-23-2010, 11:02 AM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
You can also compare array lists

HTML Code:
temp.a_0 = {"list", "lista"};
temp.a_1 = {"list, "listb"};

if (@temp.a_0 == @temp.a_1) //Checks if the arrays are the same

or

if (@temp.a_0 != @temp.a_1) //Checks if the array isn't the same
(This can come in handy, thought I'd let you know!)
__________________
Reply With Quote
  #5  
Old 05-23-2010, 11:06 AM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Just a note on the @temp.some_array syntax... when @ casts the array to a string, it will do a loop internally. This means that the @ operation on an array will take longer as the size of the array gets bigger.


Given the arrays N and M, it will take N.size() operations, then M.size() operations to convert them both to strings.
Then, to actually compare the two strings, it will take min(StringN.length(), StringM.length()) operations at worst (if they aren't equal).

So, that method of checking equality actually takes more operations than a GS2 loop.


However, since it is the engine doing the conversion to a string and the string equality check, it will be faster than doing a loop in GS2 (probably).


The one case where a GS2 loop would be better is if the arrays are really big, but differ near the beginning. This would be faster because you could break the loop on the first inequality. Converting the array to a string wouldn't be able to do that, which could be deadly on a really big array.

Last edited by WhiteDragon; 05-23-2010 at 11:19 AM..
Reply With Quote
  #6  
Old 05-23-2010, 12:46 PM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
So before you do an internal loop, check with the size of the array and the length before comparing them!
(Thanks @ WD)
__________________
Reply With Quote
  #7  
Old 08-07-2010, 11:49 PM
Jiroxys7 Jiroxys7 is offline
Hazard to Graal
Jiroxys7's Avatar
Join Date: Apr 2009
Posts: 343
Jiroxys7 will become famous soon enough
Alright I have a semi-related problem so I'll post it here instead of creating a new topic.
Working on a buff/debuff system I've decided to store the data I need a certain way:
e.g.: client.buffs = "Slow:60:-50","Bleed:25:100"

the 60 on the slow debuff represents the timer. so does the 25 on bleed.

now here's my problem: I seem unable to set the values representing the time left to another value. I had already changed my format thinking it was that i had all of the data for each buff as a string thinking maybe you can only change nonstring/nonquoted numbers. but i've changed it back since that didnt work.

This is the function I'm currently using to lower this value:
PHP Code:
function doReduceBuffTimer(){
  
temp.buffs client.buffs.tokenize(",");
  
temp.count temp.buffs.tokenize(",").size();
  for(
temp.0temp.temp.counttemp.++){ 
    
client.buffs.tokenize(",")[temp.c].tokenize(":")[1] -= 0.05;
  }

What am I doing wrong?
__________________
MY POSTS ARE PRONE TO EDITS!
Reply With Quote
  #8  
Old 08-08-2010, 12:12 AM
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
Try multi-dimensional arrays instead.

PHP Code:
temp.myArray null;

myArray.add({"1""2""3"});
myArray.add({"4""5""6"});

echo(
myArray[0][0]); // echoes 1
echo(myArray[0][2]); // echoes 3

echo(myArray[1][0]); // echoes 4 
Then when adding a buff you can do something like

PHP Code:
public function addBuff(skilltimeoption) {
  
player.clientr.buff.add({skilloptiontime});

and when decreasing the timer

PHP Code:
for (temp.player.clientr.buff.size() - 1>= 0--) {
  
player.clientr.buff[i][2] -= 1;
  if (
player.clientr.buff[i][2] <= 0) {
    
player.client.buff.delete(i); // remove the buff, ≤ 0 time
  
}

__________________
Reply With Quote
  #9  
Old 08-09-2010, 10:40 AM
Jiroxys7 Jiroxys7 is offline
Hazard to Graal
Jiroxys7's Avatar
Join Date: Apr 2009
Posts: 343
Jiroxys7 will become famous soon enough
Thanks I think i got it now
__________________
MY POSTS ARE PRONE TO EDITS!
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 10:13 PM.


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