Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
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  
  My entity properties... a bad idea?  (Read 2349 times)
0 Members and 1 Guest are viewing this topic.
Offline ags1

JGO Kernel

Medals: 125
Projects: 5
Exp: 5 years

Make code not war!

« Posted 2014-04-15 21:09:13 »

My entities for my fog game are just a collection of property classes. There will be thousands of entities so I want to multithread processing of the entities. To avoid race conditions, I plan to defer property updates like so (the earliest property update wins, although this might itself be a property of the property):

 * A gettable/settable property of an entity.
 * Set ops are parked and applied in the future so entity processing can be safely multithreaded.
 * Created by agslinda on 4/15/14.
public class Property<V> implements FutureUpdateable {

    public V prop, futureProp;
    //time is an abstract notion of when in a game loop something happens, it's not related to system time
    long time = 0;

    public V get() {
        return prop;

    public void set(V p, long time) {
        if (futureProp == null || time < this.time) {
            futureProp = p;
            this.time = time;

    public void update() {
        prop = futureProp;
        futureProp = null;
        time = 0;

So the Name property looks like this:

 * A name of a whatsit.
 * Created by agslinda on 4/15/14.
public class Name extends Property<String> {

There will be other kinds of properties for primitive long, boolean and double values.

Offline loom_weaver

JGO Coder

Medals: 17

« Reply #1 - Posted 2014-04-15 22:28:21 »

> There will be thousands of entities so I want to multithread processing of the entities.

Premature optimization.  Also thousands of entities isn't even a blink in the eye of today's CPUs.

Getters/setters are overrated in my opinion.  Start with an immutable class (hey no threading problems) and then expose things as you need them.
Online Roquen
« Reply #2 - Posted 2014-04-16 05:40:38 »

Where's the optimization?  This design needlessly (as described) slows things down.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ags1

JGO Kernel

Medals: 125
Projects: 5
Exp: 5 years

Make code not war!

« Reply #3 - Posted 2014-04-16 06:10:17 »

It will be slower in a single thread case, but I think it will enable the multi thread case to work...

Obviously if all the properties are immutable there would be no problem. The question is HOW to expose mutable properties.

Offline Danny02
« Reply #4 - Posted 2014-04-16 06:23:31 »

you would just clone your whole entity

class Player
  final int health;
  Player(){health = 100;}
  Player(int h){health = h;}
  takeDamage(int dmg){ return new Player(health - dmg); }

Player user = new Player();
user = user.takeDamage(15);

Online Roquen
« Reply #5 - Posted 2014-04-16 07:00:35 »

Don't multithread the AIs.  You don't need to.  If you think you need to: think again about how to lower the computations per frame.  About the only situation I'd think about it would be for a server bank targeting tens of thousands of active players and then I'd use a DSL (probably partly actor based).
Offline ags1

JGO Kernel

Medals: 125
Projects: 5
Exp: 5 years

Make code not war!

« Reply #6 - Posted 2014-04-16 07:01:17 »

Danny02, my original idea was to clone the whole entity, but now I think that would lead to a large amount of garbage collection...

Also, I think your sample is only going to work for single-property entities. Also, I think it will lead to lots of versions of the same instance floating about, which would not be desirable.

The main reasons I am thinking of multithreading are that (1) I am more accurately writing a sim than a game, so I want to go as big as I can go, and (2) the game has a fast forward button that multiplies the computational load.

@Roquen, I expect that AIs will not execute every frame for every entity. No real creature analyzes its situation 30 times a second... :-) So only a subset of the entities would be doing heavy lifting per frame.

Online Roquen
« Reply #7 - Posted 2014-04-17 12:41:09 »

Yeah that's one thing you can do...distribute across frames.  Sleep stuff as much as possible, etc. etc.

I've an example/mimimal prototype based variable here:
and of course, it's targeting single threaded. Wink
Offline ags1

JGO Kernel

Medals: 125
Projects: 5
Exp: 5 years

Make code not war!

« Reply #8 - Posted 2014-05-19 21:13:31 »

Well, I'm inching towards getting my critters to think:

public class Mind {
    private static Random R = new Random();
    private Mood mood = new Mood();
    private int millisToAct;
    private World world;
    private int countdownToCognition;
    private Entity entity;
    private Behavior chosenBehavior;

    public Mind(int millisToAct, Entity entity, World world) {
        this.millisToAct = millisToAct; = world;
        countdownToCognition = R.nextInt(millisToAct);
        this.entity = entity;

    public void think(int millisElapsed) {
        countdownToCognition -= millisElapsed;
        if (countdownToCognition < 1) {
            Set<Entity> vicinity = world.getVicinity(entity);
            Set<Entity> awareness = world.getAwareness(entity);
            int priority = 0;
            for (FutureUpdateable futureUpdateable : entity.getProperties()) {
                if (futureUpdateable instanceof Behavior) {
                    Behavior behavior = (Behavior) futureUpdateable;
                    int thisPriority = behavior.plan(entity, world, mood, vicinity, awareness);
                    if (thisPriority > priority) {
                        chosenBehavior = behavior;
                        priority = thisPriority;
            countdownToCognition = millisToAct;
        if (chosenBehavior != null) {

EDIT: works! I gave my creatures a chatter behavior, and they correctly describe their surroundings.

Offline ags1

JGO Kernel

Medals: 125
Projects: 5
Exp: 5 years

Make code not war!

« Reply #9 - Posted 2014-05-22 21:09:56 »

I think I have a good way to multithread my AIs. I have split the AI into two phases - a planning phase that does heavy computation but changes no state, and an execution phase that makes simple updates according to the plan. The planning phase is multithreaded and the execution phase is single-threaded.

   private List<FutureTask<List<Mind>>> planningTasks = new LinkedList<FutureTask<List<Mind>>>();


    private void logicFrame() {
        //do expensive planning on multithread
        for (int i = 0; i < planningTasks.size(); i++) {
        for (int i = 0; i < planningTasks.size(); i++) {
            try {
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
        //Update entities on a single thread
        for (Mind mind : minds) {
        //Push new values to buffers, re-sort x,y,z maps

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

bilznatch (27 views)
2015-08-04 11:03:17

SHC (44 views)
2015-08-01 03:58:20

Jesse (25 views)
2015-07-29 04:35:27

Riven (48 views)
2015-07-27 16:38:00

Riven (26 views)
2015-07-27 15:35:20

Riven (28 views)
2015-07-27 12:26:13

Riven (19 views)
2015-07-27 12:23:39

BurntPizza (42 views)
2015-07-25 00:14:37

BurntPizza (56 views)
2015-07-24 22:06:39

BurntPizza (35 views)
2015-07-24 06:06:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41: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!