Game Development Community

%this.something = !%this.something

by rennie moffat · in Torque Game Builder · 11/25/2009 (1:57 pm) · 9 replies

%this.safe = !%this.safe; for instance.

IS this just saying, %this.safe is not true or false?


I am not sure the common use of such a statement.



About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
11/25/2009 (1:58 pm)
I am sorry bit I just looked further at the code.



%this.safe = !%this.safe;
if(%this.safe)
%this.owner.setFrame(1);
else {
%this.owner.setFrame(0);
%this.checkForPlayer();
}



so, in this bit,
by saying
%this.safe = !%this.safe;


it is saying these conditions apply wether safe is true or false?
#2
11/25/2009 (2:04 pm)
Check out tdn.garagegames.com/wiki/TorqueScript_Quick_Reference.

Since that is "=(space)!" that means the two are separate commands or an assignment operator followed by a not operator.
#3
11/25/2009 (2:07 pm)
so it is logical and not logical.

so regardless of state (logical or not)
the conditions that apply are true regardless?
#4
11/25/2009 (2:09 pm)
@Rennie,

It's simply changing the current boolean value of %this.safe to the opposite of whatever it currently is. It's the same as:
if(%this.safe == true)
{
%this.safe = false;
}
else if(%this.safe == false)
{
%this.safe = true;
}
#5
11/25/2009 (2:11 pm)
ok, great thanks Patrick - Roller Jesus!
#6
11/30/2009 (10:05 am)
Just looking this over.

What is the point of toggling between states?
#7
11/30/2009 (10:26 am)
well for a togglebutton its obvious, you want the state to switch between up and down, which is handled by a simple "isDown" boolean.

generally there are many cases where you only change between true and false states and its no rare thing that these toggle basing on something happening or not.
#8
11/30/2009 (11:05 am)
oh ok, this is the example I was looking at, so the condition could exist for either dragging or not on a mouse down. so in essence would it be safe to assume the conditions (on OR off) must be possible in order to use it?

function MouseDraggableBehavior::onMouseDown(%this, %modifier, %worldPos)
{
   // Toggle the drag status.
   %this.dragging = !%this.dragging;
   
   // We always stop dragging on mouse up unless the toggle option is set.
   %this.cancelOnMouseUp = !%this.toggleDragState;
   
   // Schedule this or else we'll get two onMouseDowns. One for the locked
   // objects, and again for the not locked objects.
   %this.owner.schedule(0, setMouseLocked, %this.dragging);
   
   if (!%this.centerOnMouse)
      %this.offset = t2dVectorSub(%this.owner.position, %worldPos);
}
#9
11/30/2009 (11:09 am)
PS this is the full code and I noticed there is a behavior field to toggle between states or not (it is a bool), however when checked, or not, it makes no difference to the controls.

What givith?

if (!isObject(MouseDraggableBehavior))
{
   %template = new BehaviorTemplate(MouseDraggableBehavior);
   
   %template.friendlyName = "Mouse Draggable";
   %template.behaviorType = "Input";
   %template.description = "Make an object draggable by the mouse";
   
   %template.addBehaviorField(centerOnMouse, "Center the object on the mouse", bool, false);
   %template.addBehaviorField(toggleDragState, "Start dragging on one click, stop dragging on the next click", bool, false);
}

function MouseDraggableBehavior::onBehaviorAdd(%this)
{
   %this.dragging = false;
   %this.cancelOnMouseUp = true;
   %this.offset = "0 0";
   %this.owner.setUseMouseEvents(true);
}

function MouseDraggableBehavior::onMouseDown(%this, %modifier, %worldPos)
{
   // Toggle the drag status.
   %this.dragging = !%this.dragging;
   
   // We always stop dragging on mouse up unless the toggle option is set.
   %this.cancelOnMouseUp = !%this.toggleDragState;
   
   // Schedule this or else we'll get two onMouseDowns. One for the locked
   // objects, and again for the not locked objects.
   %this.owner.schedule(0, setMouseLocked, %this.dragging);
   
   if (!%this.centerOnMouse)
      %this.offset = t2dVectorSub(%this.owner.position, %worldPos);
}

function MouseDraggableBehavior::onMouseUp(%this, %modifier, %worldPos)
{
   if (%this.cancelOnMouseUp)
   {
      %this.dragging = false;
      %this.owner.setMouseLocked(false);
   }
}

function MouseDraggableBehavior::onMouseDragged(%this, %modifier, %worldPos)
{
   if (%this.dragging)
   {
      %this.moveToMouse(%worldPos);
      
      // Once we have dragged, then dragging will always stop on mouse up.
      %this.cancelOnMouseUp = true;
   }
}

function MouseDraggableBehavior::onMouseMove(%this, %modifier, %worldPos)
{
   if (%this.dragging)
      %this.moveToMouse(%worldPos);
}

function MouseDraggableBehavior::moveToMouse(%this, %worldPos)
{
   %this.owner.position = t2dVectorAdd(%worldPos, %this.offset);
}