Search code examples
javaspawning

Spawning and terminating a person in a sim game (in Java)


I am developing a 2D grid based sim game. Progress is good. Been cracking out code for the last few days really well, and I've been having some issues today.

I build two rooms, set the door position, and spawn a person. The person has the 2 room types that they must visit in order to be cured (It's a remake of Theme Hospital). I click the spawn button, Person moves onto the grid from off screen, and then when the spawn is finished, they get their first path to the first room, then second, and then back to the ending spawn point.

When I say the spawn point, I mean the point off screen / off grid, when I say the ending spawn point, I mean the point where the person is when the spawn cycle is finished, and they are fully on screen / grid.

The problem now comes, what to do with the "person" (which is an object in its own right), is finished with? Should I simple set a boolean so they are no longer "active" but data on them remains? I wasn't sure, so I tried to just remove them from the ArrayList of people objects they were being held in.

I did this by assigning a number value to each person on creation which was the same as their new position in the ArrayList, and then when removing them, using that number. I know that in itself is floored, but I'm only testing with 1 person right now. If anyone can give me some help on that, fantastic! But I suppose that is more a sub question.

When the person is to be removed, they are, and I check the size of the arraylist, and then I get the following error... "Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException"

I did some researching and I understand this is to do with synchronisation of objects in different threads, but I'm not sure that I have more than one thread. I do have a single timer, and I don't think the people ArrayList is accessed elsewhere.

Any Thoughts? Ideas? Suggestions? Comments on this?

Thanks in advance!

(I will soon post a video and update this question with a link to it)


Solution

  • Typically I give every person or entity or whatever a boolean field called isMarkedForDeletion. Then during the main loop, they will either be told to update(), or if they are marked for deletion their index will be added to a deletion list. Once you have the deletion list all populated, you can iterate through the deletion list and remove those objects from the main list. Alternatively you can just iterate backwards and pull from the tail end of the list.

    This may or may not explain your ConcurrentModificationException - I've never gotten them when working with ArrayList's before. However, I've gotten them from LinkedList's and it was precisely for the reason you described and had nothing to do with threads. The actual cause was using an Iterator. The following will cause a ConcurrentModificationException:

    for (Iterator i = list.iterator(); i.hasNext();)
    {
        Person o = i.next();
    
        o.update();
    
        if (list.isMarkedForDeletion())
        {
            list.remove(o);
        }
    }
    

    This will not cause an Exception:

    ArrayList deletedObjects = new ArrayList();
    for (Iterator i = list.iterator(); i.hasNext();)
    {
        Person o = i.next();
    
        o.update();
    
        if (list.isMarkedForDeletion())
        {
            deletedObjects.add(o);
        }
    }
    
    for (int i = 0; i < deletedObjects.size(); i++)
    {
        list.remove(deletedObjects.get(i));
    }
    

    Perhaps the simplest way to do it, will not cause an Exception:

    for (int i = list.size()-1; i >= 0; i--)
    {
        Person o = list.get(i);
    
        if (o.isMarkedForDeletion())
        {
            list.remove(i);
        }
        else
        {
            o.update();
        }
    }