Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (711)
Games in Android Showcase (213)
games submitted by our members
Games in WIP (785)
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 5741 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 dysoptian 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  
You cannot reply to this message, because it is very, very old.

numerical (333 views)
2017-02-21 07:32:16

numerical (333 views)
2017-02-21 07:31:46

theagentd (444 views)
2017-02-18 13:42:33

theagentd (442 views)
2017-02-18 13:35:16

h.pernpeintner (1610 views)
2017-01-24 22:39:11

h.pernpeintner (1597 views)
2017-01-24 22:38:32

Galdo (2162 views)
2017-01-12 13:44:09

Archive (2134 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2670 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2353 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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!