Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Check when looping through entities is done.  (Read 598 times)
0 Members and 1 Guest are viewing this topic.
Offline roseslayer

Junior Member


Medals: 1



« Posted 2013-06-24 20:27:50 »

Hello there,

I am currently having a small issue about ConcurrentModificationException(). I know there are alot of tutorials out there on how to fix this problem, but I only saw evryone had trouble removing entities. I am having trouble adding entities. I thaught I do the same thing as I did with destroying entities. I didnt knew I had this issue, because I was adding entities in the Game.java and not in an entitie. In that way I added the entities before the level was "ticking" through the arrayList of the entities. But now my problem is: I don't know how to add entities after ticking throught them. This is what I tried:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
    public synchronized void tick() {
       Iterator<Entity> it = getEntities().iterator();
        while (it.hasNext()) {
           Entity e = it.next();
           e.tick();
           
           if (e.destroyed) {
              it.remove();
           }
           
           if (e.spawn) {
              Entity s = e.getEntityToSpawn();
              this.getEntities().add(s);
              e.spawn = false;
           }
        }
    }


But as you can see I am just again adding the entities while looping through the list. Does someone know how to fix this and only explain it. I need to learn java so I want to get the solusion by myself!

Already thanks!
-RoseSlayer

Fundamentum W.I.P.
Offline Danny02
« Reply #1 - Posted 2013-06-24 20:51:53 »

one easy why to fix this is by having two collections while iterating over all game objects. Caching those two collections or creating every tick a new one and throwing the older one away is another topic.

So you tick loop over your game objects would look like this.

1. Get a empty collection
2. iterate over your old collection
3. add all game objects which still live to the new collection
4. add any new game objects to the new collection
5. mark the new collection as the primary game object collection
Offline ags1

JGO Ninja


Medals: 62
Projects: 3
Exp: 5 years


Make code not war!


« Reply #2 - Posted 2013-06-24 21:17:29 »

You can simply remove the entities using iterator.remove(), and add new entities to a holding collection while your loop is running. Then after the loop is finished, add all the entities from the holding collection to the real collection.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline roseslayer

Junior Member


Medals: 1



« Reply #3 - Posted 2013-06-25 14:04:00 »

Thanks for the help. If someone is interested in this code because (s)he is doing it also wrong:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
       List<Entity> nextEntities = new ArrayList<Entity>();
       
       for (Entity e : getEntities()) {
          e.tick();
          if (!e.destroyed){
             nextEntities.add(e);
            }
          if (e.spawn){
             nextEntities.add(e.entityToSpawn);
          }
        }


There is only 1 problem when I am spawning entities now: The first and last entity are glitching out or something. I am using this code to spawn them:
1  
2  
3  
4  
5  
         if(input.space.isPressed() && level.getTile(x >> 3, y >> 3) == Tile.GRASS){   
            bunny = new MobBunny(level, ((x >> 3)) * 8, ((y >> 3)) * 8, 1,5);
            spawn = true;
            entityToSpawn = bunny;
         }


And this is my Entity.java (just in case):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
package blockygames.game.entities;

import blockygames.game.gfx.Screen;
import blockygames.game.level.Level;

public abstract class Entity {
   
   public int x, y;
   protected Level level;
   public boolean destroyed = false, spawn = false;
   public Entity entityToSpawn;
   
   public Entity(Level level) {
      init(level);
   }
   
   public final void init(Level level) {
      this.level = level;
   }
   
   public abstract void tick();
   
   public boolean isDestroyed() {
      return destroyed;
   }
   
   public Entity getEntityToSpawn() {
      return entityToSpawn;
   }
   
   public int getX() {
      return x;
   }
   
   public int getY() {
      return y;
   }
   
   public abstract void render(Screen screen);
}


If someone has an idea how to fix this problem, because I don't know what I am doing wrong. The bunny is speedhacking/crashing or some sort of glitch.

Fundamentum W.I.P.
Offline jonjava
« Reply #4 - Posted 2013-06-25 21:29:23 »

Read these lovely things from princec and theagentd.

Though it has to be said, considering the number of things that might be found in an ArrayList, removing things from the head of the queue is really often so fast you'd never ever notice it. It depends on how many things are likely to be in it...

Consider a real-life example of, hmm... Particles. So you've got 5,000 particles in a frame (a high figure, but not unlikely). They're stored in an ArrayList. Each entry in the ArrayList takes 4 bytes, so that's 20kb of RAM, fitting neatly in the L1 data cache on many if not all desktops.

