Author Topic: Updating my world  (Read 5938 times)

Leafybubbles

  • Newcomer
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Updating my world
« on: October 17, 2012, 05:13:11 PM »
I'm extreemly new to roguelike dev, and java game dev in general. I'm following Trystan's roguelike guide but am finding i'm running into trouble.

I have set up a feature where the "Fungus" spreads on the update meathod. The fungus is spreading correctly but i'm getting an error in my build output

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
   at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
   at java.util.AbstractList$Itr.next(AbstractList.java:343)
   at Roguelike_Dev.World.update(World.java:69)
   at Roguelike_Dev.screens.PlayScreen.respondToUserInput(PlayScreen.java:123)
   at Roguelike_Dev.ApplicationMain.keyPressed(ApplicationMain.java:33)
   at java.awt.Component.processKeyEvent(Component.java:6225)
   at java.awt.Component.processEvent(Component.java:6044)
   at java.awt.Container.processEvent(Container.java:2041)
   at java.awt.Window.processEvent(Window.java:1836)
   at java.awt.Component.dispatchEventImpl(Component.java:4630)
   at java.awt.Container.dispatchEventImpl(Container.java:2099)
   at java.awt.Window.dispatchEventImpl(Window.java:2478)
   at java.awt.Component.dispatchEvent(Component.java:4460)
   at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1850)
   at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:712)
   at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:990)
   at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:855)
   at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:676)
   at java.awt.Component.dispatchEventImpl(Component.java:4502)
   at java.awt.Container.dispatchEventImpl(Container.java:2099)
   at java.awt.Window.dispatchEventImpl(Window.java:2478)
   at java.awt.Component.dispatchEvent(Component.java:4460)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

After much playing around, i realized that my character "@" is not being updated and redrawn when the error occurs, only the fungus is being printed.

After this error occuring multiple times, the Character refuses to move (not sure if it won't update or won't redraw) I'm assuming it has to do with a List going out of bounds?

Anybody that is fimiliar with this tutorial or with this error in general please help me out? I can upload my project files somewhere if need be


mrrstark

  • Rogueliker
  • ***
  • Posts: 66
  • Karma: +0/-0
    • View Profile
Re: Updating my world
« Reply #1 on: October 17, 2012, 05:35:34 PM »
I encountered this error myself when I started into my first game-dev project.

Concurrent Modification means that somehow you're modifying the list that you're iterating through.

Code: [Select]
Ex:
for(Type t : list)
{
   list.remove(t);
}

or more likely wayyyy down in w/e you're calling from the loop:
for(Monster m : monsterList)
{
   doBattle(m, player);
}

void doBattle(Monster m, Player player)
{
    player.Fight(m);
    if(m.dead())
        monsterList.remove(m);
}
You can't modify that list since changing its size while iterating that way would screw up the iteration.

requerent

  • Rogueliker
  • ***
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: Updating my world
« Reply #2 on: October 17, 2012, 05:42:11 PM »
I don't know Trystan's tutorial, but your problem is a general one.

Code: [Select]
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
You're modifying a list while iterating through it.

Essentially, you're doing something like this-
Code: [Select]
for(Object o in objects):
     o.update(/*...*/);
     if(o.dead) objects.remove(o);

You need to protect the integrity of your list while you iterate through it. To do that, you need to keep an ObjectsToAdd and ObjectsToRemove lists in your update list.

Code: [Select]
for(Object o in objects):
     o.update(/*...*/);
     if(o.dead) ObjectsToRemove.add(o);

/*...*/

for(Object o in ObjectsToRemove)
     objects.remove(o);

You can do it a dozen different ways. Need to see code if you want any more help.