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 / Define an array with a dynamic size?

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 13th Jun 2018 15:16
Am I just having a stupid moment this morning? Or can I really not calculate the size of an array?

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
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 13th Jun 2018 15:54 Edited at: 13th Jun 2018 20:53
Add a dim and change the period to a comma



That compiles and works

....and technically....you dont need the = in the #constant lines
#CONSTANT TILE_X x
works ok too but also works if you leave the "=" in there
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 13th Jun 2018 18:47
I know about the equals, I was just trying different things. Period was a typo when I copied it in. doh.

I thought we didn't need to use DIM anymore? I just tried your solution and it still gives the same error.

main.agc:39: error: Unexpected token "x", Array dimension must be an integer literal or a constant (line 7 in the example)
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
PSY
Developer
7
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 13th Jun 2018 19:08 Edited at: 13th Jun 2018 19:16
Setting the size of an array using an arithmetic expression or a standard variable is not allowed.

Not allowed:
map as integer[SIZE*3]

Not allowed:
tile as integer = 666
map as integer[tile]


EDIT:
The following code works using DIM:



PSY LABS Games
Coders don't die, they just gosub without return
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 13th Jun 2018 20:54 Edited at: 13th Jun 2018 20:59
This compiles and works

x = getVirtualWidth() / 32
y = getVirtualHeight() / 32

#CONSTANT TILE_X = x
#CONSTANT TILE_Y = y

dim map[TILE_X, TILE_Y] as Tile

As long as the type tile actually exists.....
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 13th Jun 2018 21:03
I don't know why that solution wasn't working for me earlier but it is now.
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
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 18th Jun 2018 14:56
you can redim with .length =
yes Dim was the older syntax.
https://www.appgamekit.com/documentation/guides/12_array_changes.htm
AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : NVIDIA (390.65) GeForce GTX 1050 Ti : Mac mini OS High Sierra (10.13)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Aug 2018 17:08
Thread might be a little old, but I just learned something new related to this.

Since defining constants doesn't actually assign any value to them but merely a reference (or once compiled constants are replaced with whatever is assigned to it), you have to watch out when using a constant in a function if it is pre-calculated. The scope of whatever you use to define the constant has to be accessible from the function as well.

For instance, you could not access "thing" from a function:
foo = 100
#constant thing = foo / 32


But you can if you make "foo" global.
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
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 20th Aug 2018 19:50
Ive always had no problems with using constants in functions
When the compiler comes accross a constant it replaces its value when it was first declared into it

I use constants all the time and they are my preferred choice
The thing is they should be declared at the beginning of the program then all code can use them
if any function tries to use the same variable name such as thing in your example it will give an error
fubar
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Aug 2018 22:43
The problem with constants comes from dynamic or calculated values. If the expression itself isn't global, the function will give an error. So the constant isn't really a value, it acts more like a pointer.
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

Login to post a reply

Server time is: 2024-04-20 10:34:39
Your offset time is: 2024-04-20 10:34:39