Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / How to resize multi-dimensional arrays?

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Mar 2017 01:32 Edited at: 3rd Mar 2017 01:36
I've already read through this thread without any luck. https://forum.thegamecreators.com/thread/212991

I'm trying to resize the 2D data array. The first dimension seems to compile without any errors, but trying to size the second dimension fails on every variation I've tried.


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Mar 2017 01:41
Doh, nevermind I figured it out. I overlooked Scraggle's explanation in the other thread.

1st dimension
array.length
2nd dimension
array[0].length
3rd dimension
array[0,0].length

I think that's very weird, but whatever.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Mar 2017 04:15
Ok, I'm still having issues. The array reports the size I'm changing it to, but I can't actually utilize any of those indices. Far as AppGameKit is concerned they don't exist.


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 3rd Mar 2017 06:02 Edited at: 3rd Mar 2017 06:03
shouldn't it be thing[0,5] = 13, seeing as thing[5] length is never set

Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 3rd Mar 2017 07:43

Quote: "I overlooked Scraggle's explanation in the other thread."

I don't know which thread you are referring to there but below is my SpriteGroup file (keep it, #include it, it's a very handy thing) and in that I resize multidimensional arrays.

AGK V2 user - Tier 1 (mostly)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Mar 2017 12:39 Edited at: 3rd Mar 2017 13:21
The only place where I see you resize it at is in your first function like:
spriteGroup.length = spriteGroup.length + 1

But what dimension does that increase? And when you insert a new element, spriteGroup[g].insert(s), where is it going (what's it indices)?

Quote: "shouldn't it be thing[0,5] = 13, seeing as thing[5] length is never set"


Not sure what you mean. When I write:
thing.length = 20
thing[0].length = 15

Is that not the same as declaring it as thing as integer[20,15] ? Because all that seams to do is allow the array to report back the lengths/size I've set but doesn't actually allow me to set any values at those indices.

My work around for the moment is to initialize my array with something very large. As resizing it to something smaller appears to work as expected. It's just not expanding the array properly. This isn't ideal as it's a huge waste of memory on start up (and slows things down), but at least it works and I can finish my project in the meantime.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 3rd Mar 2017 14:29
Does this clear it up?
AGK V2 user - Tier 1 (mostly)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Mar 2017 17:05
Not really. Focusing on just arr2 for a moment, how does it have 3 lengths when it was only defined with 2 dimensions?

Wait, I think I'm starting to get it. Each index is capable of defining a different size for it's other dimension? (which would be nice if the help docs stated that anywhere)

I drew up a diagram explaining how I think they work, which I think could help out a lot of other people.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Attachments

Login to view attachments
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 3rd Mar 2017 17:26 Edited at: 3rd Mar 2017 18:03
Quote: "Each index is capable of defining a different size for it's other dimension?"

Yes
Quote: " Focusing on just arr2 for a moment, how does it have 3 lengths when it was only defined with 2 dimensions"

Because we set its size to 2 so we have essentially said there are three (0, 1, 2) elements in the first dimension.

Continuing to focus on arr2, how's this?


Initially we've set arr2 to have 2 dimensions, each one with no size at all.
Then we set the first dimension to have a size of 2 (so three elements). Which is the same as this: arr2 as integer[2,-1]
Then in the first loop we have have set the size of the 2nd dimension and given each one a value
Finally, in the third loop we print it all out to prove it worked.
AGK V2 user - Tier 1 (mostly)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Mar 2017 18:19
Is there a shorthand method for setting the same size to all indices of a specific dimension without having to loop through them?


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 5th Mar 2017 11:37
That would be this:

But that, of course, only works if know the size at initialisation.
AGK V2 user - Tier 1 (mostly)

Login to post a reply

Server time is: 2024-04-23 17:45:24
Your offset time is: 2024-04-23 17:45:24