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 / Remove from type array?

Author
Message
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 23rd Jul 2017 17:18
The following code generates this error: remove is not a valid type. .length is working fine.



I am wanting to start with an array of say 20, and remove 1 one each time until 0 length remains.
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 23rd Jul 2017 17:23
Ah.. Answered my own question

node.open.remove(i)
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 23rd Jul 2017 17:41
when removing nodes from an array in a loop like that only every 2nd item will be remove because the indies are shifted backwards after each irritation, see the below code



a way round this is to index your array backwards

Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 23rd Jul 2017 19:01
Wow. Thanks! I would have never figured that out.
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 23rd Jul 2017 19:58
Also, want to add to increase array size for an array type one will need to do

type.array.length = newlength

as I have not been able to get insert to work for arrays defined as a type.

Thank you again, PTC! I doubt I could have figured that out quickly.
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 23rd Jul 2017 20:13
So what actually happens when you increase the array size like that? Does the player allocate a new array and copy everything over?
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 23rd Jul 2017 20:36 Edited at: 23rd Jul 2017 20:36
It increases your array to new length. I am not sure how it works internally. But it does add another element.

So, array[0] becomes array[1]. You have a new element to store info in. The other elements are still intact.

Here is what I do when I want to add an element to my array:
type.array.length = type.array.length+1
Then I can input the information for the new element.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 23rd Jul 2017 21:16
Quote: "Thank you again, PTC! I doubt I could have figured that out quickly."


no problem, glad to help, its actually quite a common mistake to make, just always remember that if your going to be removing items from the array you are indexing always do reverse iteration.

Quote: "as I have not been able to get insert to work for arrays defined as a type."


when adding to a type array I usually define a temporary type, fill the data and then add to the array, I never had any problems doing it like this



Quote: "So what actually happens when you increase the array size like that?"


using the .length method would simply add an empty array element and you would need to fill the data giving the new index to the array, IMO using .insert() with a temp type is much tidier

Login to post a reply

Server time is: 2024-04-25 14:10:34
Your offset time is: 2024-04-25 14:10:34