Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:07 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Need help with Multi-Dimensional Arrays

Author Message
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 12:26pm 06 Aug 2020
Copy link to clipboard 
Print this post

I want to create an Array that has 3 index's, with the first being an ID, the second being an X co-ord and the third being a Y co-ord.
OPTION BASE 1
DIM MYARRAY(128,1,1)


So, for each ID, I want to use a loop and fill in the details for x and y.

I've tried a few ways. For example,
FOR ID = 1 TO 128
 MYARRAY(ID, X, Y) = MYARRAY(ID, X*8*16, Y*8*11)
   ?ID, X*8*16, Y*8*11
   X = X + 1
   IF X = 8 THEN
     X = 0
     Y = Y + 1
   END IF
NEXT ID


The intended results are:
MYARRAY(0, 0, 0)
MYARRAY(1, 128, 0)
MYARRAY(2, 256, 0)
...
MYARRAY(128, 1920, 616)


Here's the actual output:
0, 0, 0
1, 128, 0
ERROR: Index out of bounds.


Obviously, i'm doing it wrong... any help would be great, thanks
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 12:35pm 06 Aug 2020
Copy link to clipboard 
Print this post

  Atomizer_Zero said  
Here's the actual output:
0, 0, 0
1, 128, 0
ERROR: Index out of bounds.


Obviously, i'm doing it wrong... any help would be great, thanks


I think you're trying to look at index 128 of the second bound ... however that bound is only 2 elements long 0 & 1.

Regards,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 12:39pm 06 Aug 2020
Copy link to clipboard 
Print this post

I made an error in the initial post. I mixed up using BASE 1 and the code I was testing...

So for completenes

'this is the complete program.
DIM MYARRAY(127,1,1)
FOR ID = 0 TO 127
MYARRAY(ID, X, Y) = MYARRAY(ID, X*8*16, Y*8*11)
  ?ID, X*8*16, Y*8*11
  X = X + 1
  IF X = 15 THEN
    X = 0
    Y = Y + 1
  END IF
NEXT ID


I cant create an array with MYARRAY(127,0,0), which doesn't make sense, if base 0 is default.
Edited 2020-08-06 22:41 by Atomizer_Zero
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 01:24pm 06 Aug 2020
Copy link to clipboard 
Print this post

Im going to just do what the NES PPU does (lol).

Have an Object Attribute Array, where it has an Index (ID), and multiple poisitions in that for various data.

So,
OPTION BASE 1
DIM MYARRAY(128, 2)
FOR ID = 1 TO 128
 MYARRAY(ID,1) = X*8*16
 MYARRAY(ID,2) = Y*8*11
 X = X + 1
 IF X = 16 THEN
   X = 0
   Y = Y + 1
 END IF
NEXT ID


This should be good enough. I can expand the array downwards as needed, to include tile types and special flags, etc.
Edited 2020-08-06 23:34 by Atomizer_Zero
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 01:47pm 06 Aug 2020
Copy link to clipboard 
Print this post

  Atomizer_Zero said  DIM MYARRAY(127,1,1)
. .
FOR ID = 0 TO 127

There are 128 possible values for "ID = 0 TO 127", but you've only created buckets for 127 of them. Try "DIM MYARRAY(128,2,2)".

With option base 0, MYARRAY(127,1,1) should access the last element. OPTION BASE 0 can be confusing.

~
Edited 2020-08-06 23:50 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
PicFan
Senior Member

Joined: 18/03/2014
Location: Austria
Posts: 133
Posted: 06:41pm 06 Aug 2020
Copy link to clipboard 
Print this post

Try this ?


OPTION BASE 0

DIM MYARRAY(127,1,1)   „You have per ID, 4 Vars.“

FOR ID = 0 TO 127
  MYARRAY(ID, 0,0) = X*8*16
  MYARRAY(ID, 0,1) = Y*8*11
  MYARRAY(ID, 1,0) = A
  MYARRAY(ID, 1,1) = B
  ?ID, X*8*16, Y*8*11
  X = X + 1
  IF X = 8 THEN
    X = 0
    Y = Y + 1
  END IF
NEXT ID

 
hitsware2

Guru

Joined: 03/08/2019
Location: United States
Posts: 719
Posted: 07:00pm 06 Aug 2020
Copy link to clipboard 
Print this post

> OPTION BASE 0 can be confusing.

Having learned some BASIC before ' OPTION BASE '
was developed I find anything other than 0 confusing  
my site
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 07:29pm 06 Aug 2020
Copy link to clipboard 
Print this post

  Atomizer_Zero said  I made an error in the initial post. I mixed up using BASE 1 and the code I was testing...

So for completenes

'this is the complete program.
DIM MYARRAY(127,1,1)
FOR ID = 0 TO 127
MYARRAY(ID, X, Y) = MYARRAY(ID, X*8*16, Y*8*11)
  ?ID, X*8*16, Y*8*11
  X = X + 1
  IF X = 15 THEN
    X = 0
    Y = Y + 1
  END IF
NEXT ID


I cant create an array with MYARRAY(127,0,0), which doesn't make sense, if base 0 is default.

That code counts ID 0 to 127 and X from 0 to 15, Y from (unset, so zero) to (er, I think it's 7 or 8).

You'd need DIM MYARAY(127, 15, 7) 'or 8

Oh, hang on, you also use MYARRAY(ID, X*8*16, Y*8*11) so you need a lot bigger - use the max X then multiply it by 8*16 and max Y times 8*11.

I think you don't mean the code to be like that!

John
Edited 2020-08-07 05:30 by JohnS
 
Atomizer_Zero
Senior Member

Joined: 04/07/2020
Location: United Kingdom
Posts: 134
Posted: 09:47pm 06 Aug 2020
Copy link to clipboard 
Print this post

Thanks for all the replies. It is confusing for sure. It's just as confusing as getting the data back out again. But its all good. I'm just going to do what I mentioned in post 4, where I use a 2D array, with the first index will be the index of the map, and the second index will have "slots" for data.

it "looks" like this


MYARRAY(128, 3)

VISUALLY, IT'D LOOK LIKE THIS...
ID 1_
    |_x
    |_y
    |_whatever
ID 2_
    |_x
    |_y
    |_whatever
ID 3_
    |_x
    |_y
    |_whatever


Much like a struct in c++ or a class object in java, where you give it parameters such as X, Y and so on.. To access the data, you just say "MYARRAY(ID,1)" for "X", "MYARRAY(ID,2)" for Y, etc.

Quite simple I think.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025