Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 1969 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?
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
« Reply #1 - Posted 2012-04-18 04:19:21 »

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

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.

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
Pages: [1]
  ignore  |  Print  

EgonOlsen (76 views)
2018-06-10 19:43:48

EgonOlsen (56 views)
2018-06-10 19:43:44

EgonOlsen (76 views)
2018-06-10 19:43:20

DesertCoockie (256 views)
2018-05-13 18:23:11

nelsongames (156 views)
2018-04-24 18:15:36

nelsongames (155 views)
2018-04-24 18:14:32

ivj94 (896 views)
2018-03-24 14:47:39

ivj94 (157 views)
2018-03-24 14:46:31

ivj94 (809 views)
2018-03-24 14:43:53

Solater (173 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!