playMaker

Author Topic: Array List get Index  (Read 4010 times)

LoneCipher

  • Playmaker Newbie
  • *
  • Posts: 29
Array List get Index
« on: March 09, 2020, 02:17:32 PM »
Hello all,

I am having issues with getting the index of gameobject items in an array list.
Every action that stores an index either does not update the value, or sets the value to 0.
I have no issue counting the array, adding to the array, or removing from the array from any FSM in my project. Array interactions complete smoothly. I just cannot get the index of items in the array.

States that I have attempted to use, to get the index of an item:
Array List Index Of
Array List Contains Game Object
Array List Get Next
Array List Last Index Of

Array List Add is capable of returning the index.

I am setting an int variable with a starting value of 0, and assigning that to the Result > Result Index field. Is there another method of getting the index value of a gameobject in an array list?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Array List get Index
« Reply #1 on: March 09, 2020, 02:31:46 PM »
Hi.
Have you tried 'Array List Contains'

also make sure that its the exact object, not a prefab or clone.

Maybe show some images or video with your setup :)

LoneCipher

  • Playmaker Newbie
  • *
  • Posts: 29
Re: Array List get Index
« Reply #2 on: March 09, 2020, 02:34:44 PM »
I have used Array List Contains.

I am creating a gameobject from a prefab, then storing that object into the array.
Is this method of working with prefabs supported?

LoneCipher

  • Playmaker Newbie
  • *
  • Posts: 29
Re: Array List get Index
« Reply #3 on: March 09, 2020, 02:38:34 PM »
Here is a ss.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Array List get Index
« Reply #4 on: March 09, 2020, 02:43:51 PM »
Hi.
creating prefabs is fine and storing them as well.

But if you use the prefab itself to look for the object, that wont work.
Once something is created its unique, event it holds the same name.

With 'Array List Contains Game Object' you should be able to find it by name, but make sure that the name is correct (including (clone) as when you create @ runtime its automatically added to the name)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Array List get Index
« Reply #5 on: March 09, 2020, 02:45:38 PM »
Hi.
Hmm, on your images i can see that game object name is empty.
so its looking for a game object named "" (nothing) :)

LoneCipher

  • Playmaker Newbie
  • *
  • Posts: 29
Re: Array List get Index
« Reply #6 on: March 09, 2020, 02:50:41 PM »
In my testing I figured I could find the object by tag instead of name (which is working). I added the name field back, and set a breakpoint after checking for the gameobject.
The object is present, but the index is not.

LoneCipher

  • Playmaker Newbie
  • *
  • Posts: 29
Re: Array List get Index
« Reply #7 on: March 09, 2020, 02:52:28 PM »
Re-sending first ss.

LoneCipher

  • Playmaker Newbie
  • *
  • Posts: 29
Re: Array List get Index
« Reply #8 on: March 09, 2020, 07:10:00 PM »
I updated ArrayMaker for Unity5 from the ecosystem to be sure I'm on the latest.
I re-tested, and the 'Array List Contains Game Object' action is still only returning the gameobject, but not the index number.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Array List get Index
« Reply #9 on: March 10, 2020, 03:00:20 AM »
Hi,

 It works here, odd.

 can you isolate your issue on an empty scene with just an fsm and this action?

Bye,

 Jean

LoneCipher

  • Playmaker Newbie
  • *
  • Posts: 29
Re: Array List get Index
« Reply #10 on: March 10, 2020, 08:36:11 AM »
The action works in a new scene. Also, the action works on a new FSM in my existing scene.
I have a habit of selecting states from one FSM and copy / pasting them into another FSM. Occasionally, I need to re-create an FSM to resolve an odd bug.
Is it a supported action to copy and paste states across different FSM's?

This bug affects multiple FSM's in my scene, but they may have been copy / paste buddies. I'll work on re-creating a couple to see if the action starts working for me.

LoneCipher

  • Playmaker Newbie
  • *
  • Posts: 29
Re: Array List get Index
« Reply #11 on: March 10, 2020, 11:05:07 AM »
I was able to reproduce my issue in a new scene. I am looping the 'Array List Contains Game Object' and an 'Array List Remove'. Each test will return the index of 0, but I will watch the array remove the index and object from index 2.

I figured the remove action was removing the game object from index value 2 and erroneously stored index value 0.

If I add a filler game object to index 0, my index values will remain at 1 through each loop.

I was expecting the array to have 'holes' when removing an item from the middle.
This may be a misunderstanding on my part, on how arrays work. When an item is removed from the array; will the array re-organize, or will the array allow an index to be empty?

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: Array List get Index
« Reply #12 on: March 10, 2020, 11:42:04 AM »
I updated ArrayMaker for Unity5 from the ecosystem to be sure I'm on the latest.
I re-tested, and the 'Array List Contains Game Object' action is still only returning the gameobject, but not the index number.

If it returns 0 every time, maybe the game object you always find is the one assigned to index 0? Take a look at your Array List too.

Besides at runtime, I remember spotting that the spots could all get shuffled in the Array List.

LoneCipher

  • Playmaker Newbie
  • *
  • Posts: 29
Re: Array List get Index
« Reply #13 on: March 10, 2020, 11:52:24 AM »
I'm thinking I always returned the object at Index 0, because that is the first index searched for an object. After removing the object and searching again, it will find the object at 0, since the array collapsed back to 0.

I ran another test and removed an item from the middle of an array, and confirmed the array collapses, and does not leave empty 'holes'.

Items stored in an array

Index  --- object
0 --- red
1 --- green
2 --- blue

Removing green leaves the array like this:
0 --- red
1 --- blue

Meaning, the 'Array Contains Game Object' action searches Index 0 first, and the array will always store values at the lowest numbers.
This is likely working as intended, and I just need to expect this functionality.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Array List get Index
« Reply #14 on: March 11, 2020, 02:28:08 AM »
Besides at runtime, I remember spotting that the spots could all get shuffled in the Array List.

Hi,

 no, the ArrayList stays consistent between edit and runtime, it's the hasbtable that can be shuffle but it doesn't matter because you are using the key to access a value. Unfortunatly, that's a per design issue of the hashtable itself ( it doesn't enforce order on serialisation/deserialization)

Bye,

 Jean