Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  Best thing to do with dead entities during game?  (Read 2223 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Posted 2005-02-06 10:51:04 »

This is how I currently clean up dead objects.
It's not a good way of doing so and must change.

During the game objects die, their hit points are at or below 0.

I've read many ways of achieving this goal but my current situation would mean I have to implement "isDead()" checks throughout my scripts and collision code.

Any alternative fast way of removing dead entities?


private transient Player pc;
private ArrayList<GameCharacter> characters;
private ArrayList<IScript> scripts;

/**Clean up the dead*/
     public void cleanUpDeadObjects()
     {
           for(Iterator<GameCharacter> i = characters.iterator(); i.hasNext()Wink
           {
                 GameCharacter character = (GameCharacter)i.next();
                 
                 if(character.isDead())
                 {
                       J2DRenderQ.getInstance().remove(character);
                       i.remove();
                 }
           }
     }

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Online princec

JGO Kernel


Medals: 369
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2005-02-06 13:56:59 »

That's pretty much what I do, except I do it right after tick()ing the entity.

Cas Smiley

Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #2 - Posted 2005-02-06 13:58:48 »

I edited my code to not render or do any collision detection.
So I can permamently erase entities or temporarily erase them.

During the game I'm worried that I will invoke the GC.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ap_kelly

Junior Member




Java rocks!


« Reply #3 - Posted 2005-02-06 23:09:18 »

You could maintain 2 lists of characters, alive and dead ones. Your render and logic code only really proccess characters that are alive. When a character dies you move it to the dead list.

If you need to spawn a new character 'reset' a dead one and move it back onto the alive list. This way you never have the GC kick in and remove objects since they're always being referenced.

Andy.

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #4 - Posted 2005-02-16 20:28:22 »

This is what I do in Cosmic Trip:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
      public void update(float deltaTime) {
            // Update all actors
           for (int i = 0; i < maxActors; i++) {
                  if (actor[i] != null) {
                        if (actor[i].isAlive()) {
                              actor[i].update(deltaTime);
                        } else if (!actor[i].isLockedInUniverse()) {
                              actor[i] = null;
                              actors--;
                        }
                  }
            }
            sortActors();
            checkCollisions();
      }


As you can see, I just 'null' the reference of most Actors when they die and let the GC do it's job, except the Actors with isLockedInUniverse() = true (things like bullets, things that get respawned very often) although I probably didn't need to worry about them either.
You'll also see that I use a raw array for keeping track of the Actors. I once moved to ArrayList, but it was a lot slower (it spent *ages* in ArrayList.RangeCheck() ), so I changed it back.
I should still implement my own List without range checking everywhere, but didn't get around to it yet.

Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #5 - Posted 2005-02-19 01:49:57 »

Would that be better than just sticking to using arrays?

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #6 - Posted 2005-02-19 04:48:19 »

Interesting.
I never thought a standard array would be faster than an ArrayList.

Sun has always touted the performance of their collections being superior to arrays.


Quote
This is what I do in Cosmic Trip:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
      public void update(float deltaTime) {
            // Update all actors
           for (int i = 0; i < maxActors; i++) {
                  if (actor[i] != null) {
                        if (actor[i].isAlive()) {
                              actor[i].update(deltaTime);
                        } else if (!actor[i].isLockedInUniverse()) {
                              actor[i] = null;
                              actors--;
                        }
                  }
            }
            sortActors();
            checkCollisions();
      }


As you can see, I just 'null' the reference of most Actors when they die and let the GC do it's job, except the Actors with isLockedInUniverse() = true (things like bullets, things that get respawned very often) although I probably didn't need to worry about them either.
You'll also see that I use a raw array for keeping track of the Actors. I once moved to ArrayList, but it was a lot slower (it spent *ages* in ArrayList.RangeCheck() ), so I changed it back.
I should still implement my own List without range checking everywhere, but didn't get around to it yet.


Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #7 - Posted 2005-02-19 10:55:02 »

Quote
Sun has always touted the performance of their collections being superior to arrays.


ArrayList internally uses an array anyway, so ArrayList can never be faster than an array I would say...
It's just more convenient than using an array because all things that need to be done to grow/shrink/remove elements is already done for you. But it's doing RangeCheck() everywhere which takes lots of time if you're doing lots of access to the ArrayList.

Quote
Would that be better than just sticking to using arrays?

Only for the convenience of being able to re-use it. There's no need to change Cosmic Trip, the good-ole array works perfectly well but I can't simply go and reuse the same code again (some things are specific to CT).

Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #8 - Posted 2005-02-19 11:25:54 »

I guess you could implement a faster ArrayList, there is an AbstractList class to do so.

The speed of the array vs ArrayList is really about the algorithms behind it. Sun have stated that ArrayList is faster than casual use of an array is because most people couldn't implement custom algorithms that are better than Sun's.

When you mentioned an array being faster than an ArrayList I immediately thought there was something wrong with the ArrayList.

It would be simple enough to implement a custom collection class and use generics on it and override the appropriate method where the range check is occuring.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline tom
« Reply #9 - Posted 2005-02-19 12:28:24 »

Quote

It would be simple enough to implement a custom collection class and use generics on it and override the appropriate method where the range check is occuring.

Why bother. I can't believe ArrayList is causing any performance problems. You seem very keen to improve performance where there are no performance problems. Profile first, then optimize.

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

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #10 - Posted 2005-02-19 12:46:54 »

Quote

Why bother. I can't believe ArrayList is causing any performance problems. You seem very keen to improve performance where there are no performance problems. Profile first, then optimize.


The thing is, ArrayList *has* caused me some performance problems more than once because of the mentioned RangeCheck(). I agree for most things ArrayList is perfectly fine, but I could do with one without RangeCheck() to avoid loss of performance. And it's easy enough to implement, so why not?

Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #11 - Posted 2005-02-20 04:11:41 »

I just checked out the ArrayList class.  None of its code is anything smarter than I would write myself to handle array size changing or anything.  

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Valodim

Junior Member




*sigh*


« Reply #12 - Posted 2005-02-20 16:36:49 »

so, you could do better? Tongue
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #13 - Posted 2005-02-20 19:41:27 »

Well, it's all very trivial and there's so little you can screw up in implementing a resizable array yourself...

Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #14 - Posted 2005-02-20 20:31:05 »

Exactly.  Resize, fill.  The getters are just linear time for loop searches.  Everything's just a no-brainer I think.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
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!