Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   MultiDimensional Arrays (https://forums.graalonline.com/forums/showthread.php?t=58971)

DarkShadows_Legend 05-15-2005 01:32 AM

MultiDimensional Arrays
 
This is new to me and it looks weird. Will someone be kind enough to explain to me how they work and provide some basic examples. I just got GS2 on this world I'm developing and want to experiment with this stuff.

I have seen some scripts that have lines of code like ...
NPC Code:

myvar = {{{val}}, val2, val3};



I believe there is another way of creating them for GS2 that is like old GS1
Quote:

GS1 -> setarray myvar,length;
GS2 -> setarray(myvar,length); // not quite sure
How would a multidimensional array be set for GS2?

If anyone could explain this to me and provide a couple of examples I would be extremely greatful. Thanks

Velox Cruentus 05-15-2005 02:03 AM

var = new[length];

var[x] = new[x];

Or:

var = {{x,y},{x,y}};

You can grab the variables by:

var[1][1];

Kaimetsu 05-15-2005 09:29 AM

Quote:

Originally Posted by Velox Cruentus
var = new[length];

I think there's also a way to have the array be initialised as multi-dimensional. new[height,width]?

Zero Hour 05-15-2005 04:32 PM

What's advantages does a multidimensional array have? I cannot understand any use.

Evil_Trunks 05-15-2005 04:56 PM

it's basically an easier to use string list, i think

normally when you want to create a structure like this

NPC Code:
item1
- sub info 1
- sub info 2
- sub info 3
item2
- sub info 1
- sub info 2
- sub info 3
item3
- sub info 1
- sub info 2
- sub info 3



you have to use a long string list:

NPC Code:
sub info 1,sub info 2, sub info 3, sub info 1,sub info 2, sub info 3, sub info 1,sub info 2, sub info 3



and use simple math to figure out where the info is (the index is i*n+3 for this example, where i is the item and n is the sub item)

with a multidimensional array, it's a little easier

NPC Code:
array = {{sub info 1,sub info 2, sub info 3},{sub info 1,sub info 2, sub info 3}, {sub info 1,sub info 2, sub info 3}}



you can access the items with:

array[i][n]

Velox Cruentus 05-15-2005 05:02 PM

Now -- The utility can be a variety of things... First off, the arrays can now carry texts...
You can use it to create a background for easy access to the x and y coordinates, such as:

PHP Code:

  this.levelt := new[64,64];
  for (
0< (64*64); i++)
    
this.level[i%64][int(i/64)] = tiletype(i%64,int(i/64)); 

And then grab the variables by:
this.levelt[x][y]; Making it just a different way to bypass it, or store variables. This could be used to make minigames with tiles of your own set, were you create the blocking via tiles:
PHP Code:

this.var := {
 {
0,1,1,0,0,2,0},
 {
0,1,0,0,1,2,0},
 {
0,0,0,1,2,2,1}
}; 

Or... You can use it, in the case that I did in my Item System script:
PHP Code:

this.item := {{"Prop",var},{"Prop",var}}; 

It all depends on what you want to use it for.

Zero Hour 05-15-2005 05:17 PM

Ah, I see :)

PrinceDark 05-15-2005 08:48 PM

This is a very nice addition. Saves me the trouble of creating so many separate arrays/string lists. Thanks all. :cool:

Evil_Trunks 05-16-2005 05:54 AM

Quote:

Originally Posted by Velox Cruentus
PHP Code:

  this.levelt := new[64,64];
  for (
0< (64*64); i++)
    
this.level[i%64][int(i/64)] = tiletype(i%64,int(i/64)); 


loading an entire level into a string list lagged the server a LOT when I did it before, and the new multidimensional (string) arrays seem to be a lot like string lists in disguise....

Velox Cruentus 05-16-2005 01:44 PM

Try clientside. It was an example.

It is a string list... Easier to access them (Infact, you can get the index of a stringlist as such: string[index]).

