Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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  
  General game object holder question  (Read 1377 times)
0 Members and 1 Guest are viewing this topic.
Offline yohash

Junior Newbie





« Posted 2012-04-18 02:20:40 »

Good evening,

I've had some experience with programming, but these last months were my first foray into OOP with Java. I was toying around with a game and have a general question about what approach/structure I should use to hold my objects. My specific case:

I have a game that has one object. Throughout the course of the game, multiple instances of this object are created, interact on-screen (via the chosen physics) and either disappear off-screen or are "destroyed" (so... also disappear). So, the collection that holds these objects is being constantly iterated over, tested for conditions, and objects are removed as these conditions are satisfied. At the same time, user interactions (button clicks) add a new object to this collection. (can think of them as bullets)

I've read about a couple of approaches that could work, but before I commit to one, I would like some general input on what may be the least-computationally expensive. Here's some that I've seen so far:

1) Use an ArrayList. Then, either use a concurrent protected version (like a synchronizedList) when using the iterators or synchronize all your access calls. I hear that synchronization can be computationally heavy; so should this be avoided? I hear CopyOnWriteArrayList can help avoid ConcurrentModificationExceptions, but that it is not good for arrays which are rapidly and dynamically modified, as it creates a copy on write.
2) Use an ArrayList, but save all 'remove' and 'add' functions for AFTER the iterator has completed its operations (to avoid concurrent modifications). THEN, compute all the additions and subtractions afterwards.
3) Same as (2) but use a 'for' loop, since there shouldn't be any concurrent modification (thus, no worries about iterator overrun)
4) Use another structure. I've read a forum where a structure called a 'bag' was made for dynamic collections. Any thoughts on this?
http://commons.apache.org/collections/apidocs/org/apache/commons/collections/Bag.html
5) ...

I hope my question is clear. I am looking for good approaches; recommendations for a sort of 'best practices' so I can start on the right foot. At the get-go, I used (1), but ran into some ConcurrentModificationException-s. Before I tore through the code and changed a lot of things, I thought I would get some experienced advice first.

Thank you for any help you can offer!
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #1 - Posted 2012-04-18 04:19:21 »

5) Don't use an Iterator. Synchronization is for threaded programs...

Myomyomyo.
Offline yohash

Junior Newbie





« Reply #2 - Posted 2012-04-18 04:57:48 »

Sorry, I didnt mention. The game-loop is threaded.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2012-04-18 06:41:20 »

Depends on your individual setup, on the way you programmed your game loop, on the number of objects involved, on the number of threads, etc.
For instance, I do a lot of hard synchronization and dont encounter performance or locking issues from that.
Keep the lock time span as short as possible. Use of synchronize gives the JVM the chance to optimize internally instead of concurrent collection classes, but there is no general advice possible.
Use a profiler to look out for blocked threads.

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.

PocketCrafter7 (12 views)
2014-11-28 16:25:35

PocketCrafter7 (8 views)
2014-11-28 16:25:09

PocketCrafter7 (9 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (66 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!