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  
  A good game architecture and design  (Read 8257 times)
0 Members and 1 Guest are viewing this topic.
Offline vbrain
« Posted 2013-07-08 19:59:30 »

I've been searching for weeks for a good game design and architectural approach. I need a great way to manage entities without having to loop through everything.

I'm developing a Real-time Strategy and I have been trying to figure out how to have the entities communicate and attack without having to loop through everything. I am using a grid manager where every entity is in a cell, then I select all the entity's neighbors to determine who to shoot. However, if there are a total of 50 entities in the nearby cells, and I have them all loop over each other and shoot, the game begins to lag. I'm trying to find an approach that doesn't utilize too many loops.

I have read many articles but so far none give a clear answer.
Please don't mention using an entity manager like Artemis, because I would still have to loop through everything.
Offline HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

« Reply #1 - Posted 2013-07-08 20:02:10 »

You'll find in many RTS's, that unit only attack when a) instructed, or b) are attacked themselves.

Offline vbrain
« Reply #2 - Posted 2013-07-08 20:03:55 »

You'll find in many RTS's, that unit only attack when a) instructed, or b) are attacked themselves.

Yes, I have played a few. The units in my game attack automatically when an enemy comes near them. I need someone with a good method to tell me how to go about making my game efficient.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2013-07-08 20:42:59 »

Use a profiler to find out the slow parts. Loops do not imply slowness.
One approach could be an event driven system, another to cut the number of processed entities for each frame / switch the entity chunk to process.
But thats all guessing.

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
Offline heisenbergman

JGO Coder

Medals: 14

L___ o_ G___ a__ P___

« Reply #4 - Posted 2013-07-09 00:04:40 »

Is your game multithreaded?

Maybe try putting the logic for checking targets into threads and run them simultaneously per entity?

Offline Shnagenburg

Junior Newbie

« Reply #5 - Posted 2013-07-11 17:10:41 »

Do units only attack the closet enemy within range? If so you may want to try adjusting your cell size to remove unneeded collision checks (a collision being an enemy collides with a units sight circle). Start with cells that are very small then expand outward until you get a candidate. Is it the case that units are only going to change their attack target if someone in those cell sets moves? An easy optimization is to only check for a new target if the nearby enemies have moved. Be sure your units are only checking the distance to relevant entities, such as not people on the same team. If you're using the distance formula to check who to attack, keep in mind that sqrt() is more expensive than just using radius^2 for everything.
Offline lcass
« Reply #6 - Posted 2013-08-22 20:44:46 »

Id say that you would either have to have them as seperate objects or use polymorphism to make an entity super  that has a tick(){}; and then the object itself handles the rest. there isnt really a better way to see if your troops can do anything other than make them check.
Offline Grunnt

JGO Kernel

Medals: 143
Projects: 8
Exp: 5 years

Complex != complicated

« Reply #7 - Posted 2013-08-22 21:52:36 »

However, if there are a total of 50 entities in the nearby cells, and I have them all loop over each other and shoot, the game begins to lag.

50 entities looping over 50 nearby other entities = 50 * 50 = 2500 decisions who to shoot each update. This is assuming that each entitiy will shoot each and every update loop, which is of course not the case I assume. These numbers are extremely low, and it does not make sense at all that this should cause lag.

Without seeing any code I can just say: do some profiling to find out why this is so slow.

Offline lcass
« Reply #8 - Posted 2013-10-01 20:46:16 »

Store the x and y of enemys in an array . Scan this array and then check the x and y (usin pythag) if the distance is less than their range kabloowy attack. Yes do put this in a seperate loop.
Offline philfrei
« Reply #9 - Posted 2013-10-02 02:34:04 »

If you have 50 entities each in two neighboring cells, are all 100 engaged? or only those closest to each other?

A couple ideas (hard to know without seeing the decision-making code itself, or a better description of the decision algorithm):

1) if you are using distance, remember that distances can be ranked without having to calculate square-roots. (X1-X2) * (X1-X2) + (Y1-Y2) * (Y1-Y2) is sufficient for ranking--no need to get a square root of this if the only purpose is ranking.

2) maybe there should be "sub-cells" of some sort within the cells?

But as several people have pointed out already, if the algorithm is efficient, you can brute force check many more entities against each other than just 50 against 50, so there's probably something in the decision logic that can be honed out.

music and music apps:
Pages: [1]
  ignore  |  Print  

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

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

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

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

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

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

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

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

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

Solater (159 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!