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 2 [3]
  ignore  |  Print  
  Java Continuations and GreenThreads  (Read 16683 times)
0 Members and 1 Guest are viewing this topic.
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #60 - Posted 2013-02-27 00:57:58 »

That's cool, thanks! Smiley

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #61 - Posted 2013-02-27 00:58:12 »

You can also rewrite the deployed jars with: http://pastebin.java-gaming.org/dd4093e4c42

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #62 - Posted 2013-02-27 01:04:02 »

That bytecode manipulation, it's like crack! Wink

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #63 - Posted 2013-02-27 01:06:15 »

I released the Java 6 versions of the library, can you verify it works?


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #64 - Posted 2013-02-27 01:23:40 »

Yep, works great with Java 6, thanks! Smiley

Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #65 - Posted 2013-02-28 14:02:52 »

I got what this lib is doing now, but I wasn't able to run my code. It got me this error:
Calling function not instrumented
In Coroutine.yield();.

I guess it is because of the bytecode not being changed. Having a yield() function simply reporting an error doesn't make sense.

So I looked into continuation-agent.jar and found that MAINFEST. It had the "premain" stuff included, but I didn't know how to use it. Either I did something wrong when I tried to run my exported .jar using a edited MAINFEST, or this doesn't work.

I've also tried to look at ant. I didn't use ant before, so this wasn't easy for me, and I didn't got it working in those couple of hours. I don't know how to "integrate" the ant code Matthias Mann showed in his blog post.

Is there a way to activate the java code agent through code?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #66 - Posted 2013-02-28 20:27:09 »

You can attach the java agent by following these instructions: Pointing

Quote
Add these files to the classpath:
   continuations-matthiasm.jar
   continuations-riven.jar

For ahead-of-time bytecode transformation, use the Ant task:
   de/matthiasmann/continuations/instrument/InstrumentationTask.java

For runtime bytecode transformation, use the Java Agent as VM parameter:
   -javaagent:continuations-agent.jar

http://indiespot.net/files/projects/continuationslib/usage.txt

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ags1

JGO Ninja


Medals: 62
Projects: 3
Exp: 5 years


Make code not war!


« Reply #67 - Posted 2013-02-28 22:14:33 »

Do I read this correctly - mixing green threads and real threads is problematic? I must misunderstand, as obviously this would mean not being able to take advantage of the multiple hardware threads of modern processors...?

Ah... I read the thread again, I think I get it now.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #68 - Posted 2013-02-28 22:41:59 »

You need a VirtualProcessor per native thread.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #69 - Posted 2013-03-01 12:29:59 »

You can attach the java agent by following these instructions: Pointing

I search literally everywhere. In MatthiasM's blog, everywhere in this topic... I just didn't got the Idea to look for a README or usage.txt in the Download directory... :/ my bad

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #70 - Posted 2013-03-01 12:37:20 »

You can attach the java agent by following these instructions: Pointing

I search literally everywhere. In MatthiasM's blog, everywhere in this topic... I just didn't got the Idea to look for a README or usage.txt in the Download directory... :/ my bad

Smiley

The file is also in the zip, which similarly contains the license files.
Out of all distributed files, files like README.txt are most often immediately skipped Pointing


It's actually not that odd that you couldn't find it at Matthias Mann's blog, as the agent code was originally mine and Matthiasm included it later.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #71 - Posted 2013-03-01 13:31:47 »

Works perfectly here Smiley Glad I could understand the basics of Green Threads and Continuations.

I just wonder, what if I want all my entities (which run on a VirtualThread each) have a delta time delivered. Just like I usually do it with a tick(float delta) method. How can I do that?
I don't actually have the problem, since there is
Gdx.graphics.getDeltaTime()
but I don't find 'static' to be a solution.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #72 - Posted 2013-03-01 15:50:05 »

Ticked based approaches are the opposite of what green threads are meant to provide.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline VeaR

Junior Member





« Reply #73 - Posted 2013-03-01 16:28:19 »

Ticked based approaches are the opposite of the what green threads are meant to provide.

Having two "services" for each entity is what i prefer. One "high level script" (or "behavior script") with continuations, that gives orders like this:

set_destination(target)
wait_until_target_reached_or_failed

Then a second, tick based loop, which handles the per-frame updates to position, direction, path following etc. This second service can run in a separate native thread, as you are suggesting, without using green threads. Then the two threads exchange messages. The path generation can be done on the script thread because that thread is not time sensitive, and generating a path is a one-time big time expense, while path-following is not. If some script event is delayed, thats not a problem. The bot might just stand a couple of more milliseconds where its path ended before continuing with its next action, but that is not game-breaking. But statistically, bots will be always following a path, and will rarely require attention from the script thread. Attacking the player, however, must be done on the tick thread, because the bot must react to player right away. What the script thread can do, is to set the bots reaction to player. Something like: "attack on sight", "neutral", "friendly", "ignore", "stop the path if attacked" etc. Then the condition checking and actual attacking is done by the per-frame thread. If the script wants the attack to end, it has to change the reaction type.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #74 - Posted 2013-03-01 16:38:11 »

