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 08-27-2006, 09:46 PM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
Bind a variable to a function?

Ok, after my lack of success in the last thread I'm attempting to build an associative array manually. (Hopefully with more functionaility too, like being able to iterate through it.)

What I want to achieve is standard in OOP, but i'm not sure whether it's possible in GS.

The code will hopefully make more sense:
PHP Code:
function array(indexes,values) {
  
this.inds indexes;
  
this.vals values;
  
this.item item(); 
  return 
this;
}

function 
item(ind) {
   return 
this.vals[this.inds.index(ind)];
}

//then use like:
arr = array({"a","b","c"},{"one","two","three"});
var = 
arr.item("b"); //var should be "two" 
Is this kind of thing possible? I'm trying to achieve some pretty basic OO functionality.

If this is possible it'd really help me with a lot of scripts.

Thanks.
Reply With Quote
  #2  
Old 08-27-2006, 11:48 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
You can do things like this:

PHP Code:
myvar = new TStaticVar();
myvar.join("myarrayclass"); 
and then add functions into myarrayclass.
Reply With Quote
  #3  
Old 08-27-2006, 11:55 PM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
ah. Thanks.

Joined classes dont seem to get updated when I press apply. I just added a function to one and it's not recongnised. The ones I added originally are.

Also, if I change something it doesn't seem to update.
Reply With Quote
  #4  
Old 08-28-2006, 02:32 PM
ForgottenLegacy ForgottenLegacy is offline
-Backtoscripts-
Join Date: Aug 2003
Location: California
Posts: 289
ForgottenLegacy is on a distinguished road
Send a message via AIM to ForgottenLegacy
You need to recreate the TStaticVar. Call a destroy() to it in some script, then remake it. Should work.
__________________
"The higher you fly, the harder it is to breathe."

[Kaidenn] Maybe I will somehow take control of Lance's body when he isn't looking, have him log onto Kingdoms, update one script, and leave.
[Kaidenn] And leave him exactly where I found him, unchanged and completely unnaware of what just took place the last two minutes.
[GrowlZ] Lance: You might want to lock your bedroom door tonight
Reply With Quote
  #5  
Old 08-28-2006, 03:40 PM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
Where should destroy go?

Heres what I have:

PHP Code:
//#CLIENTSIDE