=) Strings and variables are interchangible now (which is why you can do: player.chat == "Woop!")

Evil_Trunks 05-16-2005 11:54 PM

Quote:

Originally Posted by Velox Cruentus
=) Strings and variables are interchangible now (which is why you can do: player.chat == "Woop!")

there is still a big speed difference between using numeric arrays and string lists/string arrays

Velox Cruentus 05-17-2005 01:22 AM

Of course -- Digits take a lot less space then letters. It's pretty much known. Take this for example:

(-- binary, 2 bytes in length --)
5 (5) = 0000 0101
A (65) = 0100 0001

Now... Which one do you think has the least data to support? Plus they have to mention that the latter binary is a string. More calculations to make... Funness.

Kaimetsu 05-17-2005 01:40 AM

Quote:

Originally Posted by Velox Cruentus
Of course -- Digits take a lot less space then letters

Dude, a digit is a character just as much as a letter is.

Quote:

(-- binary, 2 bytes in length --)
5 (5) = 0000 0101
A (65) = 0100 0001
Those representations are not two bytes long.

Quote:

Now... Which one do you think has the least data to support?
What are you attempting to prove? Both of those representations use the same amount of bytes.

Velox Cruentus 05-17-2005 01:49 AM

They are? I thought numbers are directly equivelent to what they mean, whereas letters are...

A byte isn't 4 bits?

Kaimetsu 05-17-2005 01:56 AM

Quote:

Originally Posted by Velox Cruentus
They are? I thought numbers are directly equivelent to what they mean, whereas letters are...

You said 'digit', not 'number'.

Quote:

A byte isn't 4 bits?
No.

Velox Cruentus 05-17-2005 02:01 AM

Ah. Yea... They are 8 bits. I thought it was 4 bit before. :-/

I meant 'number'.

Evil_Trunks 05-18-2005 01:57 AM

Quote:

Originally Posted by Velox Cruentus
Of course -- Digits take a lot less space then letters. It's pretty much known. Take this for example:

(-- binary, 2 bytes in length --)
5 (5) = 0000 0101
A (65) = 0100 0001

Now... Which one do you think has the least data to support? Plus they have to mention that the latter binary is a string. More calculations to make... Funness.

I am saying that I think the engine internally converts a numeric array to a string list if you try to store characters in the array, causing a drastic speed reduction

Of course that is just my speculation, and only Stefan can really say (*nudge*)

Admins 05-18-2005 06:34 PM

Quote:

Originally Posted by Evil_Trunks
I am saying that I think the engine internally converts a numeric array to a string list if you try to store characters in the array, causing a drastic speed reduction

It is using floating point (64 bit) values, strings, lists or pointers depending on what variables you add to the array. A floating point value is only converted to string if you request it as string, e.g. if you output the value with echo/sendtonc.

Speed comparisons: String lists on serverside are really slow right now, although you need to use quite huge string lists to see a speed difference in the script. The string lists are more optimized on clientside. Also numeric arrays are much faster.
In new scripting engine it is using the same mechanism for normal numeric arrays and for string lists. They are probably slightly slower than numeric arrays (10-20%), but of course much faster than string lists. With Graal version 4 you also have the advantage that the script engine is coded in C/C++, so even numeric arrays should be faster than with v2.

Admins 05-23-2005 03:17 PM

Bad code detected:
Initializing multi-dimensional arrays is done like this


arr = new[2][2][2]


You can also intiialize the subarrays manually e.g. to get different sized subarrays, but stuff like new[2,2] will not work

Kaimetsu 05-23-2005 03:41 PM

Quote:

Originally Posted by Stefan
Bad code detected

Yeah, my bad. I've never ever used multidimensional arrays in C, so I forgot how the syntax worked. In my opinion, the [x,y] syntax makes more sense when the created array is a contiguous block of memory, but that doesn't really apply to Graal.


All times are GMT +2. The time now is 07:35 PM.

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