Game Development Community

BindObj in Behaviors

by Deozaan · in Torque Game Builder · 08/23/2008 (8:05 pm) · 4 replies

Hi everyone,

I'm trying to write a controls behavior for some objects and I'm using the Asteroids Controls behavior as a base.

I see this part of the code:

function PlayerControlsBehavior::moveLeft(%this, %val)
{
   %this.left = %val;
}

And I can't figure out where or how %val gets any value assigned to it. Any help with that?

But maybe that's not relevant to what I'm trying to do.

What I'm trying to do is make it so that pressing (and releasing) a button will toggle something on or off. The button does not need to be held down to activate the toggle, and releasing the button doesn't do anything. To toggle it on or off again, just press the button again.

For some reason I can't get this to work using BindObj, but I am able to get it to work using Bind or BindCmd.

Can someone please explain what the differences between these are, as the documentation still has me confused.

Thanks!

#1
08/23/2008 (8:44 pm)
If I remember correctly, "%val" is just 1 when the key is pressed, and 0 when the key is lifted up. You could do what you want with something kind-of like

if( %val == 1 )
    %this.left = !%this.left

This would toggle the value "left" between 1 and 0 on each key press.
#2
09/17/2008 (10:33 pm)
Thanks William. Sorry for the late reply. I've been busy with school.

Your example for toggling is a good idea, but I think where I'm confused now is whether the toggle function will run over and over and over again for the entire duration the key is down (i.e. toggling on and off multiple times per keypress) or just run the function once per keypress when using moveMap.bindObj() to call the functions.
#3
09/18/2008 (12:32 pm)
Keybinds are only called on press and release, thats just how they work.
#4
09/20/2008 (4:28 pm)
Thanks James.

Aha! I see now: That means that when the key is pressed down, it sends %val = 1 and when it is released it sends %val = 0.