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 Studio Chat / Sorting arrays and types

Author
Message
Richard Stevens
4
Years of Service
User Offline
Joined: 14th Jan 2020
Location:
Posted: 15th Apr 2024 17:21 Edited at: 15th Apr 2024 17:33
I recently discovered the sort command.. which would probably be far more efficient than my horrible code if I could get it to work for me in other than a very simple array.

If instead i have a type that looks something like this:



so it might look like:

Quantity:4 //number of beasties in target scope
beastyno[4,6,2,13] // filled with my beasty reference numbers
distancetoexit[13,40,20,11} // filled with their respective distance to exit.

If I then wanted to sort beastyno on distancetoexit I'd get something like:

Quantity:4
distancetoexit [11,13,20,40] < sort this smallest first
beastyno[13,4,2,6] < and rearrange these to match

and then get my answer by querying

target=targets.beastyno[1]

is that possible? or am I being lazy? I do currently have something that works, just wondering if I'm missing a simple trick.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th Apr 2024 19:16
Have you tried:

targets.beastyno.sort()
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda
Pixel-Perfect Collision

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 17th Apr 2024 07:03

Robbert Prins
User Offline
Joined: 18th Apr 2024
Location: Netherlands
Posted: 18th Apr 2024 21:10
Perhaps sort () could play a role here, but in this particular case where you only want to know the closest target, an easier solution may be to loop through the distances and keep track of the smallest value and the index corresponding to that value.

Then after the loop, use the stored index to get the number of the corresponding beasty from the beasties array.

Robbert Prins
User Offline
Joined: 18th Apr 2024
Location: Netherlands
Posted: 18th Apr 2024 21:34 Edited at: 18th Apr 2024 22:14
If you do want to use sort() I think it would be tricky to sort the distance array, and then apply the same sort order to the number array.

Instead you may want to create a Beast type that contains both a beast's distance and its number. The distance should be the first field of the type. Then create an array of beasts, and apply sort() to that array. This will sort the beasts according to their distance because sort() will sort on the first field of the type.

This is the same as the second code example under "Sorting and Searching" on this page:

https://www.appgamekit.com/documentation-studio/guides/12_array_changes.htm

Login to post a reply

Server time is: 2024-05-04 10:45:22
Your offset time is: 2024-05-04 10:45:22