Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Question about checking an array. (https://forums.graalonline.com/forums/showthread.php?t=134259278)

Jiroxys7 05-23-2010 07:17 AM

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?

cbk1994 05-23-2010 07:27 AM

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) { 


Jiroxys7 05-23-2010 07:58 AM

Thanks again, cbk! :D

xAndrewx 05-23-2010 11:02 AM

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!)

WhiteDragon 05-23-2010 11:06 AM

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.

xAndrewx 05-23-2010 12:46 PM

So before you do an internal loop, check with the size of the array and the length before comparing them!
(Thanks @ WD)

Jiroxys7 08-07-2010 11:49 PM

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?

cbk1994 08-08-2010 12:12 AM

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
  
}



Jiroxys7 08-09-2010 10:40 AM

Thanks I think i got it now :D


All times are GMT +2. The time now is 12:00 AM.

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