Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: LoneCipher on March 09, 2020, 02:17:32 PM

Title: Array List get Index
Post by: LoneCipher 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?
Title: Re: Array List get Index
Post by: djaydino 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 :)
Title: Re: Array List get Index
Post by: LoneCipher 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?
Title: Re: Array List get Index
Post by: LoneCipher on March 09, 2020, 02:38:34 PM
Here is a ss.
Title: Re: Array List get Index
Post by: djaydino 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)
Title: Re: Array List get Index
Post by: djaydino 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) :)
Title: Re: Array List get Index
Post by: LoneCipher 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.
Title: Re: Array List get Index
Post by: LoneCipher on March 09, 2020, 02:52:28 PM
Re-sending first ss.
Title: Re: Array List get Index
Post by: LoneCipher 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.
Title: Re: Array List get Index
Post by: jeanfabre 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
Title: Re: Array List get Index
Post by: LoneCipher 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.
Title: Re: Array List get Index
Post by: LoneCipher 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?
Title: Re: Array List get Index
Post by: Broken Stylus 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.
Title: Re: Array List get Index
Post by: LoneCipher 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.
Title: Re: Array List get Index
Post by: jeanfabre 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
Title: Re: Array List get Index
Post by: Broken Stylus on March 11, 2020, 07:33:50 AM
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)

Right, that's the hash table indeed. Do you think one day this issue could be solved, in theory?
Title: Re: Array List get Index
Post by: jeanfabre on March 13, 2020, 02:20:32 AM
Hi,

 nop :) this is inherent to how .NET framework has designed hashtables, not how ArrayMaker was designed. it's more efficient because it's not taking any memory nor processing to enforce order where it's not needed.


Bye,

 Jean
Title: Re: Array List get Index
Post by: Broken Stylus on March 13, 2020, 07:17:44 AM
Could it be possible to use Unity to control, through a layer (wrapping?) the Hash Table so as to graft some extra (UI) functions on it?
Title: Re: Array List get Index
Post by: jeanfabre on March 13, 2020, 08:16:29 AM
Hi,

 yes, that could be possible, but again, I don't see the point since that's not how hashtables are meant to be used. How many hashtable keys do you have?

if order is important, use two arrayLists, and use the arraylist reference as the columns ID, then you fall back on your feet.

Bye,

 Jean
Title: Re: Array List get Index
Post by: Broken Stylus on March 14, 2020, 12:34:01 PM
If you have many keys and they have a meaning, it makes sense and becomes practical that they can be ordered according to a specific logic (alphabet, groups, type, etc.) without having to move back and forth between this and an Array List.
Not only the order cannot be altered on a case by case basis, like sliding one key two slots up or down like it can be done for Unity Sorting Layers, because you can only edit a key and its value or add or remove one, so tough luck if for organizational purposes, you find that a given key you forgot or want to add would fit better if placed in, say, the twelfth position instead of slot 86... this is already annoying but then comes the unpleasant experience when the order is all flushed once you press Run.
Without knowing the intricacies of .NET, this has always been a sour source of confusion I would gladly do without if possible.
Title: Re: Array List get Index
Post by: jeanfabre on March 17, 2020, 04:39:48 AM
Hi,

Yes, this is something that potentially can make it into a major overall of ArrayMaker interface.

Bye,

 Jean