Now, imagine the rather unlikely even that every single particle dies one frame, and you discover this as you iterate through the ArrayList (using an integer index, not an iterator, just for absolute efficiency's sake). You have to shuffle 4,999 particles down 4 bytes. It's all in the L1 cache so memory access is effectively free to the CPU; you spend 4,999 cycles moving your particles assuming the loop that copies the data is about as simple and efficient as it can be. Then you have to do it again on the next particle. 4998 cycles. Repeat to solve triangular number, approximately (5000x5000)/2, or in the end 12,500,000 clock cycles, without ever having to touch the L2 or L3 caches most likely, let alone system RAM, until the end of the operation.

That's 12.5m cycles out of your 3.3 million or so you've typically got in a single video frame (2GHz core). Oh dear, you just spent 4 frames ditching a mere 5000 particles. Judder. Imagine if you had, somehow, 10000 particles. That'd be nearly a second wasted doing something utterly trivial and would present itself as a horrible jarring delay in your buttery smooth animation.

But what if, weirdly but possibly, the worst case occurred when even going backwards - every other particle died in one frame? Well, then you'd be compacting a slowly more complex list of particles even if you were scanning backwards. Scanning forwards would be exactly the same speed too.

So you'd be crafty and use the third and final technique which is to make a copy of the original arraylist, and copy the surviving particles into it each frame. This performs consistently no matter how many live or dead particles you have in any particular frame or the pattern of their expiration.

Anyway - for the OP - basically you need to do Just That. Each frame, scan your list of Interactables, and copy each live one into a second ArrayList, and then point your game at that ArrayList. Flip between two ArrayLists, alternating each frame, rather than creating (and expanding!) an ArrayList every frame, or that'll be slow.

Cas Smiley

[/quote]

LinkedList is rarely useful, since it has so much overhead even when it is supposed to be faster. For a very very large list where objects are constantly removed randomly hundreds of times per frame or so, LinkedList might be faster. The problem with LinkedList is that it creates an Entry object which it wraps in each object added to store the next and previous entries. This allocation (and later garbage collection) makes it a lot slower to add and remove stuff than ArrayList in most use cases. ArrayList's only weakness is removing objects in the beginning or middle of the list, since all following objects have to be shifted to fill the hole created. The longer the list the slower it becomes.

Even when you have lots of stuff to remove, ArrayList can be a good choice. Lists are often used to keep track of game objects. So you say "But wait! The objects will always be added at the end of the list, but as they die they will be removed! I have thousands of objects, so I should use a LinkedList to avoid shifting thousands of objects on every remove()!". Nope! Most likely the best solution is to use TWO ArrayLists and pingpong your objects between them. By doing that you can avoid all shifting while still getting fast (and random) access to all objects.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
ArrayList<GameObject> currentList, nextList;

//Lists created at load time


//Example game loop:

while(true){

    update(); //Updates all objects
   render(); //Draws all objects

}

private void update(){
    for(int i = 0; i < currentList.size(); i++){
        GameObject go = currentList.get(i);
        go.update();
        if(go.isAlive()){
            nextList.add(go);
        }
    }
   
    //Alive objects are now in nextList. Clear currentList.
   currentList.clear();
   
    //Swap current and next list
   ArrayList<GameObject> temp = nextList;
    nextList = currentList;
    currentList = temp;
}


Also Doesn't LinkedList leave null spaces in the List if you remove an object? or was that Hashtable...
Nope, LinkedList does not leave null spaces. Only arrays do that (obviously). Hashtables/HashMaps KIND OF do that, but that's simply because the key no longer maps to anything, so it returns null instead. Not really the same in my opinion.

related post (ConcurrentModificationProblem with bullets): http://www.java-gaming.org/topics/indexoutofbounds-index-7-size-7-need-help/28638/msg/261156/view.html#msg261156

quotes from: http://www.java-gaming.org/topics/arraylist-vs-linkedlist/27016/msg/240144/view.html#msg240144


PS: That new quote restriction is annoying as hell, that's why the quotes are broken.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2013-06-25 21:40:29 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
       List<Entity> nextEntities = new ArrayList<Entity>();
       
       for (Entity e : getEntities()) {
          e.tick();
          if (!e.destroyed){
             nextEntities.add(e);
            }
          if (e.spawn){
             nextEntities.add(e.entityToSpawn);
+            e.entityToSpawn = null;
+            e.spawn = false;
          }
        }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (103 views)
2014-09-10 06:39:09

Tekkerue (51 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!