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 / Array Insert() Help

Author
Message
Faltor
8
Years of Service
User Offline
Joined: 20th Jul 2015
Location:
Posted: 20th Jul 2015 11:39
So I'm trying to insert an element into an empty array using the insert() function. I am doing this in order to create an array from scratch at runtime, however when I do this the array is created as (for example): {1, 2, 3, 4} and has a length of 3. I understand this is due to the fact that agk includes an extra element at the [length] index. This is confusing in that an array with 4 elements in it has shows a length of 3.
Now if I start with an array with a length of 1 there is an empty element at the start (for example): {0, 1, 2, 3, 4} and shows a length of 4. Still confusing.
Is there a way in which I can insert() into and empty array and keep the correct length?

Thanks for your help

-Faltor
theUsual
8
Years of Service
User Offline
Joined: 13th Jul 2015
Location: Norway
Posted: 20th Jul 2015 15:52 Edited at: 20th Jul 2015 15:52
You can assign values to the elements manually.


To avoid confusing regard the .length command, do this when getting the length:
ar.length + 1

Array length:
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 20th Jul 2015 15:53 Edited at: 20th Jul 2015 15:56
Hi Faltor,

its so you have an element at Zero.
Array.length gives you the real length + 1.
The "+1" is the element at Zero, not at the [length] like you said.

Array: 1,2,3,4
Index: 0,1,2,3

[Edit]@theUsual aw you beat me in one minute[/Edit]


Using AGKv2 Tier1
Faltor
8
Years of Service
User Offline
Joined: 20th Jul 2015
Location:
Posted: 20th Jul 2015 18:38
Thanks for the quick responses guys. I get it now. I assumed you were supposed to leave the last element blank in order to have a "correct" length attribute. Instead you just plan for the extra element.

-Faltor

Login to post a reply

Server time is: 2024-03-28 23:56:14
Your offset time is: 2024-03-28 23:56:14