Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: krmko on May 22, 2019, 06:34:58 AM

Title: Index was out of range when adding an item to arraylist[SOLVED]
Post by: krmko on May 22, 2019, 06:34:58 AM
Hi guys,

i have an identical FSM in all of my enemy prefabs that adds a number (int) to an arraylist when the enemy enters the screen, and removes an item when the enemy goes out of the screen. It works flawlessly except on one prefab that reports an "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" error.

I don't get it, it's a completely same action (array list add) with completely same parameters on all objects.

Any ideas?
Title: Re: Index was out of range when adding an item to arraylist
Post by: djaydino on May 22, 2019, 06:58:37 AM
Hi,
Can you show the fsm/state/actions
Title: Re: Index was out of range when adding an item to arraylist
Post by: krmko on May 22, 2019, 07:06:06 AM
Sure

(https://i.postimg.cc/Xv8sJCrf/screenshot-289.png)

That's it, it's completely same everywhere. Here's the array.

(https://i.postimg.cc/tgxVvQjY/screenshot-290.png)
Title: Re: Index was out of range when adding an item to arraylist
Post by: djaydino on May 22, 2019, 10:30:32 PM
Hi.
I mean the flow of the fsm/states.

And also how are you removing, as you mentioned that you are removing them.
Title: Re: Index was out of range when adding an item to arraylist
Post by: krmko on May 22, 2019, 11:14:24 PM
I don't think flow is of much importance here, it's in the start state.

(https://i.postimg.cc/ZK1sVq1v/screenshot-291.png)

I replaced the arraylist add action with array list add range and it works now, maybe it should be inspected to check for bugs.

For removing from the array list i use array list remove at end remove the item from index 0, as simple as that, no mistake there.

Edit: nah, there's still errors.
Title: Re: Index was out of range when adding an item to arraylist
Post by: krmko on May 24, 2019, 12:29:47 AM
Strange thing happened, now i'm getting the same warning instead of error.
Title: Re: Index was out of range when adding an item to arraylist
Post by: djaydino on May 24, 2019, 01:22:46 AM
Hi.
How many enemies are going on and off screen at the same time?

What purpose does the int have?
Title: Re: Index was out of range when adding an item to arraylist
Post by: krmko on May 24, 2019, 01:27:47 AM
It's just there to fill the array so i can get the count. I reckon an int takes the least processing of all data types.

No enemies get in the screen until the array is empty (enemies are either dead or left the screen), so it's quite fail safe in that regard.

There are instances when 10-20 enemies appear at the same time and are to be added to the array, but the problem happens regardless of enemy number.
Title: Re: Index was out of range when adding an item to arraylist
Post by: djaydino on May 24, 2019, 03:52:13 AM
Hi.
Is there some state between the 'Array List Add' and the "Array List Remove" that has a delay? (wait or next frame event)

Also did you try the same with a int and use 'Fsm Int Add' (add -1 for remove)
Title: Re: Index was out of range when adding an item to arraylist
Post by: krmko on May 24, 2019, 04:20:28 AM
No delays.

I haven't tried, but i figured out more elegant solution. I simply added a disabled tag child to the enemy, when it enters the screen, the tag activates, when it exits or the enemy is destroyed, the tag holding object is deactivated (or destroyed) and the spawner is checking the tag count instead of counting the items in the array. When it's zero, it will spawn another wave.

Unfortunately, get tag count uses find objects so it's not quite good performance-wise, but it works.

Nevertheless, i'm really curious why doesn't this work properly since i'll probably need it again sooner or later and i can't seem to find the way to debug it.
Title: Re: Index was out of range when adding an item to arraylist
Post by: djaydino on May 24, 2019, 04:34:15 AM
Hi.
Try having a next frame in between.
Title: Re: Index was out of range when adding an item to arraylist
Post by: krmko on May 25, 2019, 07:06:23 AM
Ok, i'll try it out.
Title: Re: Index was out of range when adding an item to arraylist
Post by: jeanfabre on May 29, 2019, 11:46:33 PM
Hi,

 Did this got sorted out?

 Bye,

 Jean
Title: Re: Index was out of range when adding an item to arraylist
Post by: krmko on May 30, 2019, 12:14:30 AM
I sorted it out by changing the system altogether (by using tag count), since i'm knee deep and i can't afford to switch to array again to try putting in the next frame for test sake. I'll be probably using more array lists in the future so i'll keep this in my sights.