Game Development Community

Solved: Mount Camera Behavior

by Raymond Martinez · in Torque Game Builder · 05/27/2010 (5:09 pm) · 5 replies

I am trying to write a behavior that will automatically mount the camera to the owner of the behavior. The code below does not work properly OnAddToScene, but it does work onMouseUp. Does anyone know how to fix this or what is wrong?

if (!isObject(CameraMoveBehavior))
{
   %template = new BehaviorTemplate(CameraMoveBehavior);
   
   %template.friendlyName = "Camera Mount";
   %template.behaviorType = "Effects";
   %template.description  = "Move the camera to this object's world limits when the object is clicked";

   %template.addBehaviorField(moveTime, "The amount of time for the move to take (seconds)", float, 1.0);
}

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

function CameraMoveBehavior::onAddToScene(%this, %scenegraph)
{   
    if (!isObject(sceneWindow2d))
      return;   
   
   sceneWindow2D.mount( %this.owner );
}

function CameraMoveBehavior::onMouseUp(%this, %modifier, %worldPos)
{
   if (!isObject(sceneWindow2d))
      return;
   sceneWindow2D.mount( %this.owner );
}

#1
05/28/2010 (3:09 am)
Why not just put

scenewindow.mount()

inside on behaviorAdd()?



No need to call it twice and especially onMouseUp. You'd have to be doing something very funky in order for that to make sense.
#2
05/28/2010 (9:35 am)
@Raymond,

The reason - it seems - that it's not working the way you have it is that the sceneWindow isn't fully initialized when you're calling the mount function. Hence, why it works in the onMouseUp callback.

The answer is to fire it from a schedule (as is often the answer when things get wacky in TGB).
if (!isObject(CameraMoveBehavior))
{
   %template = new BehaviorTemplate(CameraMoveBehavior);
   
   %template.friendlyName = "Camera Mount";
   %template.behaviorType = "Effects";
   %template.description  = "Move the camera to this object's world limits when the object is clicked";

   %template.addBehaviorField(moveTime, "The amount of time for the move to take (seconds)", float, 1.0);
}

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

function CameraMoveBehavior::onAddToScene(%this, %scenegraph)
{   
   echo("onAddToScene called");
    if (!isObject(sceneWindow2d))
      return;   
   
   echo("Start Camera position: " @ sceneWindow2D.getCurrentCameraPosition());
   
   %this.schedule(150,mountCamera);
}

function CameraMoveBehavior::mountCamera(%this)
{
   sceneWindow2D.mount( %this.owner );
   echo("New Camera position: " @ sceneWindow2D.getCurrentCameraPosition());
}

function CameraMoveBehavior::onMouseUp(%this, %modifier, %worldPos)
{
   if (!isObject(sceneWindow2d))
      return;
      
   sceneWindow2D.mount( %this.owner );
}
Note, that I pulled it out into it's own function so I could use one schedule to fire both the echo and the mount simultaneously.

Hope it helps!
`Patrick
#3
05/28/2010 (10:17 am)
@ moffat: I actually tried that originally and that didn't work either. I had a feeling that it was what Patrick here said. But I couldn't think of a way to do what he said. ( side note: did you ever play Tibia, I had a friend on that game named Sir Moffat )

@Patrick: Thanks Patrick this is exactly what I was looking for. Your a life saver.
#4
05/28/2010 (10:45 am)
I also found an alternative solution for anyone that runs into a similar problem. I used CameraMountBehavior::onLevelAdded
#5
05/28/2010 (11:10 am)
Glad I could help!