Small scripting problem
by Jeffrey Stark · in General Discussion · 08/08/2011 (1:13 pm) · 10 replies
Hello,
so I'm fairly new to scripting anything, I'm trying to create a 2d airplane game which the airplanes cannot go slower than a certain speed. I've got a max speed fine and they dont go backwards but a plane hanging in midair isn't great. so I thought I would try to add a gravity force if the plane went below a certain speed. here's my code.
if (!isObject(DependentgravaticforceBehavior))
{
%template = new BehaviorTemplate(Dependentgravaticforce);
%template.friendlyName = "Dependent force";
%template.behaviorType = "physics";
%template.description = "changes gravity based on speed";
%template.addBehaviorField(minspeed, "Mimimum speed to fly", float, "10");
%template.addBehaviorField(flyinggravity, "gravity force while flying", float, "1");
%template.addBehaviorField(stalledgravity, "gravity force while stalling", float, "0.1");
}
function Dependentgravaticforce::onBehaviorAdd(%this)
{
if (velocity > %this.minspeed)
{
gravityCoefficient = %this.flyinggravity
}
else if (velocity <= %this.minspeed)
{
gravityCoefficient = %this.stalledgravity
}
so like I said this is all new to me, I was wondering if someone could help me find the problem. (hopefully there aren't like 50, that would be embarrassing) I think it may be the "velocity" I couldn't find the code for that so I guessed. Thanks in advance.
so I'm fairly new to scripting anything, I'm trying to create a 2d airplane game which the airplanes cannot go slower than a certain speed. I've got a max speed fine and they dont go backwards but a plane hanging in midair isn't great. so I thought I would try to add a gravity force if the plane went below a certain speed. here's my code.
if (!isObject(DependentgravaticforceBehavior))
{
%template = new BehaviorTemplate(Dependentgravaticforce);
%template.friendlyName = "Dependent force";
%template.behaviorType = "physics";
%template.description = "changes gravity based on speed";
%template.addBehaviorField(minspeed, "Mimimum speed to fly", float, "10");
%template.addBehaviorField(flyinggravity, "gravity force while flying", float, "1");
%template.addBehaviorField(stalledgravity, "gravity force while stalling", float, "0.1");
}
function Dependentgravaticforce::onBehaviorAdd(%this)
{
if (velocity > %this.minspeed)
{
gravityCoefficient = %this.flyinggravity
}
else if (velocity <= %this.minspeed)
{
gravityCoefficient = %this.stalledgravity
}
so like I said this is all new to me, I was wondering if someone could help me find the problem. (hopefully there aren't like 50, that would be embarrassing) I think it may be the "velocity" I couldn't find the code for that so I guessed. Thanks in advance.
#2
08/08/2011 (1:56 pm)
Yeah I thought about looking at the console right after I posted this, the only error that is popping up is BehaviorComponent::addBehaviors - Missing Behavior DependentgravaticforceBehavior. After that I added the missing "Behavior" onto the end of a couple of my lines and added that missing } at the end but I still get the same error.
#3
if (!isObject(DependentgravaticforceBehavior))
{
%template = new BehaviorTemplate(DependentgravaticforceBehavior);
%template.friendlyName = "Dependent force";
%template.behaviorType = "physics";
%template.description = "changes gravity based on speed";
%template.addBehaviorField(minspeed, "Mimimum speed to fly", float, "10");
%template.addBehaviorField(flying, "gravity force while flying", float, "1.0");
%template.addBehaviorField(stalled, "gravity force while stalling", float, "1.0");
}
function DependentgravaticforceBehavior::onBehaviorAdd(%this)
{
if (t2dVectorLength(%owner.linearVelocity) > %this.minspeed)
{
(gravityCoefficient = %this.flying);
}
else
{
(gravityCoefficient = %this.stalled);
}
}
08/08/2011 (6:01 pm)
here's an updated version, the console marks the error right after "(gravityCoefficient = %this.flying);" , any help is appreciatedif (!isObject(DependentgravaticforceBehavior))
{
%template = new BehaviorTemplate(DependentgravaticforceBehavior);
%template.friendlyName = "Dependent force";
%template.behaviorType = "physics";
%template.description = "changes gravity based on speed";
%template.addBehaviorField(minspeed, "Mimimum speed to fly", float, "10");
%template.addBehaviorField(flying, "gravity force while flying", float, "1.0");
%template.addBehaviorField(stalled, "gravity force while stalling", float, "1.0");
}
function DependentgravaticforceBehavior::onBehaviorAdd(%this)
{
if (t2dVectorLength(%owner.linearVelocity) > %this.minspeed)
{
(gravityCoefficient = %this.flying);
}
else
{
(gravityCoefficient = %this.stalled);
}
}
#4
Since gravityCoefficient is a variable, you can't leave it naked, you have to mark whether it is local (%) or global ($). That tells the compiler whether this variable will be used within its function (locally) and then discarded, or if it will be saved (globally) for other functions to use. Since it looks like you'll want to use "gravityCoefficient" outside of that function, it should be preceded by a $.
08/08/2011 (10:50 pm)
I am not 100% positive since I do not work with behaviors, but I believe that the problem is that there is not a "%" or "$" preceding gravityCoefficient.Since gravityCoefficient is a variable, you can't leave it naked, you have to mark whether it is local (%) or global ($). That tells the compiler whether this variable will be used within its function (locally) and then discarded, or if it will be saved (globally) for other functions to use. Since it looks like you'll want to use "gravityCoefficient" outside of that function, it should be preceded by a $.
#5
08/09/2011 (9:55 am)
Ah thank you, well explained. I think that points me in the right direction. I appreciate it.
#6
08/10/2011 (7:08 am)
Also, you don't need those parenthesis for the inside of the if/else execution code. Parenthesis are used to denote precedence (ie, the stuff inside them get executed before other parts of an equation), or for passing arguments into a function/method or keyword that uses them. And yes, gravityCoefficient either needs a % or $ if it is a local or global variable, respectively, or maybe a %this. prefixing it if it is part of the DependentgravaticforceBehavior. If it is none of the above, you have to find out how to reach that variable, and code that accordingly, or else it's just sort of dangling out there to pop errors and swallow values into a black hole of nothingness, from which no known gameplay can escape...
#7
08/10/2011 (7:53 am)
haha yeah. I've been figuring more and more problems like that out as I've been going. It's a fairly steep learning curve. I did see a setminlinearvelocity function. that may be easier to get working. really the only thing I'm trying to accomplish is keeping my planes from being able to just sit still midair. As you can imagine it kind of takes the flying feel away from the game.
#8
08/10/2011 (9:04 am)
Do you have Torsion? If not, it's totally worth the money, and I use it every day. Helps tons in debugging, even with simple stuff.
#9
08/10/2011 (9:37 am)
No I don't, I saw it, wasn't really sure what it was when I was first looking into the torque 2d product so I didn't pick it up. I'll look into it, I think there's a demo to try and see if I can figure it out. From the screenshots it looks like it could be helpful in locating things in my project.
#10
08/10/2011 (12:25 pm)
If you don't, the free alternative I would recommend would be notepad++. It will at least recognize and mark the syntax and let you do some neat things. Also, when you inevitably have to debug, it is going to help tremendously to have your console available, and to do that you need to make a simple change. In your project's "common" folder, you need to open "commonConfig.xml", find the line that says "<ConsoleKey>tilde</ConsoleKey>", and change "tilde" to some other key, I would recommend "ctrl z". For some reason "tilde" just doesn't work.
Torque 3D Owner Ted Southard