Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualTheChubu

Posted 25 November 2013 - 01:44 AM

public void run(){
while(true){ //An infinite loop

if(quitGame){ //Make a boolean called quitGame and set it to false. When you want to quit, set it to true and the thread will stop running.

break;

onQuitGame(); //do whatever needs to be done after the game closes

}



Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

            

for(NPC npc : npcs) npc.update();//Put a method called "update" in your NPC class and override it for any subclasses you have.

//This is where the NPC should do things like change its position, interact with the world, etc.



repaint(); //goes through some Java wizardry, eventually calling the paint(Graphics g) method.

try {

    Thread.sleep(1000 / 60); //Sleep for 1 60th of a second, giving a frame rate of 60FPS

} catch(InterruptedException e) {}

            

    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}

}
}

There are a few problems with that piece of code. "onQuitGame()" is unreachable (loop breaks right before it gets called). You could move the condition to the while so you can do:
 

while(didntQuitGame)

{
// do stuff.

}
onQuitStuff();

Making the "onQuit" method reachable and the ending condition more obvious (if the didntQuitGame value gets set by another thread, then it should be volatile so the thread evaluates its finish condition with the most recent value of didntQuitGame, lest the VM optimize out the condition if the thread never sees it change).

 

 

Then the sleep call. What you're doing there is sleeping for 16 milliseconds, yes, that will make the loop run 60 times per second IF it had nothing else to do. Your frame time will be 16ms + whatever time it takes to do everything else in the game. Say that npc updating + repainting takes 4ms, your frame time will be of 16ms sleep + 4ms, ie 50 frames per second. Say that you do something heavy and your game takes 16 ms to compute a frame, something that would work wonderfully at 60 fps will work at a barely playable 30 fps with that sleep call in the middle.

 

And last, why there is a "setPriority()" there in the middle? What does accomplish? In any case, in Java (as with anything involving OS threads), the result of setting a priority of a thread is entirely OS dependent. So whatever results you get in Windows, won't be the same on Linux or OSX.

 

As for the "one thread per NPC"... I'll leave that for someone else biggrin.png


#2TheChubu

Posted 25 November 2013 - 01:42 AM

public void run(){
while(true){ //An infinite loop

if(quitGame){ //Make a boolean called quitGame and set it to false. When you want to quit, set it to true and the thread will stop running.

break;

onQuitGame(); //do whatever needs to be done after the game closes

}



Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

            

for(NPC npc : npcs) npc.update();//Put a method called "update" in your NPC class and override it for any subclasses you have.

//This is where the NPC should do things like change its position, interact with the world, etc.



repaint(); //goes through some Java wizardry, eventually calling the paint(Graphics g) method.

try {

    Thread.sleep(1000 / 60); //Sleep for 1 60th of a second, giving a frame rate of 60FPS

} catch(InterruptedException e) {}

            

    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}

}
}

There are a few problems with that piece of code. "onQuitGame()" is unreachable (loop breaks right before it gets called). You could move the condition to the while so you can do:
 

while(didntQuitGame)

{
// do stuff.

}
onQuitStuff();

Making the "onQuit" method reachable and the ending condition more obvious (if the didntQuitGame value gets set by another thread, then it should be volatile).

 

 

Then the sleep call. What you're doing there is sleeping for 16 milliseconds, yes, that will make the loop run 60 times per second IF it had nothing else to do. Your frame time will be 16ms + whatever time it takes to do everything else in the game. Say that npc updating + repainting takes 4ms, your frame time will be of 16ms sleep + 4ms, ie 50 frames per second. Say that you do something heavy and your game takes 16 ms to compute a frame, something that would work wonderfully at 60 fps will work at a barely playable 30 fps with that sleep call in the middle.

 

And last, why there is a "setPriority()" there in the middle? What does accomplish? In any case, in Java (as with anything involving OS threads), the result of setting a priority of a thread is entirely OS dependent. So whatever results you get in Windows, won't be the same on Linux or OSX.

 

As for the "one thread per NPC"... I'll leave that for someone else biggrin.png


#1TheChubu

Posted 25 November 2013 - 01:41 AM

public void run(){
while(true){ //An infinite loop

if(quitGame){ //Make a boolean called quitGame and set it to false. When you want to quit, set it to true and the thread will stop running.

break;

onQuitGame(); //do whatever needs to be done after the game closes

}



Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

            

for(NPC npc : npcs) npc.update();//Put a method called "update" in your NPC class and override it for any subclasses you have.

//This is where the NPC should do things like change its position, interact with the world, etc.



repaint(); //goes through some Java wizardry, eventually calling the paint(Graphics g) method.

try {

    Thread.sleep(1000 / 60); //Sleep for 1 60th of a second, giving a frame rate of 60FPS

} catch(InterruptedException e) {}

            

    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}

}
}

There are a few problems with that piece of code. "onQuitGame()" is unreachable (loop breaks right before it gets called). You could move the condition to the while so you can do:
 

while(didntQuitGame)

{
// do stuff.

}
onQuitStuff();

Making the "onQuit" method reachable and the ending condition more obvious.

 

 

Then the sleep call. What you're doing there is sleeping for 16 milliseconds, yes, that will make the loop run 60 times per second IF it had nothing else to do. Your frame time will be 16ms + whatever time it takes to do everything else in the game. Say that npc updating + repainting takes 4ms, your frame time will be of 16ms sleep + 4ms, ie 50 frames per second. Say that you do something heavy and your game takes 16 ms to compute a frame, something that would work wonderfully at 60 fps will work at a barely playable 30 fps with that sleep call in the middle.

 

And last, why there is a "setPriority()" there in the middle? What does accomplish? In any case, in Java (as with anything involving OS threads), the result of setting a priority of a thread is entirely OS dependent. So whatever results you get in Windows, won't be the same on Linux or OSX.

 

As for the "one thread per NPC"... I'll leave that for someone else biggrin.png


PARTNERS