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 / Array find - index issue in AGK studio 2022.06.27

Author
Message
yoda333
11
Years of Service
User Offline
Joined: 3rd Apr 2012
Location:
Posted: 5th Jul 2022 10:55 Edited at: 5th Jul 2022 12:55
Hi

Can any of the forum members explain why the number 19 shows an index of -1 instead of 1 in this example please. The code is very similar to the example in the Guide but uses integers instead of strings.



Thanks
yoda333
Bored of the Rings
User Banned
Posted: 5th Jul 2022 11:15 Edited at: 5th Jul 2022 11:19
all odd, never used .find before in AppGameKit but at a guess this probably works with string params and not integer, this code worked but only when I changed the array index to 10, I changed the array to a string instead of integer, not tried back on integer again yet:



bug by the sounds of it.
Pro Programmer / Data Scientist, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 5th Jul 2022 12:37 Edited at: 5th Jul 2022 12:41
names as integer [ 5] should be names as integer [ 6]

Actually forget that, I just tried it.
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 5th Jul 2022 13:04 Edited at: 5th Jul 2022 13:05
Definitely a bug, it seems to only give the address of every other find, even without swapping.

yoda333
11
Years of Service
User Offline
Joined: 3rd Apr 2012
Location:
Posted: 5th Jul 2022 13:04 Edited at: 5th Jul 2022 13:08
Hi

Thanks for the replies.



I've made a change to the code in lines 8 and 9 of the original source but the code should still return an index of 1 as 19 is at index1 after the swap



(Displays Index -1)

if I change



The correct index is returned
2
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 5th Jul 2022 13:07
Read my other comment mate, there's a bug.
yoda333
11
Years of Service
User Offline
Joined: 3rd Apr 2012
Location:
Posted: 5th Jul 2022 13:10
Hi Steve

I've read your reply. I thought it was my error but it still fails with the corrections.



Thanks for your response.
Steve Ancell
18
Years of Service
User Offline
Joined: 16th Feb 2006
Location: Brighton, East Sussex, UK
Posted: 5th Jul 2022 13:36 Edited at: 5th Jul 2022 13:38
It gets weirder, if you sort the array first it will show where every number is.



And if you then do the swap it all spazzes out again.



[edit] If I remember right it does the same with string arrays, I found I had to sort them before searching them. [/edit]
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 5th Jul 2022 15:47 Edited at: 5th Jul 2022 15:48
Quote: " I found I had to sort them"

always, if you intend to use .find. it's how we get the speed boost when compared to manually searching each index.

either .sort before .find or after .swap, or .insertsorted when adding elements (where previous elements were .insertsorted or .sort was performed).
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Jul 2022 17:54
So that tells me find() relies on a binary search. If this isn't in the documentation that it must be sorted, it really needs to be added.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

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

Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 5th Jul 2022 19:26 Edited at: 5th Jul 2022 19:28
Quote: " If this isn't in the documentation that it must be sorted, it really needs to be added."

it is (see Sorting and Searching about 1/4 of the way down), but i believe the exception is if all entries were inserted or already defined in ascending order.

meanwhile, note: .Sort/.InsertSorted = Case-Sensitive #927

all of this being said, i use .find in just about any sizable project and, knowing the above, have had no issues while relying on .find functionality.
adambiser
AGK Developer
8
Years of Service
User Offline
Joined: 16th Sep 2015
Location: US
Posted: 5th Jul 2022 19:42
find is for sorted data.
indexof is for any data.

indexof needs to be added to the docs.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 5th Jul 2022 20:59 Edited at: 5th Jul 2022 21:00
Quote: "indexof needs to be added to the docs"

yes

meanwhile, is anyone actually using .IndexOf() given THIS?

ie, as it stands, aren't we better off writing our own .IndexOf() (with the aforementioned Start Index parameter if desired) as we did before .IndexOf was hurried into existence?
adambiser
AGK Developer
8
Years of Service
User Offline
Joined: 16th Sep 2015
Location: US
Posted: 6th Jul 2022 01:03
@Virtual Nomad: My issue for .IndexOf is when using user types. An integer or string array works fine. But yeah, I did shun away from it because of the issue.
yoda333
11
Years of Service
User Offline
Joined: 3rd Apr 2012
Location:
Posted: 6th Jul 2022 03:06 Edited at: 6th Jul 2022 03:08
Hello to the forum members.

I do not want to perform a sort as the data in the array elements are sprite numbers for a shuffled deck of cards.

If it's a bug then I will follow AppGameKit bug reporting guidelines, if not - is indexof the way to go?


Please advise
Thanks yoda333
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 6th Jul 2022 05:15 Edited at: 6th Jul 2022 05:46
it's not a bug.

meanwhile, i cobbled THIS together from another project. it might help.

[Click] to Shuffle, hover to reveal the card's name.

.find works without sorting using the exception mentioned above.

and, i'm assuming you're using a traditional deck of playing cards. regardless, if you're using a sprite with frames as i am, another way to go about it is to simply define each frame (pretty much what i'm doing) in an array and just reference frames vs sprite IDs via GetSpriteCurrentFrame().

some of the stuff like sprite animations, etc, carry value beyond the obvious - we can use various properties to do different things kinda like pseudo-types.

ACK! i didn't make it obvious but Shuffle() fills CurrentDeck[] which is the random order of the cards. ie, add:

above Sync() to see the first 13 cards (Sprite IDs) in the newly-shuffled deck. Cards[] remains untouched where it's the reference/list for look-up by Sprite ID (ThisCard).

Attachments

Login to view attachments
yoda333
11
Years of Service
User Offline
Joined: 3rd Apr 2012
Location:
Posted: 6th Jul 2022 08:38
Thanks Virtual Nomad and others that have responded to my problem.

I have enough to get back on track.

All the best
yoda333

Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 2nd Sep 2022 16:39 Edited at: 2nd Sep 2022 16:52
The issue seems to be how the .Find( ) function works.
That is using Integer or Floating Points and using .Find( ) on an Unsorted Array., it will fail (-1 or "Could not Find").
Where-as if you use .Sort( ) first then it will work.

This will also work IF you manually sort:



This said as "Find" ONLY works on the Root Type Element., it tends to be a better solution to use a Custom Find for what you want to look for.
The main downside is that without templates... we can't do this as a Single Function for Any Array; but instead must have a custom find for each array type we expect.

Login to post a reply

Server time is: 2024-03-29 12:36:29
Your offset time is: 2024-03-29 12:36:29