Game Development Community

My Pause Behavior

by Robert Carroll · in Torque Game Builder · 06/03/2010 (2:24 pm) · 17 replies

Hi, I made a mouse pause behavior but I can't get it to work.
if (!isObject(MousePause))
{
   %template = new BehaviorTemplate(MousePause);
   
   %template.friendlyName = "MousePause";
   %template.behaviorType = "Scene";
   %template.description  = "MousePause on Mouse Down";
}

function MousePause::onBehaviorAdd(%this)
{
   %this.owner.setUseMouseEvents(true);
   if (!isObject(moveMap))   
      return;
}

function MousePuase::onMouseDown(%this)
{
   %this.pauseGame()      
   $isPaused = false; 
}

function pauseGame()   
{   
   if($isPaused)   
   {      
      $timescale=1;        
   }   
   else  
   {   
      $timescale=0;   
   }   
      
   $isPaused = !$isPaused;   
}

This is the console

>>> Advanced script error report.  Line 25.
>>> Some error context, with ## on sides of error halt:
   %this.owner.setUseMouseEvents(true);

   if (!isObject(moveMap))   

      return;

}



function MousePuase::onBehaviorRemove(%this)

{

   

}



function MousePuase::onMouseDown(%this, %modifier, %worldPos)

{

   %this.pauseGame()      

   $isPaused = false; 

##}##



function pauseGame()   

{   

   if($isPaused)   

   {      

      $timescale=1;        

   }   

   else  

   {   

      $timescale=0;   

   }   

      

   $isPaused = !$isPaused;   

}  


>>> Error report complete.

I'm confused?

#1
06/03/2010 (2:59 pm)
on mouse down you have $isPaused as false, should it not be set to true?



#2
06/03/2010 (3:21 pm)
You're going to get and error because you're calling %this.pauseGame() but your pauseGame function doesn't have a namespace.

Try changing it to:
function mousePause::pauseGame(%this)
{
 ...
}
Also, you should have your code in your onBehaviorAdd function after the check if the action map exists. Rennie is right too, move that into the onBehaviorAdd function so it defaults to being unpaused, then when you click it will pause.
function MousePause::onBehaviorAdd(%this)  
{     
   //this is a check to see if the 
   if (!isObject(moveMap))            
   return;  

   %this.owner.setUseMouseEvents(true);
   $isPaused = false; 
}

The logic in your pauseGame function is a little funky. Try what you have and work on that.

btw, you also misspelled pause a few times.
#3
06/03/2010 (7:24 pm)
%this.pauseGame()
$isPaused = false;

you are missing ; after the pauseGame()
#4
06/04/2010 (12:40 pm)
Actually It stops working after the first pause/resume :(
#5
06/04/2010 (1:21 pm)
Lets see you're new behavior...
#6
06/04/2010 (1:43 pm)
if (!isObject(MousePause))
{
   %template = new BehaviorTemplate(MousePause);
   
   %template.friendlyName = "MousePause";
   %template.behaviorType = "Scene";
   %template.description  = "MousePause on Mouse Down";
}

function MousePause::onBehaviorAdd(%this)
{
   %this.owner.setUseMouseEvents(true);
   if (!isObject(moveMap))   
      return;
}

function MousePuase::onMouseDown(%this)
{
   %this.pauseGame()      
   $isPaused = false; 
}

function pauseGame()   
{   
   if($isPaused)   
   {      
      $timescale=1;        
   }   
   else  
   {   
      $timescale=0;   
   }   
      
   $isPaused = !$isPaused;   
}
#7
06/04/2010 (2:38 pm)
sorry if i am jumping in, but by putting a global ($isPaused = false;) in your onMouseDown, are you not making it virtually impossible to ever pause the game? Because how does it ($isPaused) ever get set to true as your code requires it. Also why make $isPaused = !$isPaused;?


I am an intermediate level programmer with Torque and this just strikes me as wrong. I would, and do use something more like this and with success.


pauseBtn onMouseDown()
{
if($timeScale == 1)
$timeScale = 0;

if($timeScale == 0)
$timeScale = 1;
}


Something like that just seems simpler and I bet it will work.











#8
06/04/2010 (8:05 pm)
I tried this
if (!isObject(MousePause))
{
   %template = new BehaviorTemplate(MousePause);
   
   %template.friendlyName = "MousePause";
   %template.behaviorType = "Scene";
   %template.description  = "MousePause on Mouse Down";
}

function MousePause::onBehaviorAdd(%this)    
{       
//this is a check to see if the   
if (!isObject(moveMap))              
return;    
      
%this.owner.setUseMouseEvents(true);  
$isPaused = false;   
}  

function MousePause::onMouseDown(%this)
{
   %this.pauseGame();      
   $isPaused = true; 
}

function MousePause::onMouseDown()
{
if($timeScale == 1)
$timeScale = 0;

if($timeScale == 0)
$timeScale = 1;
}
      
   $isPaused = !$isPaused;   
}
#9
06/04/2010 (8:19 pm)
I don't have any pause behavior in my games, but I think you're looking for something more like:

if (!isObject(MousePause))
{
   %template = new BehaviorTemplate(MousePause);
   
   %template.friendlyName = "MousePause";
   %template.behaviorType = "Scene";
   %template.description  = "MousePause on Mouse Down";
}

function MousePause::onBehaviorAdd(%this)    
{       
  %this.owner.setUseMouseEvents(true);  
}  

function MousePause::onMouseDown()
{
  if($timeScale == 1)
    $timeScale = 0;
  else
    $timeScale = 1;
}
#10
06/05/2010 (2:00 am)
I tried that but it didn't work ether :(
#11
06/05/2010 (3:08 am)
The next step is to put echo-statement inside the functions to see if they are being called:

if (!isObject(MousePause))
{
  %template = new BehaviorTemplate(MousePause);
   
  %template.friendlyName = "MousePause";
  %template.behaviorType = "Scene";
  %template.description  = "MousePause on Mouse Down";
}

function MousePause::onBehaviorAdd(%this)    
{       
  echo( "MousePause behavior added to object " @ %this.owner );
  %this.owner.setUseMouseEvents(true);  
}  

function MousePause::onMouseDown(%this)
{
  echo( "In MousePause::onMouseDown, $timeScale = " @ $timeScale );
  if($timeScale == 1)
    $timeScale = 0;
  else
    $timeScale = 1;
  echo( "  * End of Function, $timeScale = " @ $timeScale );
}

Make sure there are no error message in the console, too.
#12
06/05/2010 (3:40 am)
@Robert,
You have 2 onMouseDown functions.
that makes no sesne, you only need, (can have?) 1. So....

onBehaviorAdd()
[
$timeScale = 1;
}

onMouseDown()
{
if($timeScale == 1)
$timeScale = 0;

if($timeScale == 0)
$timeScale = 1;
}


there is no need for the $isPaused variable, as you have it, unless you are using something elsewhere, but of course in that case you can jus use the $timScale variable. Also...



function MousePause::onMouseDown()  
{  
if($timeScale == 1)  
$timeScale = 0;  
  
if($timeScale == 0)  
$timeScale = 1;  
}  
  ///wtf is this?!      
   $isPaused = !$isPaused; 
///not necessary, and probably would report an error I am guessing.    
}




