![]() |
Forum Index : Microcontroller and PC projects : Need help with Multi-Dimensional Arrays
Author | Message | ||||
Atomizer_Zero Senior Member ![]() Joined: 04/07/2020 Location: United KingdomPosts: 134 |
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 KingdomPosts: 4311 |
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 KingdomPosts: 134 |
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 KingdomPosts: 134 |
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 StatesPosts: 3378 |
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: AustriaPosts: 133 |
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 StatesPosts: 719 |
> 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 KingdomPosts: 4044 |
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 KingdomPosts: 134 |
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. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |