Trouble modifying a behavior
by Corey Magin · in Torque Game Builder · 02/07/2008 (4:33 pm) · 7 replies
Hi. I want to modify the shoots behavior:
%template.addBehaviorField(projectile, "The projectile to clone and shoot", object, "", t2dSceneObject);
which binds whatever object I choose to be the projectile. I want the player to be able to press 1, 2 or 3 on the keyboard with a different projectile for each keypress. I'd like to write a switch function that changes the ammo state but how do I assign a keybind to the projectile?
Thanks in advance
%template.addBehaviorField(projectile, "The projectile to clone and shoot", object, "", t2dSceneObject);
which binds whatever object I choose to be the projectile. I want the player to be able to press 1, 2 or 3 on the keyboard with a different projectile for each keypress. I'd like to write a switch function that changes the ammo state but how do I assign a keybind to the projectile?
Thanks in advance
About the author
#2
02/08/2008 (3:13 pm)
@Corey - In Torque, you don't want to try to do this in the main game loop, you want to use the event systems that are built into the engine. Look at the behavior you have, and see how it sets up the keybind, and just set up some others.
#3
press joystick "0" - $ammoType = 1
press joystick "1" - $ammoType = 2
press joystick "2" - $ammoType = 3
or I might toggle through ammo types with the same keypress:
press joystick "0" - $ammoType++ and if == 3, reset to 1.
still working on this if anyone has any pointers I'd appreciate it. Still very new to torque script
02/08/2008 (9:26 pm)
Okay. I'm trying to figure out how to keybind a variable so that:press joystick "0" - $ammoType = 1
press joystick "1" - $ammoType = 2
press joystick "2" - $ammoType = 3
or I might toggle through ammo types with the same keypress:
press joystick "0" - $ammoType++ and if == 3, reset to 1.
still working on this if anyone has any pointers I'd appreciate it. Still very new to torque script
#4
02/08/2008 (11:33 pm)
@Corey - You bind keys to functions. Look in the behavior code, and (assuming you're using the behavior I think you're using) you'll see:function ShootsBehavior::onBehaviorAdd(%this)
{
if (isObject(moveMap))
moveMap.bindObj(getWord(%this.fireKey, 0), getWord(%this.fireKey, 1), "fire", %this);
}This is binding the key specified by the fireKey field to the "fire" method of this behavior. Try adding another keybind like this:function ShootsBehavior::onBehaviorAdd(%this)
{
if (isObject(moveMap))
{
moveMap.bindObj(getWord(%this.fireKey, 0), getWord(%this.fireKey, 1), "fire", %this);
moveMap.bindObj(getWord(%this.cycleKey, 0), getWord(%this.cycleKey, 1), "cycle", %this);
}
}You'll want to add another behavior field to specify the cycleKey, and of course you'll need to create the cycle method to do your switching, e.g.function ShootsBehavior::cycle(%this, %val)
{
if (%val == 0)
{
// the key went from pressed to unpressed, don't do anything.
return;
}
// do projectile cycling here
}Does that help?
#5
02/09/2008 (12:21 am)
Yeah that definately helps. One question though, what does the getWord method do with the 0 and 1?
#6
02/09/2008 (12:30 am)
The second argument to getWord is the index of the item you want to get, counting from 0, so getWord(%list, 0) returns the first element of the list, getWord(%list, 1) returns the second, etc.
#7
02/09/2008 (11:09 pm)
Got it all working really well, thanks again
Torque Owner Corey Magin
Where is the main game loop where I could check for keypresses during the game? Would any function do inside the game.cs? hmmm.