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 / undim vs local scope

Author
Message
Getafix
7
Years of Service
User Offline
Joined: 14th Apr 2017
Location:
Posted: 2nd May 2017 08:52
Hi,

If I create an array in a function, and it goes out of scope, does it get cleared up as though I called undim? Stated the other way, is there any reason or advantage to dim/undim an array that's local to a function?

Thanks
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 2nd May 2017 09:31 Edited at: 2nd May 2017 09:36
there exit an old and new style.
dim has used always global scope also if you define it inside a function.
see new syntax and use local before array name
https://www.appgamekit.com/documentation/guides/12_array_changes.htm

AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
GameDevGuy
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location:
Posted: 2nd May 2017 14:02 Edited at: 2nd May 2017 14:03
I would confirm that the new array syntax is the way to go. Not only can they be declared as local, and are automatically disposed of when the function exits.

I used to think that surely fixed dimension arrays would be more efficient or faster. But after doing many tests the old style arrays do not seem any faster than the new.

So unless you have a lot of existing code that uses dim, that doesn't need rewriting for some other reason, I can't think of a good reason to not use the new dynamic arrays.

Note, while the version 2 arrays "completely clean themselves up" when declared locally, this only works for arrays of basic types like number and strings.

As far as I can tell, version 2 arrays are not smart enough to dispose of User Defined Types, if the array elements come from outside of the function.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd May 2017 09:39
@GameDevGuy
Quote: "As far as I can tell, version 2 arrays are not smart enough to dispose of User Defined Types, if the array elements come from outside of the function."

so far as I know
assigning an User Defined Types with = do an copy/paste.
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 3rd May 2017 11:53 Edited at: 3rd May 2017 11:54
Quote: "As far as I can tell, version 2 arrays are not smart enough to dispose of User Defined Types, if the array elements come from outside of the function."


Hmm - I got all my user types defined on their own tab/file and then some of them are instanced as arrays inside functions and passed around as needed. Haven't noticed any problems. Though I should perhaps do some close monitoring of memory usage and have a look to see if this leads to memory leaks.

Though one thing that is a bit annoying - if i reset an array manually, using myArray.length = 0 - one also need to call myArray.remove(0) to totally flush it. Unlike a certain other language I use where it is as simple as myArray = nil
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd May 2017 12:18
Quote: "- if i reset an array manually, using myArray.length = 0 - one also need to call myArray.remove(0) "

"Therefore an array of length 0 has one element and an empty array will return the length "-1"."
= -1 will make an empty array
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 3rd May 2017 16:01
aaaah, I see. How obvious - all one need do is RTFM

Thanks
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd May 2017 16:18
Quote: "RTFM "

i would also expect 0 is empty and 1+ are elements
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)

Login to post a reply

Server time is: 2024-11-24 16:53:23
Your offset time is: 2024-11-24 16:53:23