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 / [BUG][2.0.17] insert(0) operation on an empty array

Author
Message
Eqqman
9
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 4th Mar 2016 06:25 Edited at: 5th Mar 2016 06:35
The insert(value, 0) operation will fail if done on an empty array. Since adding the element to the empty array would in fact place it as element 0, it might be expected that the operation will succeed.
Goo Goo G\'Joob!
Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 4th Mar 2016 09:53
Remember to always post a working code snippet to show your problem.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 4th Mar 2016 22:05
That is the correct functionality. The item is added at array element zero.
Eqqman
9
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 5th Mar 2016 06:34


This crashes for me AppGameKit 2.0.17.
Goo Goo G\'Joob!
Digital Awakening
AGK Developer
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 5th Mar 2016 10:14
Ah, you are trying to insert the value 5 at index 0 but index 0 does not exist. This is the same as using an index larger than the size of the array. You are basically out of bounds.

If you do this then the value will be added at the end of the existing array.

Eqqman
9
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 5th Mar 2016 18:29
Quote: "Ah, you are trying to insert the value 5 at index 0 but index 0 does not exist. This is the same as using an index larger than the size of the array. You are basically out of bounds."

Yes, I know why this is failing. As I point out, since the array is empty, an insertion at either the front or the rear of the array will locate the new element at index zero, so I would not expect insert(value, 0) to fail on an empty array as the end result is exactly what I ask for- the new element is index 0. I cannot use insert(value) in my code as all new elements need to go into the start of the array and insert(value) places elements at the end according to the AppGameKit online help. Also, when my code is called I cannot make assumptions about the length of the array; it can be either empty or not empty. As a workaround I was forced to do this:

Goo Goo G\'Joob!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Mar 2016 19:31
It is correctly failing. You can't reference an element that doesn't exist. It is just the same as trying:

array.insert(value, 999)

The reference problem is still the same.
Your workaround is a good way to solve your problem.
If you want more efficient code then you could add a dummy element at 0, and ignore it. Then your original approach would work.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Yodaman Jer
User Banned
Posted: 6th Mar 2016 17:06
@Eqqman:

This is how lists/expandable arrays have always worked, at least in TGC's tools (as far back as I can remember anyway!). Your workaround is great, though!

Back in the DBP days I seem to remember that you could choose to add a new element at the "top" or "bottom" of the array - perhaps Paul could find a way to implement similar functionality in AppGameKit, and then this would easily solve your problems.

Slowly compiling code, one byte at a time.
Follow meh blague for more zany thoughts and possibly offensive programming!

Login to post a reply

Server time is: 2024-09-29 11:32:31
Your offset time is: 2024-09-29 11:32:31