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.

Newcomers AppGameKit Corner / Changing length of multi-dimensional arrays

Author
Message
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 28th Jan 2018 17:08
I've had a look through and seen examples but am still feeling dumb!

My understanding....
MyArray[10] creates an array 10 units long
If I do MyArray.length = 20 I now have the same as if I'd done MyArray[20]

So if I do...
MyArray[5,10] creates an array 5 wide and 10 deep

How do I change it using the .length command to end up with the equivalent of MyArray[5,20]
How do I change it using the .length command to end up with the equivalent of MyArray[15,10]
How do I change it using the .length command to end up with the equivalent of MyArray[15,20]

I'm hopeless at reading other people's code (I have discovered) so if you can keep it "Dummies Guide" level that would be great! Preferably just the command line that would extend the arrays to the lengths shown.. that would be great!

Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 28th Jan 2018 21:06 Edited at: 29th Jan 2018 11:40
Quote: "How do I change it using the .length command to end up with the equivalent of MyArray[5,20]"

it appears you do it like this
MyArray as integer[0,0]
MyArray.length = 5
for a = 0 to MyArray.length
MyArray[a].length = 20
next a

so you need to add a length for each part of the array
MyArray[0].length = 20
MyArray[1].length = 20
MyArray[2].length = 20
based on the examples in the help files
edit, lol, just realised it was linking to a file on my PC, no wonder the link didn't work
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 29th Jan 2018 12:08
Thanks.

But... Really?!?! That seems an odd way to process such a requirement?! You would have thought you could just increase one dimension with a single command?! Might have to start looking at .insert etc to see if that would be easier

Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Jan 2018 14:51
Multi-dimensional arrays are quite weird in AppGameKit in my opinion. Took me awhile to wrap my head around them. I've asked this same question before myself.

https://forum.thegamecreators.com/thread/219029
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 29th Jan 2018 17:24
It does seem a bit weird but apparently you can have different lengths for each "part".
I.e.
Array[0].length = 10
Array[1].length = 5
Meaning Array[0,10] is valid but Array[1,10] is not.
So I guess that's why it needs to be more complicated than expected?
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 29th Jan 2018 17:52
Yeah, this is odd - I should imagine most people would not want each dimension at different lengths to each part but rather add additional "rows" or "columns" to the array. My understanding of multi-dimensional arrays has just felt a tremor in the force.

