Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Multidimentional arrays (https://forums.graalonline.com/forums/showthread.php?t=59331)

calani 06-08-2005 05:27 AM

Multidimentional arrays
 
I recently started a project that should have been fairly simple - a rudimentary 2d gravity simulator. I was later going to make this into 3d and have some fun with it after i got the basics working.. however, I encountered a very annyoing problem: I couldn't get my array to work.
Hearing that GS2 had multidimentional array support (yes, I'm behind the times a bit due to being extremely busy), I arranged everything so it would be easy access and keep the script looking clean.

here's my array format:
objects_array={object,object,object,..}
object={image,{x,y},{dx,dy},{movable,mass}}

so, a sample two object array would be:
{{"crystalflash.gif",{3,-3},{-2,1.8},{1,500}},{"crystalflash.gif",{60,28},{-3,-1},{1,256}}

I haven't been able to get setarray( ) to work with this... I tried setting it to the number of sub-arrays, and then setting each individual array element, but reading them always returns a zero.


What am I doing wrong?

Kaimetsu 06-08-2005 05:31 AM

Could you show your actual script?

Normally the 'new' operator is used for array initialisation.

objects_array = new[20];
objects_array[0] = new[4];
objects_array[0][1] = new[2];
etc.


Of course, that's all very long and convoluted, which is why I wouldn't use as many arrays as you. Why not just use a format like '{image,x,y,dx,dy,movable,mass}'?

calani 06-08-2005 05:46 AM

messy, I know.



PHP Code:

//NPC made by *calani

//#CLIENTSIDE
function onCreated()  init();

function 
init() {
 
// object data: { image, {x,y}, {dx,dy}, {moveable, mass} }

 // index pointers
  
x=1;      // x
  
y=2;      // y
  
image=0;  // image
  
coords=1// xy
  
delta=2;  // dxy
  
extra=3;  // other data

  
this.objectscount=5;
  
setarray(this.objects,this.objectscount);
// ranges added in for ease while debugging
  
this.range.xy={{-5,5},{-60,60}};
  
this.range.dxy={{-3,3},{-3,3}};
  
this.range.mass={1,500};

  for(
i=0;i<this.objectscount;i++) {
    
this.objects[i][coords][x] = random(this.range.xy[x][1],this.range.xy[x][2]);
    
this.objects[i][coords][y] = random(this.range.xy[y][1],this.range.xy[y][2]);

    
this.objects[i][delta][x] = random(this.range.dxy[x][1],this.range.dxy[x][2]);
    
this.objects[i][delta][y] = random(this.range.dxy[y][1],this.range.dxy[y][2]);

    
this.objects[i][extra][1] = int(random(0,2)); // bool
    
this.objects[i][extra][2] = random(this.range.mass[1],this.range.mass[2]);

/*
    this.string=i @ ": {"@this.objects[image]@", {"@this.objects[i][coords][x]@","@this.objects[i][coords][y]@"}, {"@this.objects[i][delta][x]@","@this.objects[i][delta][y]@"}, {"@this.objects[i][extra][x]@","@this.objects[i][extra][y]@"}}";
//    player.chat=this.string;
    triggeraction(0,0,serverside,"*gravitytest","tonc","*gravitytest: " @ this.string);
*/
    
sleep .05;
  }



Kaimetsu 06-08-2005 05:59 AM

You are using sleep in a loop? Never use sleep in a loop!

Also, I am confused about 'this.range.xy'. Where is the 'xy' property defined? You can't just invent these arbitrarily. You need to address the relevant array elements directly.

calani 06-08-2005 06:06 AM

variable naming convention that lets me see at a glance what variables are when i have a bunch of similar ones - bad habit from gs1.
I'll break off of this if i can get arrays working >.<

In the old engine, variable names could contain periods, not sure in the new.



edit: and as for using sleep in a loop - I've come across a few situations where its necessary, such as a huge loop (10000+ loops).. if they exceed 10000, the script breaks, so add in a sleep every so many loops to ensure it works. I used this a script to see if the random function was a bell curve or flat (256000 iterations).. turns out to be remarkably flat ^ ^. In this instance I added it so I didn't get a 2-3 second bit of lag whenever I update the script, plus also i had a player.chat= for debugging, and I wanted to see what it said.

Kaimetsu 06-08-2005 06:29 AM

Quote:

Originally Posted by calani
In the old engine, variable names could contain periods, not sure in the new

Nono. Bad, bad idea. GS2 is much more object-oriented - if you go throwing dots into variable names then you're just gonna confuse the poor thing.

Quote:

edit: and as for using sleep in a loop - I've come across a few situations where its necessary, such as a huge loop (10000+ loops).. if they exceed 10000, the script breaks
Well, I don't see how that necessitates sleeping. Timeouts would work just fine.

Trevor 06-08-2005 11:49 AM

Quote:

Originally Posted by Kaimetsu
You are using sleep in a loop? Never use sleep in a loop!

Why not?

Velox Cruentus 06-08-2005 01:31 PM

x and y are the weapon's or player's variable, and there's a conflict of common variable.

this.objects = new[this.objectcount][3][2];

PHP Code:

  for(i=0;i<this.objectscount;i++) {
    
this.objects[i][coords][x] = random(this.range.xy[x][1],this.range.xy[x][2]);
    
this.objects[i][coords][y] = random(this.range.xy[y][1],this.range.xy[y][2]);

    
this.objects[i][delta][x] = random(this.range.dxy[x][1],this.range.dxy[x][2]);
    
this.objects[i][delta][y] = random(this.range.dxy[y][1],this.range.dxy[y][2]);

    
this.objects[i][extra][1] = int(random(0,2)); // bool
    
this.objects[i][extra][2] = random(this.range.mass[1],this.range.mass[2]);
    
sleep .05;
  } 

That part is very messy.

PHP Code:

  for(i=0;i<this.objectscount;i++) {
    
this.objects[i][0][0] = random(-5,5);
    
this.objects[i][0][1] = random(-60,60);

    
this.objects[i][1][0] = random(-5,5);
    
this.objects[i][1][1] = random(-60,60);

    
this.objects[i][2][0] = int(random(0,2)); // bool
    
this.objects[i][2][1] = random(1,500);
    
sleep .05;
  } 

Some repeated errors were that you count "1,2,3,4", but the array are counted "0,1,2,3". Most of the time, you were asking for arrays that didn't even exist.
Other then that, you were trying to initiate arrays that weren't even called (You only have 5 arrays, you didn't include three subarrays it's 2 sub arrays for them)

calani 06-08-2005 10:21 PM

Yes, I fixed those last night and got it working. i still like using the coords, delta, etc variables, though, as i'll be changing the structure of the array later, and thus will only need to change one area to update it

Kaimetsu 06-08-2005 11:12 PM

Quote:

Originally Posted by Trevor
Why not?

You already know the answer, I think?

Inverness 06-08-2005 11:13 PM

Quote:

Originally Posted by Kaimetsu
You are using sleep in a loop? Never use sleep in a loop!

Why not? I do not believe that setTimer() would stop the execution of the script beyond that point for the specified time. Therefor you use sleep() to prevent lag. :O!

Trevor 06-08-2005 11:45 PM

Quote:

Originally Posted by Kaimetsu
You already know the answer, I think?

Although I would personally never put a sleep in a loop, I don't see why someone
should never do it.

Kaimetsu 06-08-2005 11:57 PM

Quote:

Originally Posted by Inverness
Why not? I do not believe that setTimer() would stop the execution of the script beyond that point for the specified time. Therefor you use sleep() to prevent lag

What? I don't see what you're saying. You seem to be positing a false dichotomy.

Admins 06-14-2005 01:04 PM

I guess best would to make something like

PHP Code:

myarray = new[objectscount];
for (
i=0i<objectscounti++)
  
myarray[i] = {var1,{var2,var3},{var4,var5}}; 



All times are GMT +2. The time now is 11:12 AM.

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