Learning how to script, pointers requested.
by Tony84 · in Torque Game Builder · 12/24/2009 (4:08 pm) · 5 replies
Hey there everyone, I bought TGB today and I also bought interactive scripting tutorial which I went throught, however there's a problem I'm not able to put my head around. My humble intention is to make a megaman who can jump up and return to earth. However what happens is player is able to press jump in midair and get really really high. What I intend to do with following code is to "disable jumping while in air", however it's not working.
If a kind soul could give me a pointer with a problem I've been trying to solve past 10 hours, I'd be deeply, deeply thankful:
If a kind soul could give me a pointer with a problem I've been trying to solve past 10 hours, I'd be deeply, deeply thankful:
// BUTTON PRESS ACTIONS
function Up()
if(Collision = true);
{
$MEGA.playAnimation(herojump);
$MEGA.setLinearVelocityY( -$MEGA.Hyppynopeus );
}
else
$MEGA.playAnimation(herojump);
//BUTTON RELEASE ACTION
function UpStop()
{
}About the author
#2
12/25/2009 (8:19 am)
What I tried next was naming scene object by name of "ground", knowing my character's class is "megaman" (btw just for training purposes, not attempting to actually a make a megaman game) I could place that into code too. I simply tried if I can find a solution from here, hence if megaman and ground meet there should be "working" in console, alas there's no such thing.//Collision attempt.
function ground::onCollision(%this, %obj, %col)
{
if(%col.getClassName() $= "megaman")
{
echo("working!");
%obj.delete();
}
}
//Collision attempt.
#3
%val (and %this.jump) are given a value of 1 when the key is pressed down, and a value of 0 when the key is released. Then, we check if our character currently has a constant force (i.e. gravity) pushing on it. If the constant force is 0, then we know the player is on the ground and is allowed to jump - the if (!%gravity) condition is true and we can call %this.updateMovementY();
If the constant force is any number other than 0, then we know the player is in the air and should not be allowed to jump again - the if condition is false and the updateMovementY method is skipped.
In case it interests you, within the updateMovementY method is where we apply a linear velocity to the player character and turn gravity on. It looks like this:
The tutorial this is from isn't very beginner friendly, but if you want to take a look it can be found here: tdn.garagegames.com/wiki/TGB/Tutorials/Platformer/Player. The behavior code is at the bottom of the page and meant to replace the entire playerClass script due to some issues that have cropped up with the physics in TGB.
12/25/2009 (8:57 am)
Welcome to TGB! I don't know if this will exactly solve your problem, but here is an example of how to control the player jumping. The basic assumption is that when the player is on the ground, we don't need to have gravity turned on. When the jump key is pressed, this method is called:function PlatformerControlsBehavior::jumpAction(%this, %val)
{
%this.jump = %val;
%gravity = %this.owner.getConstantForceY();
if (!%gravity)
%this.updateMovementY();
%this.updateAnimation();
}%val (and %this.jump) are given a value of 1 when the key is pressed down, and a value of 0 when the key is released. Then, we check if our character currently has a constant force (i.e. gravity) pushing on it. If the constant force is 0, then we know the player is on the ground and is allowed to jump - the if (!%gravity) condition is true and we can call %this.updateMovementY();
If the constant force is any number other than 0, then we know the player is in the air and should not be allowed to jump again - the if condition is false and the updateMovementY method is skipped.
In case it interests you, within the updateMovementY method is where we apply a linear velocity to the player character and turn gravity on. It looks like this:
function PlatformerControlsBehavior::updateMovementY(%this)
{
%this.owner.setLinearVelocityY(%this.jump * %this.jumpSpeed);
if (%this.jump)
{
%this.owner.setConstantForceY(%this.gravity);
}
}The tutorial this is from isn't very beginner friendly, but if you want to take a look it can be found here: tdn.garagegames.com/wiki/TGB/Tutorials/Platformer/Player. The behavior code is at the bottom of the page and meant to replace the entire playerClass script due to some issues that have cropped up with the physics in TGB.
#4
I tried applying the code, however console tells me that:
(28): Unable to find object '@{'
(31): Unable to find object: '' attempting to call function "UpdateMovementY"
(32): Unable to find object: '' attempting to call function "updateAnimation"
I have a feeling I don't quite understand how %this works, my best guess is that it's referring to whatever "%this" at current time is? However if this is the case I do not know how to declare and tell computer what "%this" supposed to be.
ps. is "%this" used to define where a variable is stored?
12/25/2009 (10:50 am)
Thank you for your replyI tried applying the code, however console tells me that:
(28): Unable to find object '@{'
(31): Unable to find object: '' attempting to call function "UpdateMovementY"
(32): Unable to find object: '' attempting to call function "updateAnimation"
function Megaman::onLevelLoaded(%this, %scenegraph)
// DEFINING WHAT BUTTONS ARE AVAIBLE
{
$Megaman = %this;
moveMap.bindCmd(keyboard, "Up", "Up();", "UpStop();");
%this.enableUpdateCallback();
}
//==== UP BUTTON ==========
function Up()
{
PlatformerControlsBehavior::jumpAction();
}
function UpStop()
{
}
//==== UP BUTTON ENDS =====
function PlatformerControlsBehavior::jumpAction(%this, %val)
{
%this.jump = %val;
%gravity = %this.owner.getConstantForceY();
if (!%gravity)
{
%this.updateMovementY();
%this.updateAnimation();
}
}I have a feeling I don't quite understand how %this works, my best guess is that it's referring to whatever "%this" at current time is? However if this is the case I do not know how to declare and tell computer what "%this" supposed to be.
ps. is "%this" used to define where a variable is stored?
#5
As long as you only have 1 "Megaman" in your level, you have already assigned that object to the global variable $Megaman. You can then call functions like below:
12/26/2009 (2:48 pm)
%this refers to the object that a particular function is being called on. It is useful for functions that apply to multiple objects, either because they share the same class or use the same behavior. There was a recent thread that discussed %this here: www.torquepowered.com/community/forums/viewthread/107709. If you still have questions, feel free to ask.As long as you only have 1 "Megaman" in your level, you have already assigned that object to the global variable $Megaman. You can then call functions like below:
function Up()
{
%gravity = $Megaman.getConstantForceY();
if (!%gravity)
{
$Megaman.updateMovementY();
}
}
Torque Owner Tony84
My logic was that "if object is not moving, it must be on ground. So if velocity is 0 you are on ground.
I'm new to scripting, but really excited and motivated to learn more, I hugely appreciate any pointers.
Thank you!