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)

furry_mougle 08-31-2011 01:00 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];



That returns as a NULL value, you know?

Mark Sir Link 08-31-2011 01:36 AM

please stop using thiso throughout this script, you clearly have no idea what its intent and purpose is for. I am sure you are having all sorts of scoping issues by calling it since thiso probably refers to a null object or the player itself when you are doing that.

PHP Code:

//#CLIENTSIDE
function onCreated(){
  
this.rights = {"Drag""Boots""Blocks"};
  
this.selected 0;
}

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


or

PHP Code:

//#CLIENTSIDE
function onCreated(){
  
this.rights = {"Drag""Boots""Blocks"};
  
this.selected 0;
}

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



Astram 08-31-2011 10:25 PM

By the way, what do
? and % mean in scripting? My scripting reference says:

? = tertiary operator (condition?true-case:false-case)
% = mod; a%b = a - int(a/b)*b;

I don't really understand that...

cbk1994 08-31-2011 10:39 PM

? is basically a shorthand for some types of if-statements.

PHP Code:

condition value_if_true value_if_false 

i.e.

PHP Code:

temp.var = "";

if (
3) {
  
temp.var = "x was more than 3";
} else {
  
temp.var = "x was less than 3";


is equivalent to

PHP Code:

temp.var = (3) ? "x was more than 3" "x was less than 3"

As for modulus, think of it like remainder.

PHP Code:

// 0
// 2
// 1
10 // 3 



All times are GMT +2. The time now is 06:56 AM.

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