Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Efficient caching for Entity System  (Read 1443 times)
0 Members and 1 Guest are viewing this topic.
Offline DzzD
« Posted 2010-02-21 11:52:58 »

I am looking for a way of doing an efficient caching system for an ES.

all update are made in memory (but only requiered/active parts of the DB are loaded) and periodically the cache is saved to the DB, the problem I found is that how to efficiently cache components of my entities without explode memory and keep an extremly fast acces to component based on their entity ID ?
for now I got something like the following :

use native array for memory storing
  => String componentLabel[];
  => Point3D componentLocation[];

and use an hastable to get entity index in natives arrays :
 => Hashtable<Integer,Integer> entityIDtoNativeArrayIndex;

I only use one hastable that give me the index of the entity for each possible components, I was wondering if anybody have a better idea ?  one problem I could face later will be that :
- memory used for an entity with only one component will be always equals to the size of all componenets.
- a new componenent will make a big memory grow (for all active entity it will exist an entry).

any (simple..) idea to compact this structure (wich is basically a 2D array with size equals to activeEntity*nbComponent) in an efficient manner  ?


EDIT:

what I need is to be able to get a list of all entity having a component of type "XXX" while still being able to faslty access some other component of those entities.

something that may look like :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
 havingPhysics[]=getListOfHavingPhysics();
 for(int n=0;n<havingPhysics.length;n++)
 {
   applyPhysics(havingPhysics[n]);
 }
 
 havingAppearance[]=getListOfHavingAppearance();
 for(int n=0;n<havingAppearance.length;n++)
 {
   applyAppearance(havingAppearance[n]);
 }

where havingAppearance & havingPhysics may overlap

Offline DzzD
« Reply #1 - Posted 2010-02-21 12:31:02 »

I got an idea that sound pretty good for me (to be honest I am still in R&D for that...)

maybe I could have an object Entity that will have an integer for every component, this integer will be the native array index
like  "grosso modo" :
1  
2  
3  
4  
5  
6  
Entity
{
 int haveApperance;
 int havePhysics;
 int haveLocation
}

with -1 means (null) => no component and other value equals to the index of the component in the native array, but this will be still 4 bytes per existing componenets for any existing entity this does not completly solve the memory probleme but seems to solve the  fast "cross-component" access

Offline DzzD
« Reply #2 - Posted 2010-02-21 12:55:57 »

first :

an entity classe :
1  
2  
3  
4  
5  
6  
Entity
{
 int haveApperance;
 int havePhysics;
 int haveLocation
}


and an entity native array :
1  
2  
Entity entities[];   //an array with all loaded entity
Hashtable<Integer,Integer> entityIndexById; //Enable to get an entity index in the native entity array with its ID


some components natives array :
1  
2  
3  
4  
CMPPoint3D location[];
CMPAppearance appearance[];
CMPString label[];
etc...


with each component having a properties entityIndex on a supperclass
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
SuperClassComponent
{
 int entityIndex;   //index of the entity in the native entity array
}

ClassComponentApperance extends SuperClassComponent
{
   //entityIndex is inherited
  int color; // ....or any other apperance component properties

}



so I can get all entity with a given component by just reading the component native array, and access any other component faslty
1  
2  
3  
4  
5  
6  
7  
for(int n=0;n<appearance.length;n++)
{
   applyApperance(appearance[n]);
   int entityIndex=apperance[n].entityIndex;
   Entity e=entities[entityIndex];
   location=location[e.locationIndex];
}


sounds pretty efficient, the only thing more I would requiere is an Entity class that dont always use an int per possible components...

EDIT: typos

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

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Reply #3 - Posted 2010-02-21 23:47:14 »

I personally tend to optimise entity management in each program i make. I find that I always do it different.
I am curious to know, what sort of game/application you were developing this for?

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline DzzD
« Reply #4 - Posted 2010-02-22 21:56:18 »

so finally end with something that I am satisfy with, will not explain details as it is internally very similar to the previous post :

one native array for all components :
1  
2  
3  
CMPLabel labels[];
CMPAppereance apperances[];
etc....


an entity array and a fast index acces Hastable
1  
2  
Entity entities[];
Hastable <Integer,Integer> entityNativeIndex;


the new point is that I added an EntityManager that is responsible of adding new entity and/or components, every component added to an entity is added by the entitymanager every new entity is added by the entity manager, this enable good integrity and nivce looking code and good eficiency,
code looks like :
1  
2  
3  
4  
5  
6  
7  
8  
9  
EntityManager em=...;
//load from DB
em.loadEntityFromDB(102);
//or build by hand
em.addEntity(103);
CMPLabel l=em.addComponentLabel(103);
l.value="label103";
CMPColor c=em.addCMPColor(103);
c.value=0xFFFFFF;


than later

1  
CMPVisible visibles=em.getVisibles();


and still able to threat componnents as blocks for systems or DB saving with cross access to other components of the current entity

1  
2  
3  
4  
5  
6  
7  
for(int n=0;n<visibles.length;n++)
{
 doVisibleSomething(visibles[n]);
 //and keep direct access to any other component
Entity e=visibles[n].getEntity();
 e.getAnotherComponent().value=3;
}


the EntityManager is responsible to maintain coherence between component & entity, all storage (component/entity) is made in native arrays, the Entity class is only usefull to enable cross component access and fast entity loading (in DB it give for an entity all components it use => direct DB modifcation are then inded not alowed to keep DB integrity).

I personally tend to optimise entity management in each program i make. I find that I always do it different.
I understand what you mean and have similar feeling than you, it would have been a lot easier (faster) to make something more specific but I really wanted to find something more generic to let 'more open ways for the futur'

I am curious to know, what sort of game/application you were developing this for?
this is for an online game : MMO and we can say in a certain manner MMORPG (even if there wont be any magic, potion, dwarf or such...)


EDIT: the slower part is to get an entity by its ID (use of the hashtable) but it should not be used a lot, all other acces are direct acces : all component keep a direct link to its entity and all entity keep a direct link to each of its components

Offline i30817

Junior Member





« Reply #5 - Posted 2010-02-22 22:41:34 »

Try the hasmap with cuckoo hashing someone here made.
Offline DzzD
« Reply #6 - Posted 2010-02-23 08:57:42 »

Try the hasmap with cuckoo hashing someone here made.
thanks as I said above it is not a bottlneck but if you can point me a link to this one and if it make a significant improvment it will be nice anyway

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.

BurntPizza (21 views)
2014-09-21 02:42:18

BurntPizza (15 views)
2014-09-21 01:30:30

moogie (18 views)
2014-09-21 00:26:15

UprightPath (25 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (43 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (97 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
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!