Game Development Community

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
Page«First 1 2 Next»
#21
10/09/2008 (6:45 am)
Are you getting into the spring function at all?

This works for me:

moveMap.bind(keyboard, "alt w", sprint);

function sprint(%val)
{
   echo("Enter sprint");
   if(%val)
   {
      echo("Start Schedule");
      $Sprint_Schedule = schedule(1, 0, updateSprint);
   }
   else
   {
      echo("Cancel Schedule");
      cancel($Sprint_Schedule);
      PlayerBody.runEnergyDrain = 0;
      $mvForwardAction = 0;
   }
}

function updateSprint()
{
   echo("Moving with energy" SPC LocalClientConnection.Player.getEnergyLevel());
   $mvForwardAction = ($movementSpeed / 20) * LocalClientConnection.Player.getEnergyLevel();
   PlayerBody.runEnergyDrain = 0.3;
   $Sprint_Schedule = schedule(100, 0, updateSprint);
}

BUT, if you want to be able to hold the W button down and then hit the alt key to start sprinting, but above bind will not do that, you have to hit alt then W. How exactly are you wanting this to work? Do you want the alt key to toggle sprinting? That would be setup a little different.
#22
10/09/2008 (6:53 am)
Not to be a spoil sport here, but the scripted version of sprinting is not a good solution, for it to work in multiplayer the code would have to use many commandtoclient and commandtoserver function calls which would cause unnecessary network communication.

Instead you need to move the run physic logic into C, specifically the processTick() .
Bind your sprint function to affect a move trigger on the client such as $mvTriggerCount3
function sprint(%val){
   if(%val)
      $mvTriggerCount3++
}
Then look up the trigger on the received move of processTick within C

void Player::processTick(const Move* move){
// omited for space
if(move  && move->trigger[3]  ){
	//do login here
}
// omited for space
}


Also note never try to reinvent the wheel.
#23
10/09/2008 (7:18 am)
@ Anthony! I honestly didn't think sprinting was going to be such a major effort. I thought is was just, if you press alt-w, then increase the speed, and decrease the energy, and recalculate your speed based on energy by checking the energy level every 1000ms

It doesn't sound complicated, and I thought it could all be done in script client-side.

I think I might be in over my head.

@Eric, keyboard freezes with the latest code, only F10 and F11 work... That's OK. Looks like I have to take this to the engine, anyways.

Thanks everyone!
Tony
#24
10/09/2008 (9:00 am)
Doesn't freeze for me on a fresh TGEA 1.7.1... So something up with TGE, or something else is conflicting. That code is straight out of my default.bind.cs.

But if you are planning on going multiplayer might be better to go engine as Anthony suggested. Shouldn't be a big problem to do that.
#25
10/09/2008 (9:48 am)
Copy/Paste into default.bind.cs for TGE 1.5.2, doesn't work. I can walk, but sprint "alt w" is ignored.

I'm going to have to edit the source...

Thanks everyone!

Tony
#26
10/10/2008 (7:26 am)
Quote:It doesn't sound complicated, and I thought it could all be done in script client-side.
It isn't complicated - at least, the nature of your changes aren't complicated. It's just that script is good for some things, code is good for others, and both have their limitations. This is one of the cases here code is a far more sensible route, even for functionality that seems simple.
Scripting should be primarily used to define parameters, not behaviour. So script will tell you how fast a character sprints and how much energy drains per second. Script also defines when to start and stop sprinting. But the movement of the character while sprinting, just like normal character movement, should be handled in code.
#27
10/10/2008 (8:58 am)
Well, see, it was so easy to script a "sprint" button that just used $movementSpeed * 3

Its the whole "energy level" part that makes it require source changes. I tried the resource that @Anthony linked above. I made all the changes, double checked everything, built my project, but it isn't working yet.

It really doesn't look that difficult, but even the "easy" things are challenging when you're only learning C++ by jumping in headfirst.

Yes, I may be insane :)

Tony
Page«First 1 2 Next»