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 / Redefining arrays?

Author
Message
Paronamixxe
AGK Bronze Backer
12
Years of Service
User Offline
Joined: 10th Feb 2012
Location: Sweden
Posted: 4th Apr 2014 21:10
Hi!
I have made a function that will automatically load a level for my game using information from files and such... I need to use a lot of arrays to store a lot of information. However, when I have completed the level and want to load a new one I ofcourse need to redefine the information in the arrays... and sometimes the size. This would work fine as it is... If you restart the game.

So, after you have completed the level and are returning to the main menu, I delete all the sprites that where created for the level. And the next time I try to launch a level I have made sure that all the Type's aren't being called again (thought that this would cause a lot of errors)... However, I need to redefine the arrays according to their new size? At the moment everything works fine until I try to construct the second level I want to play. Then I get the well-known: "subscript out of bounds at line 2198 in "null"" which is telling me that my arrays are receiving more information than they were defined to receive.

How can I redefine the arrays? or is there a simpler solution?

Some code that might be useful:

Just before the Do loop is run:

In the "levelbuilder":
(example of my arrays.... all are set up like this)
Just before you exit the "win screen" to go back to the menu (to do another level):


I seem to remember that you could find the complete code in one temporary file somewhere in the bowels of the programs folders, so I could check up the exact line which was causing the error. However, I've forgotten where it is... And it might be DBPro I'm thinking about...

cheers

nope.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 4th Apr 2014 21:30 Edited at: 4th Apr 2014 21:31
i believe the type is a fix struct after compile.
if then around make no sense.
redim u can use dim with other number.

you can make a clean function that get the index
then you set all at 0 0.0 or ""



AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
Paronamixxe
AGK Bronze Backer
12
Years of Service
User Offline
Joined: 10th Feb 2012
Location: Sweden
Posted: 4th Apr 2014 21:37
Problem is that Entity, for example, has a different amount of variables depending on which level is loaded...
For example: In level one there could be 2 enemies:

And in level two I have 5:


Not really sure what you mean by
Quote: "i believe the type is a fix struct after compile.
if then around make no sense.
redim u can use dim with other number."


If just using the Dim command again with the same array name but another value works... Why am I getting the subscript out of bounds error? :S

nope.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 4th Apr 2014 22:48 Edited at: 4th Apr 2014 22:50
maybe u use older version of agk?

i mean this. a type did not changed at runtime.




AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
Paronamixxe
AGK Bronze Backer
12
Years of Service
User Offline
Joined: 10th Feb 2012
Location: Sweden
Posted: 4th Apr 2014 23:06
Yes I know, I added the run variable so that it wouldn't run twice? And I'm updating AppGameKit as we speak....
Just updated, same error :/ I'll have to have a look in my code again

nope.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Apr 2014 23:41
You can just use DIM to redefine the array. If make it larger, it will keep the current data intact.

Quote: "Why am I getting the subscript out of bounds error?"

Since there are no array size commands, you keep track of this manually. Maybe you have other variables you're not resetting after changing the array?

Paronamixxe
AGK Bronze Backer
12
Years of Service
User Offline
Joined: 10th Feb 2012
Location: Sweden
Posted: 4th Apr 2014 23:48
Yes!

Ok, I've sorted the error anyways and everything works like a charm! However, I was wrong when I thought that the "subscript is out of bounds" error was about my arrays in the levelbuilder... It was actually arrays in a separate "Tween" file that were causing the error because I was calling the functions inappropriately.

Problem solved!

Thanks guys!

nope.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 5th Apr 2014 10:48 Edited at: 5th Apr 2014 10:51
ok,
the error message should show the file name and row.

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
Paronamixxe
AGK Bronze Backer
12
Years of Service
User Offline
Joined: 10th Feb 2012
Location: Sweden
Posted: 5th Apr 2014 13:04
I know, but initially I was just getting an unrealistic line number in the "null" file? XD But when I updated AppGameKit, it told me that the error was being called in my "Tween" file! And thats how I managed to fix it in the end!

Thanks!

nope.

Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 5th Apr 2014 19:05
AGK has a lot of difficulty with subscripts and arrays of user defined types.
I had to make it a practice to use a variable in the 0 element to store my array sizes. Like so:
type myType
aSize as integer
someVar as integer
endtype

dim myArray[100] as myType
myArray[0].aSize = 100

Then do this any time you resize the array and use that array element in loops and such so you are within the array subscript bounds.
If you have mistakes that carry you out of bounds on your UDT arrays it is very common that AppGameKit will not catch them and continue running in without error in windows. On iOS and Android it will crash with no error message. Which makes it horrible to trace and makes it doubly important to keep track of the array bounds on your own.

That said, how are you getting AppGameKit to define a type in an if statement? I had no idea it was possible and I don't think it is good practice. Type, global, constant declarations should all be at the top of your program and the first thing you do! I also make it a habit to initialize all of the arrays I need at the beginning of my program and set their size counter to 0 so that I can easily call them later without being concerned that the have yet to be declared. Saves a ton of work because I can have arrays for set of sprite objects and I can say something like this:
for i = 1 to myArray[0].aSize
if GetSpriteExists(myArray[i].spriteID) = 1
inc count
endif
next i

if count = myArray[0].aSize
// the object was already created!
else
// the object needs to be initialized and created, but we should ensure that it is cleaned up first!
endif

This provides a bit of an out for mistakes, say you accidentally deleted one of the sprites in the object, but just hid all of the others. This type of method will clean up and reinitialize everything so you're back in working order. Also it should be noted that AppGameKit has a very small possibility of accessing non-null memory when using UDT arrays. This means every once and a while in a large program you will get some junk values in your arrays. So when you initialize your array with dim or resize it you should make it a practice to set all of the type elements to zeroes or nulls (the ones which don't contain data). This will save you from mysterious behavior later down the road, trust me!

I do really wish AppGameKit could pass arrays by reference to functions. This would allow us to have a bunch of really awesome error checking methods and array functions like sorting, inserting, removing duplicates, checking out of bounds, etc. Instead we have to write them for each and every different array

Paronamixxe
AGK Bronze Backer
12
Years of Service
User Offline
Joined: 10th Feb 2012
Location: Sweden
Posted: 6th Apr 2014 21:02
Thanks for the in-depth response!

I will keep that in mind! Probably need to do some cleaning up of my code... All in good time!

Cheers!

nope.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 6th Apr 2014 22:49
Just to stir the pot, if you worked in Tier 2 you could use std::list and other nice, dynamic objects for handling stuff with well known garbage collection and memory management.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master

Login to post a reply

Server time is: 2024-11-25 03:30:49
Your offset time is: 2024-11-25 03:30:49