playMaker

Author Topic: Get the index of last item  (Read 1030 times)

Mupp

  • Full Member
  • ***
  • Posts: 167
Get the index of last item
« on: January 07, 2021, 07:49:09 PM »
How can I do that?

nuxvomo

  • Playmaker Newbie
  • *
  • Posts: 30
  • Indy based indie producer of Nights of Yore
    • Nights of Yore
Re: Get the index of last item
« Reply #1 on: January 08, 2021, 01:37:51 AM »
Not sure if there's an exact action for this (maybe search the ecosystem for array index and see what you get).

One way to do it would be with Array Get Next and just Loop Event back on it self.
Make sure to set variables for Current Index (and Result if you need the last item as well).
Then after the Finished Event the the Current Index variable would be the Last Index (and the Result variable would be the last item).

https://hutonggames.fogbugz.com/default.asp?W1205

I use this action all of the time.
You can add actions between the Loop Event and coming back to iterate through the indexes and items therein to do whatever you like to each.

Cheers!

Jim
Producer of Nights of Yore, a streamlined tabletop role-playing game for Android and PC
http://nightsofyore.com/

Mupp

  • Full Member
  • ***
  • Posts: 167
Re: Get the index of last item
« Reply #2 on: January 08, 2021, 10:28:21 AM »
That's a very convoluted way to do it but there are no actions that I can find for arraymaker, only regular array actions. I wonder why there are none.
Thanks though.

nuxvomo

  • Playmaker Newbie
  • *
  • Posts: 30
  • Indy based indie producer of Nights of Yore
    • Nights of Yore
Re: Get the index of last item
« Reply #3 on: February 08, 2021, 09:22:55 PM »
Hey, just randomly saw this again and thought about another option.

You could do Count Children then Int Op to subtract 1 and that would be the last index.

I know you're looking for a single action that would do this but I don't think there is one.
I run in to this a lot and I'm not proficient in C# so I end up coming here for advise and/or just finding a way to get my desired result and moving on (usually the latter).
Otherwise, I would advise experimenting with the Custom Action Wizard to make your own.
Lots of people do this, post their creations here on the forum, and they sometimes end up in future updates or on the Ecosystem for everyone to use.
It's kind of a really rad part of this community.
https://hutonggames.fogbugz.com/default.asp?W166

Hope you're project is coming along well :)

Cheers,
Jim
Producer of Nights of Yore, a streamlined tabletop role-playing game for Android and PC
http://nightsofyore.com/

ch1ky3n

  • Full Member
  • ***
  • Posts: 208
Re: Get the index of last item
« Reply #4 on: February 08, 2021, 11:15:13 PM »
Hey, just randomly saw this again and thought about another option.

You could do Count Children then Int Op to subtract 1 and that would be the last index.

I know you're looking for a single action that would do this but I don't think there is one.
I run in to this a lot and I'm not proficient in C# so I end up coming here for advise and/or just finding a way to get my desired result and moving on (usually the latter).
Otherwise, I would advise experimenting with the Custom Action Wizard to make your own.
Lots of people do this, post their creations here on the forum, and they sometimes end up in future updates or on the Ecosystem for everyone to use.
It's kind of a really rad part of this community.
https://hutonggames.fogbugz.com/default.asp?W166

Hope you're project is coming along well :)

Cheers,
Jim

this is exactly how to get the last index, just count the number of entries and -1 then you get your last index

Mupp

  • Full Member
  • ***
  • Posts: 167
Re: Get the index of last item
« Reply #5 on: February 09, 2021, 09:43:50 AM »
That's how I do it too. But if there are empty slots in the list, then this method would not work right?

ch1ky3n

  • Full Member
  • ***
  • Posts: 208
Re: Get the index of last item
« Reply #6 on: February 09, 2021, 10:21:02 AM »
the general rule is you don't want any empty entry on the array, at least you have to mark it or sort it.

but if you must, in array maker actions there is always a parameter called Failure Event.

You could iterate from index 0 and keep adding the index until the failure event trigger.
say on index 15 failure is triggered (no more item)

then it means 0 - 14 are filled, and your current index is 15 = the total entries

good luck



Mupp

  • Full Member
  • ***
  • Posts: 167
Re: Get the index of last item
« Reply #7 on: February 09, 2021, 10:30:34 AM »
Great, thanks.