GameTaskQueueManager usage?

When I start a StandardGame and run a method which uses the GameTaskQueueManager, it runs perfectly. However, when I run the same method again later on it doesnt return a future. The method i've been using is the following. Forgive me for the rediculous content of the method but it was for testing sake only



  public String testQueue()
    {
       Future<String> future = GameTaskQueueManager.getManager().update(new Callable<String> ()
        {
            public String call() throws Exception
            {
                System.out.println("Test");
                String txt = "Returned String";
                return txt;
            }
           
           
        }
        );
        try
        {
            return future.get();
        }
        catch(Exception exc)
        {
            exc.printStackTrace();
        }
        return null;
    }



When I run it the first time, it returns "Returned String", when I run it again, it doesnt even show the Printline. I'm guessing it hangs on the get() method... Am I forgetting something? I've used Darkfrog's example in his article.

Thanks.

Maybe I'm missing something obvious, but the first thing that pops into mind is if you allowing time for the update loop to happen?  The .update(…) part of your code is placing the task on the update queue, so it will only get executed next time update runs.

The game doesn't hang or anything… that means the StandardGame can still execute updates and isnt hanging correct? Otherwise, how would I be able to find this out?



edit: I think you might be right since any GameTask I try to run, at runtime doesn't get executed. How can I succesfully add the GameTask to the Update loop?

If you are extending SimpleGame, StandardGame, or are using a JMECanvas, it's already done for you.  Otherwise, add something like this to your update method:


GameTaskQueueManager.getManager().getQueue(GameTaskQueue.UPDATE).execute();



You'll want to add a matching RENDER version in your render method.
renanse said:

If you are extending SimpleGame, StandardGame, or are using a JMECanvas, it's already done for you.  Otherwise, add something like this to your update method:

GameTaskQueueManager.getManager().getQueue(GameTaskQueue.UPDATE).execute();



You'll want to add a matching RENDER version in your render method.


I feel a bit silly for asking but are you sure StandardGame is extendable? Couldn't find a example in JME's source how its done.  ://

In the case of StandardGame, you'd use GameStates to add your game functionality.  There's several threads regarding how to best do so, and some wiki entries as well, iirc.

renanse said:

In the case of StandardGame, you'd use GameStates to add your game functionality.  There's several threads regarding how to best do so, and some wiki entries as well, iirc.


Oh.. that's strange however.. because when I add your line to a GameState's update method it doesnt get executed, however when I make a quick hack, to test, that looks something like this


        new Thread()
        {
            public void run()
            {
                while(true){
                GameTaskQueueManager.getManager().getQueue(GameTaskQueue.UPDATE).execute();
                    try
                    {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex)
                    {
                        ex.printStackTrace();
                    }
                }
               
            }
        }.start();



your solution works. The GameState is definatly enabled, otherwise I'd see nothing.

Edit: for some reason the Update of my GameState stops updating when It starts with


GameTaskQueueManager.getManager().getQueue(GameTaskQueue.UPDATE).execute();



Edit 2: The GameState is still active,even when the Updates are halted, but it apparantly refuses to execute the GameTask update  :?

Are you calling get() on the Future returned within your game loop perhaps?  If you do that it will cause the game loop to wait for itself to complete, which it cannot do until the blocking of the get call you made returns, so you end up with a thread blocking another thread blocking the original thread. :wink:

darkfrog said:

Are you calling get() on the Future returned within your game loop perhaps?  If you do that it will cause the game loop to wait for itself to complete, which it cannot do until the blocking of the get call you made returns, so you end up with a thread blocking another thread blocking the original thread. ;)


You're a genius!  XD
I was calling the Future from a GameState which checked for a collision! I think you got it right! That would explain as well why the GameTaskManager -would- work if I called it from a seperate thread.

But to solve it, this would mean my GameState has to create a new Thread (so the Future gets created/returned outside the gameloop) whenever there is a colission (this happends only <50 times during the entire game so I atleast wont end up with a million threads).


Basically this works for me but isnt this, as mentioned before, a quick hack?

Well, considering that the only reason GameTasks exist is for you to "inject" an execution into the game thread, if you're already in the game thread there's no reason to use it. :-p



However, if there is a possibility it will be outside of the game thread, then you can simply NOT call the get() which blocks.  If you just add it to the queue and don't call get() then it will run whenever and never cause blocking whether it's in the game thread or not.



I really think you should read up on the wiki about how this stuff works.  I spent some time writing a couple of articles about this and rather than repeating everything it would benefit you to go through them I think.

darkfrog said:

Well, considering that the only reason GameTasks exist is for you to "inject" an execution into the game thread, if you're already in the game thread there's no reason to use it. :-p

However, if there is a possibility it will be outside of the game thread, then you can simply NOT call the get() which blocks.  If you just add it to the queue and don't call get() then it will run whenever and never cause blocking whether it's in the game thread or not.

I really think you should read up on the wiki about how this stuff works.  I spent some time writing a couple of articles about this and rather than repeating everything it would benefit you to go through them I think.


After reading the article and evaluating my design, I've realised I've made a design error. Design error which led to this situation in the first place.
Oh well, back to the drawing board again. As always, thanks for replies. The community really is helpfull and without it I doubt I would've gotten this far! :)