#13
06/05/2010 (7:13 am)
@Robert, that's odd, it works fine for me. Are you sure you're properly adding the behavior?

@Rennie,
$isPaused = !$isPaused;
That is perfectly valid code. TGB will interpret 0 as false and 1 as true so that bit of code simply flip flops the boolean value (that happens to be represented by a 1 or 0 in this case).

Now, it may not give what you expect if you had other values in there besides 0, 1, true or false...

So if $isPaused == 0 and you execute $isPaused = !$isPaused, then $isPaused will now be 1 and vice versa.
#14
06/05/2010 (7:43 am)
oh i see, thats why i said I think, not I know.


cheers.

#15
06/05/2010 (1:40 pm)
Ok what I'm trying to do is put this behavior
if (!isObject(PauseBahavior))   
{   
   %template = new BehaviorTemplate(PauseBahavior);   
      
   %template.friendlyName = "Pause Game";   
   %template.behaviorType = "Scene";   
   %template.description  = "Pause and unpause the game";   
  
   %template.addBehaviorField(pauseKey, "The button to pause the game", Keybind, "keyboard P");   
}   
  
function PauseBahavior::onBehaviorAdd(%this)   
{   
   if (!isObject(moveMap))   
      return;   
         
   moveMap.bindCmd( getWord(%this.pauseKey, 0), getWord(%this.pauseKey, 1), "pauseGame();", "");   
  
   $isPaused = false;   
}   
  
function pauseGame()   
{   
   if($isPaused)   
   {      
      $timescale=1;        
   }   
   else  
   {   
      $timescale=0.3;   
   }   
      
   $isPaused = !$isPaused;   
}
This code works ^^^^

With this one

if (!isObject(MouseDownExplodeBehavior))
{
   %template = new BehaviorTemplate(MouseDownExplodeBehavior);
   
   %template.friendlyName = "Mouse Down Explode";
   %template.behaviorType = "Input";
   %template.description  = "Clicking on the object causes it to explode";
   
   %template.addBehaviorField(particleEffect, "The particle effect object to use", object, "", "t2dParticleEffect");
   %template.addBehaviorField(explodeAtMouse, "Place the explosion at the mouse point rather than the object center", bool, false);
   %template.addBehaviorField(deleteOwner, "Deletes the object when the explosion occurs", bool, true);
}

function MouseDownExplodeBehavior::onBehaviorAdd(%this)
{
   %this.owner.setUseMouseEvents(true);
}

function MouseDownExplodeBehavior::onMouseDown(%this, %modifier, %worldPos)
{
   %position = %this.owner.position;
   if (%this.explodeAtMouse)
      %position = %worldPos;
   
   %this.explode(%position);
   
   if (%this.deleteOwner)
      %this.owner.safeDelete();
}

function MouseDownExplodeBehavior::explode(%this, %worldPos)
{
   if (!isObject(%this.particleEffect))
      return;
   
   %explosion = %this.particleEffect.cloneWithBehaviors();
   %explosion.position = %worldPos;
   %explosion.setEffectLifeMode("Kill", 1.0);
   %explosion.playEffect();
}

I deleted a bunch of useless stuff and ended up with the first behavior ^^^^^
Very top post.
#16
06/05/2010 (7:14 pm)
I don't know what to tell you. The code William provided in post #9 above does exactly what you're asking for.

Create a new script in your behaviors folder and paste this code into it:
if (!isObject(MousePause))
{
   %template = new BehaviorTemplate(MousePause);
   
   %template.friendlyName = "MousePause";
   %template.behaviorType = "Scene";
   %template.description  = "MousePause on Mouse Down";
}

function MousePause::onBehaviorAdd(%this)    
{       
  %this.owner.setUseMouseEvents(true);  
}  

function MousePause::onMouseDown()
{
  if($timeScale == 1)
    $timeScale = 0;
  else
    $timeScale = 1;
}
Reload your TGB project and attach that behavior to the object that you want to click to pause the game.

Also, make sure you don't have any other behaviors with the same name or TGB will get confused; meaning this line, not just the file name:
new BehaviorTemplate(MousePause);
#17
06/06/2010 (5:29 pm)
Ok, it was something to do with the object I was putting the behavior on. I deleted that object and made a new one and it works now. Sorry for all of this :(