The documentation is definitely lacking in this area. I can't see any of this covered in the docs? As far as I can see, it shows MyArray.length = 10 then goes on to talk about multi-dimension arrays but never covers how to use the .length command for them.
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Jan 2018 17:55
I've been contemplating writing a more definitive guide to agk arrays than what's currently available. But then I figured the questions have now been asked multiple times on the forum I didn't think it was necessary anymore. Maybe I should.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 29th Jan 2018 18:11 Edited at: 29th Jan 2018 18:12
Well the way they are implemented provides more flexibility. Basically instead of a multidimensional (in your case a fixed 2d array) these are jagged arrays (in C/C++/C# terms) which means an array of arrays.

So yes this means element 0 may have an array with 10 elements in it and element 1 may have an array with 5 elements in it. You can certainly use it in a fixed fashion but having the flexibility of jagged arrays is nice IMO.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 29th Jan 2018 18:12
Yeah, I'd still like a clear concise explanation of all the commands linke legth and insert etc for multi-dimensional arrays, plan and with types and sub-types. It's amazing that this isn't covered in the manual! :S

I'm sure I'm not the only one who would benefit but, I know how long it takes to clearly explain all this stuff so won't presume that you have the time/inclination!
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 29th Jan 2018 18:13
Agree jagged (great description!) arrays is good but can't help but think that ability to add a solid block (column/row type thing) with a command would be good.
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 29th Jan 2018 18:47
I agree I mean I can see it would take some getting used to if a person is not familiar with jagged arrays. Definitely get that.

Also AGK2 has an interesting thing to keep in mind regarding the .length property. It is not actually the true length of the array. It is actually the value used when declaring the array and is 0-indexed.

So .length=5 and .length returning 5 is actually an array that has 6 elements in it not 5.
Element 0
Element 1
Element 2
Element 3
Element 4
Element 5

If an array has 0 elements in it (basically an empty array) .length returns -1 which I find reasonable. I see -1 as a good flag to show the array is basically NULL.

They have it all documented quite well here.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 29th Jan 2018 19:06
OK so a question...

If I have a single dimension array that has been created and I want to quiz the value held in a certain cell , but clearly I don't want to request data from a cell that doesn't exist (ie beyond the scope of the array that will "crash" AGK) it's easy, I just check with

So, let's say I want to check cell number 5 (ie CheckCell = 5) I just do...

If CheckCell > Array.length
Tell user no-can-do and don't quiz the array or you'll get a "crash"
Else
ValueInCell = Array[CheckCell]
Endif

If I have a jagged multi-dimensional array, and I want to do the same what do I do....

So, let's say I want to check cell location [5,3]....

How would I safely go about ascertaining if there is such a cell in my array (in as simple and clear a way as possible so I can read the code) so that I don't request data from a cell that is beyond the scope of the array and therefore crashes?

Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 29th Jan 2018 19:25
Its fairly easy really x=5,y=3

if x<=MyArray.length and y<=MyArray[x].length
result = MyArray[x,y]
else
// Your doing something silly
endif
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 29th Jan 2018 20:08
Wow... yes. OK, I've missed the wood for the trees....
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 30th Jan 2018 15:43
Quote: "if x<=MyArray.length and y<=MyArray[x].length"


If X is greater than the length, would the expression terminate right there? Or will you get an out of bounds as it tries to evaluate the other expression?
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 30th Jan 2018 16:38
I wondered that - if so you could nest the If statements but guessed that the expression terminated after discovering the first was not true.. was going to test tonight.
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 30th Jan 2018 16:46 Edited at: 30th Jan 2018 16:56
Thats a really good point!! ...it depends on whether AppGameKit implements an AND or an ANDALSO operator?? Im used to C compilers at work that always short circuit an and operator. Every day is a learning day for me.

Like you said David...might just be better to Nest the if statements to make sure that it works no matter what implementation of AND is used

if x<=MyArray.length
if y<=MyArray[x].length
result = MyArray[x,y]
endif
endif

Technically this ^^ could be faulty code depending on whether you use 0 to length-1 or use 1 to Length. Both are valid and it is weird how an array of length "b" actually has "b+1" members in AGK. Quite odd when coming from other languages. That confused me for a while!

Theres a good Guide on Arrays for AppGameKit
https://www.appgamekit.com/documentation/guides/arrays.htm
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 30th Jan 2018 17:48
I started to write my own help doc for arrays, but you're right Ben, the existing guide covers just about everything already. However, reading over it I find some oddities, inconsistencies, and minor confusion. So I think I'm going to take what TGC already has and edit them slightly. I'll log each change I do and submit the whole thing to them.

Quote: " whether you use 0 to length-1 or use 1 to Length."

Or 0 to length. I think the docs need to specify a concrete standard, which at the moment I see it jump back and forth between 1 to length and 0 to length-1.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
PHeMoX
6
Years of Service
User Offline
Joined: 9th Jan 2018
Location:
Posted: 3rd Feb 2018 23:23
Yeah, this confused me quite a bit as well. I would like to see such an additional guide on arrays, would be useful. I had no idea they could be jagged like that.
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 4th Feb 2018 02:51
DBPro would throw an error, you would need to nest them. I suspect agk is like wise, but I'm not at my pc to test...
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 4th Feb 2018 10:28 Edited at: 4th Feb 2018 10:39
Nesting is needed in AppGameKit too as even if the first operand in an "AND" staement is false, AppGameKit still evaluates the second operand too.

Seems a wasteful practice but thats how it is.

Login to post a reply

Server time is: 2024-11-24 09:13:45
Your offset time is: 2024-11-24 09:13:45