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 / [SOLVED] Array Crash

Author
Message
Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 24th Jul 2018 05:14 Edited at: 24th Jul 2018 05:16
This causes a crash:



Am I doing something wrong?

The author of this post has marked a post as an answer.

Go to answer

fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 24th Jul 2018 05:52 Edited at: 24th Jul 2018 06:11
you have 2 by 2 array
you then try and change the dimension

but there is still nothing in the array so you will get an error

if you want to be able to increase an array size try this way

That method can be used for a huge array size and you can use num.remove in the above
example to delete elements. It is a great method for spawning enemies etc

print(num[7].x) to print the x value at the 7th element
fubar
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 24th Jul 2018 05:56 Edited at: 24th Jul 2018 05:59
Looks like Blarg[0].length and Blarg[7].length are originally defined as 2. You increased Blarg[0].length to 8. You are accessing the 6th element in Blarg[7] not Blarg[0] in the mouse click.
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)
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 24th Jul 2018 08:55 Edited at: 24th Jul 2018 08:59
This post has been marked by the post author as the answer.
To put it simply ...your doing it wrong

You start with a 2x2 array (which is actually 3x3 due to index 0 and index 2 being valid in AGK)

xxx
xxx
xxx

then you change
Blarg.length = 8 // Change size of dimension 1

xxxxxxxxx
xxx
xxx

Blarg[0].length = 8 // Change size of dimension 2 - NO IT DOESNT

xxxxxxxxx
xxx
xxx
x
x
x
x
x

Then you try to access Blarg[7,5] which doesnt exist
If you had added Blarg[7].length = 8 // Change size of dimension in column 7 then it would be fine

If you want to resize the second dimension of your array you have to resize all the internal elements of it in a loop or re-dim the whole array

If you want it to be 8,8 you can just use
dim Blarg[8,8] as integer
TomToad
6
Years of Service
User Offline
Joined: 6th Jan 2018
Location:
Posted: 24th Jul 2018 10:40 Edited at: 24th Jul 2018 10:59
Arrays in AppGameKit are not rectangular. You can think of them as more like array of arrays. When you define blarg as integer[2,2]. you create an array with 3 elements, each element containing an array of 3 elements of integers. When you do blarg.length = 8, now you have an array of 9 elements, the first three are still defined as arrays of 3 integers, the last 6 are not defined with length of -1. When you do blarg[0].length = 8, you now lengthen the first element of blarg to 9 elements of integers, leaving the next 2 at three, and the last 6 still undefined.

Hopefully, this will demonstrate what I'm trying to explain. Run it and you see the length of each element of blarg, press space to increase the size of blarg, press space again to increase the size of the first element of blarg.


Edit: second example same as first, but numbers replaced by grid. Might make things a bit clearer.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Jul 2018 13:15
You will have to resize each element of the first dimension to do what you think it's doing now. It's weird I know, it threw me off the first time too. If you search for my thread about arrays, there was a big discussion about how they work and it might be worth reading. But TomToad said it best, think of them as an array of arrays, with each array able to be a different size.
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
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 24th Jul 2018 13:19 Edited at: 24th Jul 2018 13:20
Quote: "You will have to resize each element of the first dimension to do what you think it's doing now."


Not really ....You can redimension both axis doing this even after the array is created

dim Blarg[8,8] as integer

this can be called AFTER originally setting the array to be 2x2 as I said above. Of course you could resize each member in a loop but theres not much reason to do that unless you want a non rectangular array.
Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 24th Jul 2018 17:14 Edited at: 31st Jul 2018 07:20
Bengismo wrote: "
Blarg[0].length = 8 // Change size of dimension 2 - NO IT DOESNT

xxxxxxxxx
xxx
xxx
x
x
x
x
x
"


Thanks for the clarification, this makes sense now.

I'll go back to using DIM for multi-dimensional arrays.
Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 24th Jul 2018 23:31 Edited at: 25th Jul 2018 00:29
It appears DIM does not work on arrays inside types
Example:


Is there a way to resize a dimensional array dimensionally with the array version 2 stuff?

Suppose I could use a loop, but that is ugly.
Golelorn
8
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 25th Jul 2018 00:39 Edited at: 25th Jul 2018 01:01
You must declare the array as a type.

DIM Blarg as TheType[8,8]

Then you could make the dimension equal to whatever you want.

Blarg[1,1].abc = XXX

Would this work?

Blarg[0,0].abc.length = newvalue
Blarg[0,0].abc[0].length = newvalue

I believe you would have to change all dimensions in the array. You may find using an open ended array and utilizing .insert and .remove better?

https://www.appgamekit.com/documentation/guides/12_array_changes.htm - very good explanation

I'll be honest a multi-dimensional array plus defining an array as a typeis probably way outta my league to be able to explain. I believe you might be overthinking this and confusing yourself.

Code example of open ended array:

Nieb
10
Years of Service
User Offline
Joined: 13th May 2014
Location: Lurking
Posted: 25th Jul 2018 03:52
It's ugly, but it works:
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 25th Jul 2018 09:40 Edited at: 25th Jul 2018 09:45
If its an array inside a type then you have to manually resize all the elements. Ive defined functions like below that you can pass any integer 2D array to and resize it. It works with arrays inside types or outside them.



So with htis function you can just write
Dim2i(blarg.abc,10,10) to redim the array to 10,10 (this works on any 2D integer array)


You dont need to set the array length to -1 before setting a new length for all the dimensions. (UNLESS- YOU WANT the array to be filled with zero's)

Another way is just resize AND fill with zeros is to do the following with 2 lines
DIM b [4,4]
blarg.abc = b


Id agree with the guys above that using insert and remove is a good option if you are slowly adding or removing elements but if you want to initially setup the array quickly then using DIM is the easiest. If its ana rray inside a type then using a simple function works well and is still pretty clean.

Login to post a reply

Server time is: 2024-11-23 18:24:04
Your offset time is: 2024-11-23 18:24:04