Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   array[0].name, not working? (https://forums.graalonline.com/forums/showthread.php?t=134256527)

scriptless 10-17-2009 11:58 AM

array[0].name, not working?
 
I know there is a way but I can't seem to remember how I accomplished it before. What I am trying to do is something like this:

NPC Code:
array[0].name = "test";
player.chat = array[0].name;



But, it keeps returning 0.

Any thoughts would be nice.

Crow 10-17-2009 12:05 PM

Quick guess:
I suppose "name" is a reserved word/var name. Try to use something else.

scriptless 10-17-2009 12:08 PM

Nope. Doesn't work.

I know this is possible. I think Devenio (sp?) helped me the first time about a year and a half ago maybe. *sigh*

Sometimes it does work tho. Like if I keep updating my script and changing stuff around then chaning it back to this it works.. No explination for why, but it doe, but not for long. THen it breaks again.

The only thing I think I can remember is something similar to this.

NPC Code:
with( test[0] ) {
a = "test";
}
player.chat = test[0].a;


Twinny 10-17-2009 12:39 PM

PHP Code:

function onCreated()
{
  
this.array = {1,2,3};
  
this.array[0].var = "test";
  
  echo(
this.array[0].var);


Just got that working on Testbed. As Crow said, you won't be able to use 'name' since it's reserved...

Crow 10-17-2009 12:39 PM

I tried this, and it works, even if I use "name". Are you doing this server- or clientside?

Edit: Twinny, it works with "name" as well. I just ran into something similar before, that's why I thought it wouldn't work (:

scriptless 10-17-2009 12:42 PM

Quote:

Originally Posted by Twinny (Post 1531294)
PHP Code:

function onCreated()
{
  
this.array = {1,2,3};
  
this.array[0].var = "test";
  
  echo(
this.array[0].var);


Just got that working on Testbed. As Crow said, you won't be able to use 'name' since it's reserved...

I know it is possible without:

NPC Code:
this.array = {1,2,3};



I am trying this for an array of a size that is unknown. So I need to be able to add to the array as needed. Could I just reset the size each time I add a value?

I think I got it now.

PHP Code:

//#CLIENTSIDE
function onCreated()
{
  
this.array = new TStaticVar();
  
this.array[0].var = "test";

  
player.chat this.array[0].var;



Crow 10-17-2009 12:46 PM

If temp.array[0] doesn't even exist yet, temp.array[0].name can't exist either.

Edit: Yea, static vars are your best shot I guess.

Twinny 10-17-2009 12:47 PM

What do you mean by reset the size?

PHP Code:

function onCreated()
{
  
this.array = {1,2,3};
  
this.array[0].var = "test";
  
  echo(
this.array[0].var);
  
this.array.add(4);
  
this.array.add(5);
  
this.array.remove(4);
  echo(
this.array);
  echo(
this.array[0].var);


echos

PHP Code:

test
1
,2,3,5
test 

If the array will be unsorted, give your var a special tag. Thus when you want to update it, just go through the array in a loop until you hit the tag.

scriptless 10-17-2009 12:50 PM

What I am trying to do is. Skip the need for

NPC Code:
this.array = {1,2,3};



And just do:

PHP Code:

 this.array[#].value = "blah"; 

But to do it in a for loop that size changes. I may not know if I need {1, 2} or {1,2,3}.

Here is what I mean, kinda:

PHP Code:

//#CLIENTSIDE
function onCreated()
{
  for( 
i=0i<10i++) {
    
temp.array[i] = new TStaticVar();
    
withtemp.array[i] ) {
      
this.var = "test:" SPC i;
    }
  }
  
  
player.chat temp.array[1].var;


Which does not work obviously.

Twinny 10-17-2009 12:53 PM

Sort of like,

PHP Code:

function onCreated()
{
  
temp.array.add(2);
  
temp.array.add("hi!");
  
temp.array.add("bogan");
  
  for (
t=0t<temp.array.size(); t++)
  if (
temp.array[t] == "hi!")
  {
    
thiso.pointer temp.array[t];
    
thiso.pointer.var = "woo";
    break;
  }
  echo(
this.pointer.var);


I think the index will have to have a non-null value before you can assign values to it unless you add an object instead.

[Edit]

Confirmed
PHP Code:

function onCreated()
{
  
temp.array[0] = "hi";
  
temp.array[1].var = "yes";
  echo(
temp.array[1].var);


doesn't work =(

scriptless 10-17-2009 12:58 PM

Kinda but not really.

This might help.

Instead of:
PHP Code:

temp.obj.name[0] = "bob";
temp.obj.name[1] = "eddy";
temp.obj.name[2] = "joe";
temp.obj.age[0] = "22";
temp.obj.age[1] = "23";
temp.obj.age[2] = "24"

I could use:
PHP Code:

withtemp.obj[i] ) {
this.name "name";
this.age "age";


I could then use my second example in a loop to assign each persons data in a much more easy to use manor. Because then all I would have to do to copy the data is copy what ever is at temp.obj[i].

As for the easily copy of the data I can worry about a function to do that if needed. But, do you see what I am saying?

Crow 10-17-2009 01:04 PM

Let's try to find a different solution then :p What are you trying to achieve? There are surely more ways to do what you want to do.

scriptless 10-17-2009 01:11 PM

Basically.

Instead of:
PHP Code:

array[0][0] = "bananna";
array[
0][1] = 1;
array[
1][0] = "orange";
array[
1][1] = 5

This:
PHP Code:

with( array[0] ) {
this.food "bananna";
this.price 1;
}
with( array[1] ) {
this.food "orange";
this.price 5;


See how this is much easier to udnerstand? Using multi-demension arrays can be such a pain in the butt sometimes. I would MUCH rather have arrays like my second example. But of course using a loop so I would not have to repeat these lines of code a million times.

I dont even know if you can use my first example and try and copy array[0] to newarray[0].. Possible? dunno. But you can see here I am looking at the array as the object. And giving info about it names rather then numbers in a multi demensional array.

Think of it as, when you hold something in your hand. That item has details about it.. Height, width, weight, color, smell... etc Would it make sence to say this items 0 value is 100 px.. its 1 value is 120px to explain to someone that its demensions are 100x120 ?

Crow 10-17-2009 01:18 PM

Well, associative arrays would be a nice thing to have, eh? You are not bound to arrays though. Why don't you just use multiple static vars? You could even combine them in a bigger one, like this:
PHP Code:

temp.core = new TStaticVar();
temp.core.= new TStaticVar();
temp.core.= new TStaticVar();

temp.core.a.name "blah";
temp.core.b.name "bleh"

And so on.

scriptless 10-17-2009 01:21 PM

Basically. I want to make a simple inventory.

array[i].name to be the item. Were i represents the index of the item in inventory. Each appending name after it to represent that detail about the item.

This, however, works. But is there any simpler way to do such a task? I mean I dont see why I must assign a value using add(). =/

PHP Code:

//#CLIENTSIDE
function onCreated()
{
  
temp.obj = new TStaticVar();
  for( 
i=0i<10i++) {
    
temp.obj.add("");
    
withtemp.obj[i] ) {
      
this.var = "test:" SPC i;
    }
  }


  
player.chat temp.obj[1].var;


Does:
NPC Code:
temp.obj = new TStaticVar();



Reset the array???

Also, this does not work. Any thoughts?

PHP Code:

//#CLIENTSIDE
function onCreated()
{
  
temp.obj = new TStaticVar();
  
temp.obj2 = new TStaticVar();
  for( 
i=0i<10i++) {
    
temp.obj.add("");
    
withtemp.obj[i] ) {
      
this.var = "test:" SPC i;
    }
  }
  
temp.obj2 temp.obj[0];

  
player.chat temp.obj2.var;


Just wondering about the above. Why would that not work?

Crow 10-17-2009 01:29 PM

I'm sure that this..
PHP Code:

temp.array = {}; 

..empties the array, if that's what you want.

Edit: Your last script doesn't work because you are trying to use the add() function although it does not exist. Careful, this only works on arrays. Neither temp.obj nor temp.obj2 is an array in your script.

scriptless 10-17-2009 01:37 PM

Add is working fine. I need temp.obj2 to point to the data I assign it to form temp.obj and i need this value in temp.obj2 to stay that data if I were to putnpc2.. =/

scriptless 10-20-2009 06:19 AM

Hey, back again. Here is what I got so far. But is there any way to use a function instead of having to use the 2 lines under //copy?

PHP Code:

//#CLIENTSIDE
function onCreated()
{
  
temp.obj = new TStaticVar();
  
temp.obj2 = new TStaticVar();
  
  for( 
i=0i<10i++) {
    
temp.obj.add("");
    
withtemp.obj[i] ) {
      
this.var = "test:" SPC i;
      
this.test "works:" SPC i;
    }
  }
  
  
//copy
  
temp.obj2.loadvarsfromarraytemp.obj[2].savevarstoarray(true) );
  
obj.test temp.obj2;
  
  
//test
  
player.chat obj.test.getdynamicvarnames();




All times are GMT +2. The time now is 09:44 AM.

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