playMaker

Author Topic: Index was out of range when adding an item to arraylist[SOLVED]  (Read 855 times)

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
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?
« Last Edit: May 31, 2019, 06:53:38 AM by jeanfabre »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4582
    • View Profile
    • jinxtergames
Re: Index was out of range when adding an item to arraylist
« Reply #1 on: May 22, 2019, 06:58:37 AM »
Hi,
Can you show the fsm/state/actions

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Index was out of range when adding an item to arraylist
« Reply #2 on: May 22, 2019, 07:06:06 AM »
Sure



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


djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4582
    • View Profile
    • jinxtergames
Re: Index was out of range when adding an item to arraylist
« Reply #3 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.

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Index was out of range when adding an item to arraylist
« Reply #4 on: May 22, 2019, 11:14:24 PM »
I don't think flow is of much importance here, it's in the start state.



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.
« Last Edit: May 23, 2019, 08:39:20 AM by krmko »

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Index was out of range when adding an item to arraylist
« Reply #5 on: May 24, 2019, 12:29:47 AM »
Strange thing happened, now i'm getting the same warning instead of error.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4582
    • View Profile
    • jinxtergames
Re: Index was out of range when adding an item to arraylist
« Reply #6 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?

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Index was out of range when adding an item to arraylist
« Reply #7 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.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4582
    • View Profile
    • jinxtergames
Re: Index was out of range when adding an item to arraylist
« Reply #8 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)

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Index was out of range when adding an item to arraylist
« Reply #9 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.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4582
    • View Profile
    • jinxtergames
Re: Index was out of range when adding an item to arraylist
« Reply #10 on: May 24, 2019, 04:34:15 AM »
Hi.
Try having a next frame in between.

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Index was out of range when adding an item to arraylist
« Reply #11 on: May 25, 2019, 07:06:23 AM »
Ok, i'll try it out.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14572
  • Official Playmaker Support
    • View Profile
Re: Index was out of range when adding an item to arraylist
« Reply #12 on: May 29, 2019, 11:46:33 PM »
Hi,

 Did this got sorted out?

 Bye,

 Jean

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 990
    • View Profile
    • Fat Pug Studio
Re: Index was out of range when adding an item to arraylist
« Reply #13 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.