Ticked based approaches are the opposite of what green threads are meant to provide.

Having two "services" for each entity is what i prefer. One "high level script" (or "behavior script") with continuations, that gives orders like this:

set_destination(target)
wait_until_target_reached_or_failed

Then a second, tick based loop, which handles the per-frame updates to position, direction, path following etc. This second service can run in a separate native thread, as you are suggesting, without using green threads.
That's indeed how you'd do it, except that I'm strongly suggesting to keep the second service (running the VirtualProcessor) in your main thread too. It makes sure that all memory read/write access is done on the same thread, which means your code is much simpler to comprehend and debug, as everything is deterministic.


1  
2  
3  
4  
5  
6  
7  
while(true) {
   virtualProcessor.tick(); // instruct unit movement, wait for arrival

   gameLogic.tick(); // actually move the units around

   engine.render();
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
public class Unit implements VirtualRunnable {
   private VirtualCondition targetReached = new VirtualCondition();
   private int xTarget, yTarget;

   public void moveTo(int x, int y) {
      xTarget = x;
      yTarget = y;
   }

   public void tick() {
      x += Integer.signum(xTarget-x);
      y += Integer.signum(yTarget-y);

      if(x==xTarget && y==yTarget) {
         targetReached.signal();
      }
   }

   @Override
   public void run() throws SuspendedExecution {
      while(...) { // move back and forth
        this.awaitArrivalAt(13,14);
         this.awaitArrivalAt(17,8);
      }
   }

   public void awaitArrivalAt(int x, int y) throws SuspendedExecution {
      this.moveTo(13,14);

      // this will fully suspend the green thread, there is no polling!
     targetReached.await();
   }
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline VeaR

Junior Member





« Reply #75 - Posted 2013-03-01 17:18:47 »

...
1  
2  
3  
4  
5  
6  
7  
while(true) {
   virtualProcessor.tick(); // instruct unit movement, wait for arrival

   gameLogic.tick(); // actually move the units around

   engine.render();
}


Yeah that is indeed simpler, but the path generation tends to be quite expensive. So some kind of scheduler is needed to make sure, that it doesn't happen that in a single iteration a bunch of bots request a new path at the same time, because that would be noticeable. So path generation is a good candidate to move out to a second thread. If its on a separate thread, then that thread can generate paths non-stop. And since path generation is tied to what the bot would actually do, the whole scripting is simpler and no "path generation queue" is necessary, if all is done with green threads. The bots will wait for their path until their turn comes to get it generated. I'm assuming that all this is running on a multicore CPU. With a single core, all should run on a single thread, like you described.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #76 - Posted 2013-03-01 17:26:46 »

Preferably you'd do VirtualThread.yield() in your path-finder, as to let the AI of multiple units handle path-finding concurrently (on the same thread), The longer a unit searches, the longer that specific unit has to wait for the final path, without blocking any other unit.

From an AI point of view, this is 'fair', and technically it's 'nice' (I love subjective terms).



I can't stress this enough, keep all your logic on 1 native thread, if you value your sanity.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #77 - Posted 2013-03-01 18:28:05 »

Ticked based approaches are the opposite of the what green threads are meant to provide.

What I have right now is this:
1  
2  
3  
4  
5  
@Override
public void run() throws SuspendedException {
    move(speed * Gdx.graphics.delta);
    VirtualThread.yield();
}


So you suggest not to do that, since it is 'ticky' ( Roll Eyes )... What about this?
1  
2  
3  
4  
5  
6  
@Override
public void run() throws SuspendedException {
    move(speed);
    // Probably also with subtracting the time the AI computation took:
   VirtualThread.sleep(16);
}


Well... this is awesome... I just Imagine what would happen. We wouldn't have a delta-time dependent game loop in the end, but a 'multiple-update render-once' loop. That fancy one, which doesn't make the 125 FPS jumps theagentd linked happen...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #78 - Posted 2013-03-01 18:51:33 »

Please look very closely at the example code I posted here:
http://www.java-gaming.org/topics/java-continuations-and-greenthreads/28337/msg/263607/view.html#msg263607

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #79 - Posted 2013-03-01 22:40:31 »

We wouldn't have a delta-time dependent game loop in the end, but a 'multiple-update render-once' loop. That fancy one, which doesn't make the 125 FPS jumps theagentd linked happen...
Uh, what? Did I miss something?

Myomyomyo.
Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #80 - Posted 2013-03-02 09:43:37 »


I don't actually know how to apply this to a game, which hasn't got anything similar to units. It's not a RTS.
If I apply this to my kind of game, which has "more action", then I'd be polling the input in the continuation and moving the player in the tick() method?

We wouldn't have a delta-time dependent game loop in the end, but a 'multiple-update render-once' loop. That fancy one, which doesn't make the 125 FPS jumps theagentd linked happen...
Uh, what? Did I miss something?
This
1  
2  
3  
4  
5  
6  
@Override
public void run() throws SuspendedException {
    move(speed);
    // Probably also with subtracting the time the AI computation took:
   VirtualThread.sleep(16);
}


is "running at 60 FPS" no matter what rendering framerate there is. And it's not another native thread...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #81 - Posted 2013-03-02 12:46:03 »

We wouldn't have a delta-time dependent game loop in the end, but a 'multiple-update render-once' loop. That fancy one, which doesn't make the 125 FPS jumps theagentd linked happen...
Uh, what? Did I miss something?
This
1  
2  
3  
4  
5  
6  
@Override
public void run() throws SuspendedException {
    move(speed);
    // Probably also with subtracting the time the AI computation took:
   VirtualThread.sleep(16);
}


is "running at 60 FPS" no matter what rendering framerate there is. And it's not another native thread...

It's running at 62.5 FPS, but whatever.

I don't remember posting any code that uses a variable time step recently. I've also not really recommended doing updating and rendering in different threads at the same time. Oh, did you refer to the Call of Duty video I posted? I thought you meant that some code I posted did sudden jumps up to 125 FPS.  Grin

Simply updating at a certain speed does solve the 125 FPS problem, but it also introduces some problems. Although the code looks very different, how would this behave differently from simply having a normal update loop that updates every 16th millisecond? It doesn't solve any of the key problems here. By updating 62.5 times per second, you're limiting yourself to 62.5 FPS too, since even if you were to render 125 FPS, every other frame would be identical to the one before since nothing's moved. What if the player's computer can't update the game at 62.5 updates per second? The game speed will slow down. The solution is the same as before: to update at a relatively low rate (30Hz maybe), but generate additional (unique) frames in-between by interpolating positions between the last and current frame.

The thing is that this has nothing to do with green threads, or even how you update the game. It only concerns how you render it. You can use a green thread for each game object to update them and render them with interpolation for perfect results, but green threads have nothing to do with the 125 FPS problem. You still have to face the exact same problem I was talking about in the 125 FPS problem thread.

Myomyomyo.
Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #82 - Posted 2013-03-13 20:07:05 »

@theagentd ... hm I agree and I don't know how to respond to that Smiley So I didn't post a reply

@Riven
How can I make it work with ant? I don't like distributing it with a continuations-agent.jar. It would be much more comfortable to simply make it be pre-compiled...

So here is what I used in my build.xml to try to make it work:
1  
2  
3  
4  
5  
6  
7  
8  
9  
  <taskdef name="continuations"
           classname="de.matthiasmann.continuations.instrument.InstrumentationTask"
           classpath="lib/asm-debug-all-4.1.jar:lib/continuations-matthiasm.jar:ant.jar:${module.jdk.classpath.ruinsofrevenge}"/>

   <target name="-post-compile">
      <continuations verbose="true">
          <fileset dir="${build.classes.dir}"/>
      </continuations>
  </target>


The paths "lib/asm-debug-all-4.1.jar", "lib/continuations-matthiasm-jar" and "ant.jar" are all checked and correct. Clicking on the "de.matthiasmann.*.InstrumentationTask" and IntelliJ IDEA opens the class...

But still it gives me this error:
1  
2  
Failed to load type(s):
Class not found de.matthiasmann.continuations.inistrument.InstrumentationTask

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #83 - Posted 2013-03-16 21:19:26 »

Okey. My problem is solved now, everything works now - even without an Agent (of course).

In the end I had the problem, that your classes weren't distributed pre-compiled. That would have made the "extract classes from jar; post-compile; overwrite classes in jar" redunant.

For everyone else who wants to use the ant task together with Riven's and MatthiasM's awesome libraries, can use this jar:
Direct download

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #84 - Posted 2013-03-16 21:26:15 »

I distribute the sourcecode as a separate download already.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #85 - Posted 2013-03-16 21:28:10 »

I distribute the sourcecode as a separate download already.

Yeah, thats okey, the jar I gave is just for people who don't want to put .java source files into their project, but make their project have some .jar's linked...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: 1 2 [3]
  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 (28 views)
2014-09-21 02:42:18

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

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

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

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

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

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

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

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

mitcheeb (71 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!