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 06-08-2005, 05:27 AM
calani calani is offline
Scriptess
calani's Avatar
Join Date: Aug 2003
Location: asmgarden.gmap
Posts: 606
calani is on a distinguished road
Send a message via AIM to calani
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?
__________________
Reply With Quote
  #2  
Old 06-08-2005, 05:31 AM
Kaimetsu Kaimetsu is offline
Script Monkey
Kaimetsu's Avatar
Join Date: May 2001
Posts: 18,222
Kaimetsu will become famous soon enough
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}'?
__________________
Reply With Quote
  #3  
Old 06-08-2005, 05:46 AM
calani calani is offline
Scriptess
calani's Avatar
Join Date: Aug 2003
Location: asmgarden.gmap
Posts: 606
calani is on a distinguished road
Send a message via AIM to calani
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;
  }

__________________
Reply With Quote
  #4  
Old 06-08-2005, 05:59 AM
Kaimetsu Kaimetsu is offline
Script Monkey
Kaimetsu's Avatar
Join Date: May 2001
Posts: 18,222
Kaimetsu will become famous soon enough
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.
__________________
Reply With Quote
  #5  
Old 06-08-2005, 06:06 AM
calani calani is offline
Scriptess
calani's Avatar
Join Date: Aug 2003
Location: asmgarden.gmap
Posts: 606
calani is on a distinguished road
Send a message via AIM to calani
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.
__________________
Reply With Quote
  #6  
Old 06-08-2005, 06:29 AM
Kaimetsu Kaimetsu is offline
Script Monkey
Kaimetsu's Avatar
Join Date: May 2001
Posts: 18,222
Kaimetsu will become famous soon enough
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.
__________________
Reply With Quote
  #7  
Old 06-08-2005, 11:49 AM
Trevor Trevor is offline
True
Trevor's Avatar
Join Date: Mar 2003
Posts: 2,253
Trevor is on a distinguished road
Quote:
Originally Posted by Kaimetsu
You are using sleep in a loop? Never use sleep in a loop!
Why not?
__________________


name: Trevor Fancher
email: trevor at fancher dot org
home page: fancher.org
Reply With Quote
  #8  
Old 06-08-2005, 01:31 PM
Velox Cruentus Velox Cruentus is offline
Registered User
Velox Cruentus's Avatar
Join Date: Dec 2004
Location: Quebec, Canada
Posts: 465
Velox Cruentus is on a distinguished road
Send a message via ICQ to Velox Cruentus Send a message via AIM to Velox Cruentus
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)
__________________
In a world of change... Who'll you believe?
Reply With Quote
  #9  
Old 06-08-2005, 10:21 PM
calani calani is offline
Scriptess
calani's Avatar
Join Date: Aug 2003
Location: asmgarden.gmap
Posts: 606
calani is on a distinguished road
Send a message via AIM to calani
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
__________________
Reply With Quote
  #10  
Old 06-08-2005, 11:12 PM
Kaimetsu Kaimetsu is offline
Script Monkey
Kaimetsu's Avatar
Join Date: May 2001
Posts: 18,222
Kaimetsu will become famous soon enough
Quote:
Originally Posted by Trevor
Why not?
You already know the answer, I think?
__________________
Reply With Quote
  #11  
Old 06-08-2005, 11:13 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
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!
__________________
Reply With Quote
  #12  
Old 06-08-2005, 11:45 PM
Trevor Trevor is offline
True
Trevor's Avatar
Join Date: Mar 2003
Posts: 2,253
Trevor is on a distinguished road
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.
__________________


name: Trevor Fancher
email: trevor at fancher dot org
home page: fancher.org
Reply With Quote
  #13  
Old 06-08-2005, 11:57 PM
Kaimetsu Kaimetsu is offline
Script Monkey
Kaimetsu's Avatar
Join Date: May 2001
Posts: 18,222
Kaimetsu will become famous soon enough
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.
__________________
Reply With Quote
  #14  
Old 06-14-2005, 01:04 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
I guess best would to make something like

PHP Code:
myarray = new[objectscount];
for (
i=0i<objectscounti++)
  
myarray[i] = {var1,{var2,var3},{var4,var5}}; 
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 10:27 PM.


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