bind input to functions with parameters using bindCmd()
by Mohammed Laachir · in Torque 2D Beginner · 03/30/2014 (8:25 am) · 5 replies
My code:
GlobalActionMap.bindCmd(getWord(%this.jump, 0), getWord(%this.jump, 1), "PlayerMovementBehavior::jump(%this);", "PlayerMovementBehavior::stopJump(%this);");
It turns out to be impossible to do this, the variables don't work. I haven't found another way to do this. Is there a way? I want the player to be able to jump higher if he presses the space bar longer.
GlobalActionMap.bindCmd(getWord(%this.jump, 0), getWord(%this.jump, 1), "PlayerMovementBehavior::jump(%this);", "PlayerMovementBehavior::stopJump(%this);");
It turns out to be impossible to do this, the variables don't work. I haven't found another way to do this. Is there a way? I want the player to be able to jump higher if he presses the space bar longer.
About the author
Recent Threads
#2
03/30/2014 (10:45 am)
Thanks a lot, I did try it with bindObj, but I did not know about the %val variable. This will make it a lot easier.
#3
03/30/2014 (10:47 am)
My pleasure, good sir! Happy Torque-ing!
#4
03/30/2014 (11:25 pm)
Quote:Make sure that CMD is not passed as a string but as a standalone method name.I'm being nitpicky, but I'm pretty sure they're the same thing! Words without quotes are still interpreted as strings in TS.
#5
03/31/2014 (12:28 am)
You're right Daniel, editing my original response accordingly.
Associate Simon Love
Wiki.
Bookmark the Wiki; Obey the Wiki; Don't make the Wiki angry, you wouldn't like it when it's angry. :)
The form for bindCmd is as follows
DEVICE => keyboard
EVENT => "space"
MAKECMD => Command when the player presses the key
BREAKCMD => Command when the player releases the key
BindCMD is best used when calling global functions, not functions that exist within an object's namespace (often called Methods).
PlayerMovementBehavior refers to the overall class and not the instance of the behavior you are trying to call, you cannot call methods on it directly.
Solution
First, let's say that your player object is named PlayerObject. A good practice when adding behaviors is to initialize a dynamic field to keep track of each behavior easily.
After you've created the Behavior Template and created an instance from it, store it in a dynamic field on the PlayerObject, like so :
Then, instead of using bindCMD, use bindObj; this allows you to call methods located in an object's namespace.
The form for bindObj is as follows
DEVICE => keyboard
EVENT => "space"
CMD => Command to execute when the player presses or releases the key
OBJECT => Which object do we call CMD on?
CMD can be passed as a string or as a standalone method name.
In the following example, "Handlejump" and Handlejump would both be valid.
This will call method "Handlejump" on the MovementBehavior instance attached to PlayerObject.
The trick with this function is that it will be called when you press and release the space bar. In this scenario, the engine sends an argument to the function when the corresponding key is pressed; this variables value is 1 if the key is being pressed and 0 if it is being released.
The Handlejump function then needs to be modified to accept this argument. We call this argument %val but you can call it whatever you want when defining the function.
function PlayerMovementBehavior::Handlejump(%val) { if(%val) { //Execute your jump function here } else { //execute your stopJump function here } }You can then choose how to handle things, maybe you want to have all your logic within the HandleJump function or you might also want to simply call the appropriate function and handle your logic in there instead.