Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Array help (https://forums.graalonline.com/forums/showthread.php?t=134264405)

Astram 08-29-2011 07:19 PM

Array help
 
This wont select the next variable in thiso.rights.
thiso.rights = Stick,Unstick,Jail,Stealth
can someone help me get this to work?
PHP Code:

function onKeyPressed(codekeyscancode) {
  for (
temp.0temp.thiso.rights.size(); temp.++) {
    
//[ 219
    //] 221
    //\ = 220,\,43
    
if (code == 219) {
      
//[
      
if (this.selected => thiso.rights[0]) {
        
this.selectedid temp.i;
        
this.selected thiso.rights[(@this.selectedid 1)];
        
UpdateText(this.selected);
      }
    }
    if (
code == 220) {
      
//\
      
if (this.selected =< thiso.rights.size() - 1) {
        
this.selectedid temp.i;
        
this.selected thiso.rights[(@this.selectedid 1)];
        
UpdateText(this.selected);
      }
    }
  }



Crow 08-29-2011 07:28 PM

Why are you using a thiso prefix there? Should work with a regular this prefix.

Astram 08-29-2011 08:25 PM

Quote:

Originally Posted by Crow (Post 1666045)
Why are you using a thiso prefix there? Should work with a regular this prefix.

In a different part of the script im using thiso. because well I need it

Crow 08-29-2011 08:46 PM

But you do not need it in this case. You only need that particular prefix if you want to reference to the NPC the code is executed in while being in another NPC's scope, like inside some of this stuff:
PHP Code:

with (npc) {
  
// boo



Astram 08-29-2011 11:01 PM

Quote:

Originally Posted by Crow (Post 1666091)
But you do not need it in this case. You only need that particular prefix if you want to reference to the NPC the code is executed in while being in another NPC's scope, like inside some of this stuff:
PHP Code:

with (npc) {
  
// boo



Would that mess up the script like the problem im having...

Crow 08-29-2011 11:03 PM

Quote:

Originally Posted by Astram (Post 1666115)
Would that mess up the script like the problem im having...

Just try using a this prefix instead of the thiso one.

Astram 08-29-2011 11:07 PM

Quote:

Originally Posted by Crow (Post 1666116)
Just try using a this prefix instead of the thiso one.

Alright, gotcha, but do you know why the array isnt displaying anything besides this.rights[0]

Crow 08-29-2011 11:10 PM

Quote:

Originally Posted by Astram (Post 1666120)
Alright, gotcha, but do you know why the array isnt displaying anything besides this.rights[0]

Dunno. I was being lazy and assumed that the wrong prefix was the only problem. Let me take a closer look..

Edit: What do you want to do, anyway? Your conditions seem strange; you also got your operators the wrong way (=> and =< instead of >= and <=, but I'm not sure if that even matters). Also, you're converting an integer to a string for your array index here:
PHP Code:

this.selected thiso.rights[(@this.selectedid 1)]; 

Which isn't even necessary. This should do:
PHP Code:

this.selected this.rights[this.selectedid 1]; 


Astram 08-29-2011 11:32 PM

I'm trying to make it so that you can change which variable in the array you are showing, by pressing [ and \. Once like first it will say Stick, then you press \. And it would do UpdateText(); to say Unstick, then Jail, then Stealth. But it shouldn't go any higher then the array size, and the array minimum.

Crow 08-29-2011 11:39 PM

You could use the modulo operator for this one.
PHP Code:

this.selectedid = (this.selectedid 1) % this.rights.size(); 

Should also work for "scrolling" in the other direction. No need for a loop either. Just check the key code and do that.

gaben 08-29-2011 11:45 PM

I don't even know why you're looping it strangely in the first place, but you wrote temp.i < thiso.rights.size() instead of temp.i <= thiso.rights.size()? You'd be excluding one less array item.

In general though, there really isn't anything to 'fix' persay if we don't even know how you store your array vars (this.orights? too vague) or what you're really trying to do. Apparently you want to change the array with '[' or / yet nothing is indicating any keypresses besides onKeyPressed() which you didn't utilize properly anyway. Wat?

I guess you want to increment your array [] selectors, so why not just increment temp.i and de-increment them on '[' & / keypresses?

0PiX0 08-30-2011 12:05 AM

Quote:

Originally Posted by gaben (Post 1666134)
I don't even know why you're looping it strangely in the first place, but you wrote temp.i < thiso.rights.size() instead of temp.i <= thiso.rights.size()? You'd be excluding one less array item.

No, although his script doesn't require a loop, he is looping it correctly. In arrays, the first element's index is 0, and the last element's index is array.size() - 1.

gaben 08-30-2011 12:24 AM

Quote:

Originally Posted by 0PiX0 (Post 1666138)
No, although his script doesn't require a loop, he is looping it correctly. In arrays, the first element's index is 0, and the last element's index is array.size() - 1.

I didn't say he was looping it incorrectly, I just said it was rather strange compared to what he wanted to accomplish.

Anyway, it'd be much easier to just increment like this and perform some checks for this.i or whatever number he'll be using for the array [0] selector.

PHP Code:

//#CLIENTSIDE

function onCreated(){
  
this.0;
}

function 
onKeyPressed(codekey){
  if(
key == "[" && this.>= && this.3){
         
this.i++; // increment
         
player.chat this.i// print out for verification
  
}
  else if(
key == "/" && this.0){ // don't decrement 0 to -1!
        
this.i--;
        
player.chat this.i;
  }


Then I guess you'd do something like: thiso.rights[this.i] (selectors, 0,1,2,3).

0PiX0 08-30-2011 01:44 AM

Quote:

Originally Posted by gaben (Post 1666142)
PHP Code:

function onCreated(){
  
this.0;
}

function 
onKeyPressed(codekey){
  if(
key == "[" && this.>= && this.3){
         
this.i++; // increment
         
player.chat this.i// print out for verification
  
}
  else if(
key == "/" && this.0){ // don't decrement 0 to -1!
        
this.i--;
        
player.chat this.i;
  }



It's unnecessary to initialize a var to be set to 0. When incrementing, checking the condition 'this.i >= 0' is unnecessary. Also, you have the keys swapped.

Astram, I would use the method Crow described. In the end it should look somewhat like this:
PHP Code:

function GraalControl.onKeyDown(temp.keycodetemp.keystringtemp.scancode) {
  switch (
keycode) {
    case 
219:  this.selectedid = (this.selectedid 1) % this.rights.size();  break;
    case 
220:  this.selectedid = (this.selectedid 1) % this.rights.size();  break;
    default:   return;
  }
  
player.chat this.rights[this.selectedid];



gaben 08-31-2011 12:29 AM

Quote:

Originally Posted by 0PiX0 (Post 1666156)
It's unnecessary to initialize a var to be set to 0. When incrementing, checking the condition 'this.i >= 0' is unnecessary. Also, you have the keys swapped.

Astram, I would use the method Crow described. In the end it should look somewhat like this:
PHP Code:

function GraalControl.onKeyDown(temp.keycodetemp.keystringtemp.scancode) {
  switch (
keycode) {
    case 
219:  this.selectedid = (this.selectedid 1) % this.rights.size();  break;
    case 
220:  this.selectedid = (this.selectedid 1) % this.rights.size();  break;
    default:   return;
  }
  
player.chat this.rights[this.selectedid];



wat?

PHP Code:

//#CLIENTSIDE

function onCreated(){
  
this.rights = {"Stick","Unstick","Jail","Stealth"};
}


function 
onKeyPressed(codekey){ 
  if(
key == "[" && this.3){ 
         
this.i++; // increment 
         
player.chat this.rights[this.i]; 
  } 
  else if(
key == "/" && this.0){ // don't decrement 0 to -1! 
        
this.i--; 
        
player.chat this.rights[this.i]; 
  } 




All times are GMT +2. The time now is 08:23 AM.

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