Syntax for using ENERGY in an equation?
by Infinitum3D · in Torque Game Engine · 10/07/2008 (8:35 am) · 27 replies
I have a Sprint function that works, and it reduces the energy bar over time.
$mvForwardAction = %val * $movementSpeed * 3;
What I'd like to do is affect my movement speed, based on the amount of energy I have.
maxEnergy = 100
$mvForwardAction = %val * $movementSpeed * ENERGY;
What is the syntax for ENERGY? I see maxEnergy and runEnergyDrain and minRunEnergy, but if I just se the word ENERGY it doesn't work.
Suggestions please!
Tony
$mvForwardAction = %val * $movementSpeed * 3;
What I'd like to do is affect my movement speed, based on the amount of energy I have.
maxEnergy = 100
$mvForwardAction = %val * $movementSpeed * ENERGY;
What is the syntax for ENERGY? I see maxEnergy and runEnergyDrain and minRunEnergy, but if I just se the word ENERGY it doesn't work.
Suggestions please!
Tony
#2
So I would need to use getEnergyLevel and assign that value to a variable, and then put the variable into the equation.
Does that sound right? Or is there an easier way?
Thanks!
Tony
notAProgrammer
10/07/2008 (10:53 am)
Thanks Mike!So I would need to use getEnergyLevel and assign that value to a variable, and then put the variable into the equation.
Does that sound right? Or is there an easier way?
Thanks!
Tony
notAProgrammer
#3
If I echo in the consle
echo(1991.getEnergyLevel());
It gives me 100, which is what I expect. Now, I know it would be a terrible hack to hard code the 1991 into my keybind. So, can someone help a non-programmer with the equation.
$mvForwardAction = %val * ($movementSpeed / 10) * 1991.getEnergyLevel());
Which DOES work by the way.
Thanks!
Tony
10/07/2008 (12:18 pm)
OK, my Player ID# is 1991.If I echo in the consle
echo(1991.getEnergyLevel());
It gives me 100, which is what I expect. Now, I know it would be a terrible hack to hard code the 1991 into my keybind. So, can someone help a non-programmer with the equation.
$mvForwardAction = %val * ($movementSpeed / 10) * 1991.getEnergyLevel());
Which DOES work by the way.
Thanks!
Tony
#4
You do realize though, that as your player's energy level reaches 0 in that equation, your player will be unable to move, right? Just mentioning..
10/07/2008 (12:33 pm)
If I recall correctly, "ServerConnection" will get you the client connection, from which you can find the player you're controlling. So what you're looking for is something like$mvForwardAction = %val * $movementSpeed * ServerConnection.getControlObject().getEnergyLevel();
You do realize though, that as your player's energy level reaches 0 in that equation, your player will be unable to move, right? Just mentioning..
#5
Thanks for the code! I'll try it now.
Tony
10/07/2008 (12:50 pm)
@Scott, Yes, that's exactly what I want. My player energy slowly restores automamatically when the player is standing still, it only decreases when the player is sprinting.Thanks for the code! I'll try it now.
Tony
#6
The energy depletes, but getEnergyLevel doesn't check every tick. I have to release the sprint key, and press it again, THEN it recalculates the speed, and slows Player down.
So its only calculating the speed on the initial keydown...
Thoughts?
I tried adding the code to my if (%val) like this
if (%val)
{
playerbody.runEnergyDrain = 0.3;
ServerConnection.getControlObject().getEnergyLevel();
}
but it didn't help.
Anyone know how to get it to keep checking the energy level?
Thanks!
Tony
10/07/2008 (12:55 pm)
Well, Scott's code works, but there's still a problem...The energy depletes, but getEnergyLevel doesn't check every tick. I have to release the sprint key, and press it again, THEN it recalculates the speed, and slows Player down.
So its only calculating the speed on the initial keydown...
Thoughts?
I tried adding the code to my if (%val) like this
if (%val)
{
playerbody.runEnergyDrain = 0.3;
ServerConnection.getControlObject().getEnergyLevel();
}
but it didn't help.
Anyone know how to get it to keep checking the energy level?
Thanks!
Tony
#7
That code above will not compile and work, but it should point you in the right direction.
10/07/2008 (1:53 pm)
You'll want to start a schedule when the put the key down, and stop it when the release it:if(%val)
{
$Check_Energy = schedule(0, checkEnergy());
}
else
{
cancel($Check_Energy);
$mvForwardAction = 0;
}
function checkEnergy()
{
$mvForwardAction = $movementSpeed * ServerConnection.getControlObject().getEnergyLevel();
$Check_Energy = schedule(100, checkEnergy());
}That code above will not compile and work, but it should point you in the right direction.
#8
10/07/2008 (2:32 pm)
Great Eric, thanks!
#9
10/07/2008 (5:13 pm)
Localclientconnection.player.getenergylevel()
#11
Is there anything better than PlayerBody for .runEnergyDrain? I was told itmay not work in multiplayer.
Tony
10/08/2008 (5:16 am)
Things are really progressing well, but I still need one change...Is there anything better than PlayerBody for .runEnergyDrain? I was told itmay not work in multiplayer.
Tony
#12
function sprint(%val)
{
$isSprinting = true;
if (%val)
{
PlayerBody.runEnergyDrain = 0.3;
if (isSprinting) schedule (1, 0, checkEnergyLevel);
}
else
{
cancel($isSprinting)
PlayerBody.runEnergyDrain = 0;
$mvForwardAction = 0;
}
}
10/08/2008 (9:56 am)
OK, the schedule isn't working quite yet...function sprint(%val)
{
$isSprinting = true;
if (%val)
{
PlayerBody.runEnergyDrain = 0.3;
if (isSprinting) schedule (1, 0, checkEnergyLevel);
}
else
{
cancel($isSprinting)
PlayerBody.runEnergyDrain = 0;
$mvForwardAction = 0;
}
}
#13
Then in your checkEnergyLevel function, make sure you are scheduling it again and assigning it to the same global variable.
10/08/2008 (10:02 am)
You have to assign the result of the schedule call to a global variable, then cancel that variable, so it knows what schedule to cancel.function sprint(%val)
{
if (%val)
{
PlayerBody.runEnergyDrain = 0.3;
$isSprinting = schedule(1, 0, checkEnergyLevel);
}
else
{
cancel($isSprinting)
PlayerBody.runEnergyDrain = 0;
$mvForwardAction = 0;
}
}Then in your checkEnergyLevel function, make sure you are scheduling it again and assigning it to the same global variable.
function checkEnergyLevel()
{
//do the move calculation
$isSprinting = schedule(1, 0, checkEngergyLevel);
}
#14
I know the flow is supposed to be:
If the player is sprinting,
then check the energy level,
calculate the speed,
move the player,
lather,
rinse,
repeat...
Thanks Eric! I'll keep trying.
Tony
10/08/2008 (10:40 am)
I did forget to make $isSprinting = false;I know the flow is supposed to be:
If the player is sprinting,
then check the energy level,
calculate the speed,
move the player,
lather,
rinse,
repeat...
Thanks Eric! I'll keep trying.
Tony
#15
Why can't we just add the schedule to "function sprint()" and loop?
i.e.
function sprint(%val)
{
if (%val)
{
$mvForwardAction = %val * ($movementSpeed / 20) * LocalClientConnection.Player.getEnergyLevel();
PlayerBody.runEnergyDrain = 0.3;
schedule(1, 0, sprint);
}
}
This should move the player based on the current energy level
then decrease the energy level
then the schedule should call the sprint function (itself looping only (if %val)
and move the player based on the New Current Energy Level
but it doesn't work.
Why?
Tony
10/08/2008 (12:58 pm)
Why do we need a second function for "function checkEnergyLevel"?Why can't we just add the schedule to "function sprint()" and loop?
i.e.
function sprint(%val)
{
if (%val)
{
$mvForwardAction = %val * ($movementSpeed / 20) * LocalClientConnection.Player.getEnergyLevel();
PlayerBody.runEnergyDrain = 0.3;
schedule(1, 0, sprint);
}
}
This should move the player based on the current energy level
then decrease the energy level
then the schedule should call the sprint function (itself looping only (if %val)
and move the player based on the New Current Energy Level
but it doesn't work.
Why?
Tony
#16
10/08/2008 (1:12 pm)
Because you are not passing a value to the method when you call the schedule, so it is not going into the if statement. And you still need to cancel the schedule at some point, or you are going to loop it forever.
#17
I need to put in my conditionals, isSprinting = true; to turn on the functon when ALT-W is pressed and isSprinting = false; when ALT-W is released...
I'll try again tomorrow.
10/08/2008 (2:06 pm)
Ah, OK. I think I've been staring at it too long. I need to put in my conditionals, isSprinting = true; to turn on the functon when ALT-W is pressed and isSprinting = false; when ALT-W is released...
I'll try again tomorrow.
#18
It's really much easier to start and stop the schedules by running another function within the function you bind to the key. The %val parameter gets set from the engine code and is 1 if the button was pressed and 0 if the button was released.
This is really what you want:
If you are launching a schedule that simply calls the method the schedule is in, you MUST capture the id of that schedule and have some way of calling a cancel on it. Setting booleans is going to do NOTHING to stop or start the schedules.
You should be able to copy and paste the above code and drop it into your bind.cs, then simply bind whatever key(s) you want to the sprint method and you should be good. If the above doesn't work, let me know because it should.
10/08/2008 (3:52 pm)
You HAVE to capture the schedule and call a cancel on it, or you are going to be looping though that method forever.It's really much easier to start and stop the schedules by running another function within the function you bind to the key. The %val parameter gets set from the engine code and is 1 if the button was pressed and 0 if the button was released.
This is really what you want:
function sprint(%val)
{
if(%val)
{
$Sprint_Schedule = schedule(1, 0, updateSprint);
}
else
{
cancel($Sprint_Schedule);
PlayerBody.runEnergyDrain = 0;
$mvForwardAction = 0;
}
}
function updateSprint()
{
$mvForwardAction = ($movementSpeed / 20) * LocalClientConnection.Player.getEnergyLevel();
PlayerBody.runEnergyDrain = 0.3;
$Sprint_Schedule = schedule(1, 0, updateSprint);
}If you are launching a schedule that simply calls the method the schedule is in, you MUST capture the id of that schedule and have some way of calling a cancel on it. Setting booleans is going to do NOTHING to stop or start the schedules.
You should be able to copy and paste the above code and drop it into your bind.cs, then simply bind whatever key(s) you want to the sprint method and you should be good. If the above doesn't work, let me know because it should.
#19
Thanks again for all your help. I was looking at the "WoW Style Starter.Kit" and it uses schedule with Booleans to start and stop the Yaw movements, so that's where I got the idea.
I see how you've done it now, and I can definitely follow your example, so I'll try it in tomorrow and let you know how it works.
Thanks again! You've been a big help. I've learned a lot from this.
Tony
10/08/2008 (5:21 pm)
Eric,Thanks again for all your help. I was looking at the "WoW Style Starter.Kit" and it uses schedule with Booleans to start and stop the Yaw movements, so that's where I got the idea.
I see how you've done it now, and I can definitely follow your example, so I'll try it in tomorrow and let you know how it works.
Thanks again! You've been a big help. I've learned a lot from this.
Tony
#20
Tony
10/09/2008 (6:20 am)
Sorry Eric, but it didn't work. I thought it might be the space between $Sprint and Schedule, but even with the space closed ALT-W doesn't sprint.Tony
Associate Michael Hall
Distracted...