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 / Dynamic arrays possible in tier 1?

Author
Message
wargasmic
18
Years of Service
User Offline
Joined: 15th Oct 2006
Location: UK
Posted: 17th Feb 2014 19:55
Are dynamic arrays possible in tier 1? I need to create an array of unknown size and add/push values into it. Is this possible?
Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 17th Feb 2014 20:18
Well, you can do the following:

1. create an an array with 0 size
2. use the first entry (e.g. array[0]) to store it's size
3. re-dim the array when you need it to grow (e.g. dim array[1])
4. add the value to the added segment (array[1])
5. store the new size in the first entry.
Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 17th Feb 2014 20:45
You have made 2 threads. My answer is in the other one.

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 17th Feb 2014 21:42
If it works the same way as DBPro, then it will create a brand new array each time you resize it. It retains the data, but there is an overhead involved behind the scenes in creating and deleting arrays.

I resize my arrays in blocks (e.g add 100 elements at a time). Create 2 variables to retain the array size and the current last element in use. Try to resize your arrays outside of gameplay to prevent any slowdown when it's obvious to the player.

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 17th Feb 2014 23:49
BatVink is spot on. (As usual)

Re-sizing dynamic arrays or strings in any language is costly, and can cause memory fragmentation. Delphi's lists grow automatically in increments of 16 (I think - might be 64). I usually set a sensible size at the creation point - might be 20000 for some user lists. This avoids any memory creation and destruction later.

If you re-dimension an array the following usually happens:

* A new memory allocation is created large enough to hold the new length
* The old contents are copied to the new memory
* With luck, the old memory is freed.

This is costly in processor and resource time, so you're not doing any favours to the program by incrementing arrays in ones!

-- Jim - When is there going to be a release?
The Daddy
15
Years of Service
User Offline
Joined: 13th Jan 2009
Location: Essex
Posted: 23rd Feb 2014 21:23
As Jim states....it is expensive....


Follow his advice! Either create an array larger than you need....using a fixed size and 'manage' the elements.

OR

Be patient and wait for dynamic lists in v2. As <LISTS> are on the road map for V2, your app my not be complete until you retire LOL!

OR

Use AppGameKit pascal....dynamic lists included. YUM YUM!

www.bitmanip.com
All the juicy you could ever dream of!
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 24th Feb 2014 00:21
With list or arrays it's incredibly important when you want to delete things to go from the end of the array or list downwards. If you go from start to finish in a for-loop you will die if more than one thing has to be deleted and further data moved. There's also a terminal condition where the item to be deleted is the last - trying to move data above that is fatal.

If you use Delphi or Free Pascal you don't have to bother with moving data - it's automatic - but you DO have to go downwards in a for-loop, otherwise you will be out of bounds even with one. This catches us all out from time to time. Code which looks fine may be dangerous.

Here's some pseudo-code to demonstrate why:

We have an array or list (a list is a dynamic array of pointers) of 5 elements, currently. We have a simple array of characters:

XXYXY

We want to delete the Ys.

WRONG:

for x := 0 to 4 do
begin
if ThisArray[x] = "Y" then
begin
ThisArray.delete[x]; // assuming you have such a command
// the length of the array is now 3 or 4, not 5
// but the loop counter will carry on, and BAM!
// After the first deletion (element 2) the array length is 4
// so no problem until x = 4, when it's out of bounds
end;
end;

If you do it downwards from (in this case) 5 to zero, nothing can go wrong because your x will always be within the bounds. This applies even to your own managed arrays - it's one of the many traps where perfectly legal source, which the compiler will no complain about, will kill you later!

-- Jim - When is there going to be a release?
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Feb 2014 04:12
Here's a method I use that's sort of a pseudo-expandable array. The size of the array increases by a certain amount once it has filled all available indices. It delete elements by copying the last element over the one you want to delete then decreasing the internal
pointer by 1. So technically the array isn't shrunk at all and can only grow. Depending on what you're doing, this may be a viable method. But this method can not guarantee the order of elements as any deletion can move the data around.




BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 24th Feb 2014 09:56
Just to back up JimHawkins and Phaelex' advice, I too use both of those methods.

With the Pseudo-expandable Array I do the same thing in a slightly different way. I have an extra array of just integer values. The "leftmost" represent the main array elements in use, the "rightmost" are the currently inactive ones. I only need to shift a single integer value in this scenario when deleting an element rather than a potentially large chunk of data in the bigger array.

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 24th Feb 2014 11:00
Absolutely right. Never move data - just move indices or pointers.

-- Jim - When is there going to be a release?

Login to post a reply

Server time is: 2024-11-25 03:06:24
Your offset time is: 2024-11-25 03:06:24