function onWeaponFired() {
arr = new TStaticVar(); 
arr.join("assocarray");
arr.make({"a","b","c"},{"one","two","three"});


echo(
arr.put("d","four"));
arr.put("seven","nine");
echo(
"Has:" arr.has("notakey"));
echo(
arr.keys());
arr.put("eight","ten");
echo(
"remove: " arr.remove("b"));
echo(
arr.keys());

//for(arr.start();arr.off();arr.forth()){
//player.chat = player.chat @ arr.item_for_iteration;
//}

arr.start();
while(!(
arr.off())) {
  
player.chat player.chat " " arr.item_for_iteration();
  
arr.forth();
}
arr.destroy();

I update the assocarry class and this weapon npc never shows the changes. For example, remove isn't working so i changed..

PHP Code:
public function remove(key) {
  if (
has(key)) {
    
this.values.delete(this.keys.index(key));
    
this.keys.delete(this.keys.index(key));
    return 
0;
  }
  else return 
1;

to

PHP Code:
public function remove(key) {
    
this.keys.delete(this.keys.index("b")); //in the example b exists as a key so this should work
    
this.values.delete(this.keys.index(key));
    
this.keys.delete(this.keys.index(key));
    return 
"test..."//return a string instead of a number

However, arr.remove("anything"); is still returning 0 instead of "test...".

I added arr.destroy(); to my NPC but it still never updates.

Is it because both the weapon NPC and the class npc have //#CLIENTSIDE ?


UPDATE: If I copy and paste the entire remove function and call it remove2, then call arr.remove2() it will execute as expected. remove() still executes as if it were never changed since the first time I called it even though the code is identical to remove2().

Any ideas? This is a bit strange and highly annoying because I don't know whether the script is working or not because I cant tell whether it's using the latest version of the function.

Last edited by JkWhoSaysNi; 08-28-2006 at 06:07 PM..
Reply With Quote
  #6  
Old 08-28-2006, 07:48 PM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
Fixed. I stopped using the function name remove() and all is fine. Is it a reserved function name or used for something else?

Anyway, it all seems to be working fine now.

Heres my associatve array class. Feel free to use it.

PHP Code:
//Associative array by JkWhoSaysNi
//#CLIENTSIDE
function duplicates(array) {
  
//recursivly check for duplicates in an array
  
duplicate false;
  if(array.
size() != 0) {
    for (
1<= array.size()-1i++ ) {
      if (array[
0] == array[i]) {
        
duplicate true;
      }
   }
   if (
duplicate == false) {
     array.
delete(0);
     
duplicate duplicates(array);
   }
  }
  return 
duplicate;
}
/////////////////
//Constructor
/////////////////
public function make(keys,values) {

 
//required because keys is passed as a reference to duplicates, which then empties it.
 
this.tempkeys keys

 if (
keys.size() != values.size() || duplicates(this.tempkeys)) return false
  else {
    
this.cursor 0;
    
this.keys keys;
    
this.values values;
    return 
true;
  }
}
/////////////////
//Access
/////////////////
public function has(key) {
    return (
this.keys.index(key) >= 0);
}
public function 
item(key) {
  if (
has(key)) return this.values[this.keys.index(key)];
  else return 
false;
}
public function 
has_item(value) {
  return (
this.values.index(value) >= 0);
}
public function 
keys() {
  return 
this.keys;
}
public function 
values() {
  return 
this.values;
}
public function 
item_for_iteration() {
  return 
this.values[this.cursor];
}
public function 
key_for_iteration() {
  return 
this.keys[this.cursor];
}
public function 
cursor() {
  return 
this.cursor;
}
public function 
count() {
  return 
this.values.size();
}
public function 
off() {
  
//has the cursor reached the end of the array?
  
return (this.cursor this.keys.size()-1);
}

/////////////////
//Element change
/////////////////
public function put(key,value) {
  
//add or replace
  
if (has(key)) {
    
this.values[this.keys.index(key)] = value;
    return 
1;
    }
  else {
    
this.keys.add(key);
    
this.values.add(value);
    return 
2;
   }
}
public function 
remove_item(key) {
 if (
has(key)) {
   
this.values.delete(this.keys.index(key));
   
this.keys.delete(this.keys.index(key));
   return 
true;
  }
  else return 
false;
}

public function 
replace_key(oldkey,newkey) {
  if (
has(oldkey)) { 
    
this.keys[this.keys.index(oldkey)] = newkey;
    return 
true;
  }
  else return 
false;
}
public function 
clear_all() {
  
this.keys.clear();
  
this.values.clear();
  
this.cursor 0;
 }
/////////////////
//Cursor movement
/////////////////
public function forth() {
   if (!
off()) {
     
this.cursor++;
     return 
true;
    }
    else return 
false;
}
public function 
back() {
  if (
this.cursor this.keys.size()-1) { 
    
this.cursor--;
    return 
true;
    }
  else return 
false;
}
public function 
start() {
//Move cursor back to the start
  
this.cursor 0;
}
public function 
end() {
  
this.cursor this.keys.size()-1;
}
//
//invariant this.keys.size() == this.values.size();
//invariant this.cursor < this.values.size();
// would be nice but not supported. 
usage:
PHP Code:
arr = new TStaticVar(); 
arr.join("associative_array");

//create the array. first param is an array of keys, .second is values. 
//if it works will return true, if there is a problem (e.g. duplicate key) it will return false.
arr.make({"a","b","c"},{"one","two","three"});

//add another element
arr.put("d","four");

//retrieve value for key "d"
var = arr.item("d"); //var == "four"

//change value for key
arr.put("d","five"); //arr.item("d") == "five"

//remove item
arr.remove_item("d");

//iterate through the array
for(arr.start();!arr.off();arr.forth()) {
  
player.chat player.chat " " arr.item_for_iteration();

and a few other functions which you can see by looking at the class.
Reply With Quote
  #7  
Old 09-11-2006, 04:03 PM
ApothiX ApothiX is offline
Okiesmokie
Join Date: May 2004
Posts: 1,447
ApothiX is on a distinguished road
You can also do:

PHP Code:
arr = array({"a","b","c"},{"one","two","three"}); 
with(arr) {
  
temp.var = item("b"); 

__________________


[06:24:19] * Parts: Skyld (i=silent@unaffiliated/skyld) ("Perhaps Okiesmokie did not realise that I like the boys. ")
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:08 AM.


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