![]() |
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? |
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}'? |
messy, I know.
PHP Code:
|
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. |
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. |
Quote:
Quote:
|
Quote:
|
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:
PHP Code:
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) |
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
|
Quote:
|
Quote:
|
Quote:
should never do it. |
Quote:
|
I guess best would to make something like
PHP Code:
|
| 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.