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 / Removing a type from an array

Author
Message
PeterPotamus
7
Years of Service
User Offline
Joined: 19th Apr 2017
Location: The Netherlands
Posted: 20th Jul 2017 14:32
Hi everyone,

Let's say I have an empty array called "UnitList"
Now, when I want to add one unit, I simply insert this Unit into the UnitList.
(The Unit is a custom type btw)

So.... when I am done with a specific unit, I can remove it from the UnitList.

Now here is the question:
When you remove something from an array, does AGK2 delete only the pointer, or does it erase the pointer AND the type?

To clarify:
In Blitz3D, there is a command called "New". This will create a new custom type.
After that, you can use the "For ... Each" loop to search through all the instances of the custom type.
And finally, use "Delete [pointer]" to remove it.

Supertino
7
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 20th Jul 2017 16:14 Edited at: 20th Jul 2017 16:15
not an answer but I too was wondering this, well, how garbage collection in AppGameKit works in general.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 20th Jul 2017 16:55
using either .remove() or .length=-1 does seem to clean up garbage, using the below test code and monitoring Task Manager you can see the memory usage reduces

Dybing
13
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 20th Jul 2017 17:09 Edited at: 20th Jul 2017 17:12
You declare the array as the type. For instance:



The type is not affected by where and how it is used, and by itself do not store anything. It is just a template to hold and access data in variables and arrays of that type.

So say you want to use the type above and populate it with data stored in a file, you'd do as so:



The file it reads looks like so:



...and is named "wordList.csv" and stored in the "media" folder of the AppGameKit project.

If wanting to remove a word later on, just do it with words.remove(index) and that element is removed for good. Just be careful using hard values in combination with dynamic arrays. An out of bounds will crash your program.

If wanting to zero out the array, use words.length = -1

That will delete all entries of the array, so you can load it up with values from say another file or whatever.
PeterPotamus
7
Years of Service
User Offline
Joined: 19th Apr 2017
Location: The Netherlands
Posted: 25th Jul 2017 10:39
Hi everyone,

Thanks for replying in general

@Supertino: yes, the "garbage-collection" is exactly what I was thinking about

@PartTimeCoder and Dybing: Many thanks for providing me with a number of examples. You're the greatest
I've tried the code in combination with the TaskManager as you suggested. There is next to nothing in terms of overhead/increased memory usage.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 25th Jul 2017 12:17
Quote: "There is next to nothing in terms of overhead/increased memory usage."


keep adding array items, hold space for a few minuets and build 100,000+ items, memory usage doubles (on my system anyway), clearing the array halfs it again
PeterPotamus
7
Years of Service
User Offline
Joined: 19th Apr 2017
Location: The Netherlands
Posted: 25th Jul 2017 13:26
Quote: "keep adding array items, hold space for a few minuets and build 100,000+ items, memory usage doubles (on my system anyway), clearing the array halfs it again
"


Quite right.

I was only testing within the parameters of my tower defense game, where all the enemy-waves are triggered in time and/or by intervention of the player.
However, I can always try to figure out how AGK2 handles excess items.

Stay tuned for more.

Login to post a reply

Server time is: 2024-11-22 07:24:58
Your offset time is: 2024-11-22 07:24:58