Game Development Community

Sprint Button Resource trouble

by Infinitum3D · in Torque Game Engine · 10/15/2008 (10:36 am) · 39 replies

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2697

Daniel Neilsen did what is apparently a very good resource, however, its not working for me. I'm using TGE1.5.2, and it should work according to mb and Christopher ross.

Everything looks straight forward, copy, build, play but my sprint button gets ignored. If I left out a bracket { or a semicolon ; it wouldn't build (right?), so I must have just put something in the wrong place. Any help at all would be greatly appreciated!

Thanks!

Tony
Infinitum3D

My player.cc www.geocities.com/infinitum3d/player.txt
Page«First 1 2 Next»
#21
10/17/2008 (6:17 am)
Just to make sure it really is happening, move that echo into the if(%val) to make sure that its actually getting past that.
#22
10/17/2008 (7:14 am)
OK. I'm trying it on a fresh install of 1.5.2

Its building right now (sheesh, forgot how long a first build takes)!

Tony
#23
10/17/2008 (7:45 am)
Will reply with a summeration of what to to do . . .soon
#24
10/17/2008 (8:22 am)
Just tried it in a fresh install, and still no good.

Obviously the problem is me and not the resource, but if the files compile for silentMike and work, but not for me, then it must be something other than the files I'm looking at...

@Scott, I'm sorry, what do you mean move the echo into the if (%val)? When I do that, it loads the mission, but the keyboard locks, only F-10 and F-11 work.

@Anthony, thank you!
#25
10/17/2008 (8:31 am)
DISCLAIMER, I didn't actually implement this, but it is a theory, that should work, it might have some bugs , but most importantly will teach you some important fundamentals of adjusting a class to suit your needs.



A couple things, I was looking at how I would add sprinting into the player class and found I could use some stock torque behavior and properties coupled with something new.

I will do my best to give you an out line of what is going on:

(player.cs)
First thing is the playerdatablock needs some things turned on
In the player's datablock :

A) Be sure runEnergyDrain is set, (this is a stock variable) set it to something like 0.1
(by default it would cause a drain for all movement, but I will show you where it is tested, then we will use it for our own needs)

B) Be sure maxEnergy is set, to like 100 (total energy for player)

D) Be sure rechargeRate is set to 0.3 (recharge energy constantly)

C) Be sure energy is initialized. . . (turn on properties)
playerdata::onAdd(%data, %obj){
// . . .other stuff
%obj.setEnergyLevel(%this.maxEnergy);
%obj.setRechanrgeRate(%this.rechargeRate);
}

So then on to C, we need to do a couple things here, isolate the code that affect movement speed, adjust it to suit our needs.

(player.cc)
Any object on the server is processed each tick with ProcessTick(move) . The parameter passed in is a move from the client, ($mvTriggerCount3++). So you will look at that object to determine what the player is doing. Towards the bottom of Player::processTick() you'll see something like this

if(isServerObject() || (didRenderLastRender() || getControllingClient()))


Meaning, this code executes on the server, we just rendered, we have the client who is controlling this player. Within the block you'll see a call UpdateMove(move); This is the function that handles the players physics.

In Player::UpdateMove() look for

if (mEnergy >= mDataBlock->minRunEnergy)

This block determines if the player has enough energy to move and if it drops below a point it will stop them. Specifically

if (moveSpeed)
mEnergy -= mDataBlock->runEnergyDrain;
D)

So we'll make a new block below to adjust the moveSpeed

BELOW
else
         pv.set(0.0f, 0.0f, 0.0f);

Something like :
//if there is a movespeed
//if there is a move
//if trigger 3 is press in the move
//lower the energy
//if energy isn't 0
//scale the speed based on SprintScale
		if(moveSpeed && move  && move->trigger[3] ){
           		mEnergy -= mDataBlock->runEnergyDrain;
			if(mEnergy > 0 )
				moveSpeed *= mDataBlock->SprintScale;
		}

Of course SprintScale doesn't exsist does it. So the last thing will be a quick explaination of how to add a new variable

(player.h)
E)

Within a public area of playerData class add
F32 SprintScale;

So that creates it, but we have to do a slew of other things to use it

(player.cc)
F)

First Initalize it, in PlayerData's constructor

PlayerData::PlayerData()
SprintScale = 0.0;

G)

It is safe to use now, but it is not veiwable to scripts, to do so go to. void PlayerData::initPersistFields() add it to the bottom of the function

addField("SprintScale ", TypeF32, Offset(SprintScale, PlayerData));

H)

Ok, now you could initalize it with you script playerdatablock, but it isn't networked, so to conclude let hook it in start with. void PlayerData::packData(BitStream* stream) add to the bottom

stream->write(SprintScale);

I)
go to
void PlayerData::unpackData(BitStream* stream) add to the bottom

stream->read(&SprintScale);

IMPORTANT - the packing/unpacking functions require the read and writing to occur in the same order. By putting it as the last thing in the functions we insure this


Ok so now we are really close to test. Compile.

Because we made a new property we need to initalize it on the player datablock so

(player.cs)
J)

Be Sure SprintScale is set on the playerData to something like 0.3;

(default.binds.cs)
K)

Oh and besure to have the trigger being set
moveMap.bind( keyboard, x, sprint);
function sprint(%val){
   if(%val)
      $mvTriggerCount3++;
}


From here you should have some form of sprinting when you hold x.

I hope this helps
#26
10/17/2008 (8:32 am)
Sorry, guess that wasn't clear enough. What I meant was to place that echo inside of your if/else statement, like so:

if(%val)
{
      echo("should be sprinting");
      $mvSprint = true;
}
else
{
      $mvSprint = false;
}
#27
10/17/2008 (9:38 am)
@Anthony, Thank you for going into so much detail in you explanation. I'll get to it now.

@Scott, When I do that it locks the keyboard, only F-10 and F-11 working.

Thanks
#28
10/17/2008 (10:06 am)
If you followed my example then there's no reason that should be happening, unless it has exposed an error elsewhere in your code or if you copy/pasted the example it may have introduced hidden characters.

At this point you should really run step through this in the debugger in Torsion or Codeweaver, and then in Visual Studio if the error isn't found there.
#29
10/17/2008 (10:17 am)
I've actually compiled with Infinitum's files and they work for me. I don't know why it's not working for him. I'm at a loss because the resource worked when I tried it for both engines.

@Anthony: that looks like a better way of doing it than the Sprint Resource. Tighter, cleaner code. I may have to try that.
#30
10/17/2008 (11:56 am)
I'll run it through the debugger.

I did use Anthony's code and the only error I got is from the console.log (keyboard locked up).

The error is with the $mvTriggerCount3++ in the sprint function. No problem with the build.

Any thoughts?

Tony
#31
10/17/2008 (12:10 pm)
Debugger error.

Fatal: .\engine\core\bitstream.cc@194
Out of range write
#32
10/17/2008 (12:25 pm)
I would you say you have miss matched the reading/writing of network values

for instance if you put

stream->write(SprintScale);

as the first line of PlayerData::packData(BitStream* stream)

and

stream->read(&SprintScale);

as the liast line of

void PlayerData::unpackData(BitStream* stream)

The values will be out of sync


Thus I said put them BOTH as the last line of each function and put this note

IMPORTANT - the packing/unpacking functions require the read and writing to occur in the same order. By putting it as the last thing in the functions we insure this.

Besure eveything lines up,even if you have to compare both functions line by line. . . . I would encourage putting a breakpoint in both function and checking you are getting the values you expect
#33
10/17/2008 (1:18 pm)
@Anthony, thanks!

I compared the read/write functions line by line, and unless comments or gaps between lines affect them, they are in sync, and yet, I still get the bitstream error @194

194 is AssertFatal

if(bit count + bitNum > maxWriteBitNum)
{
error = true;
AssertFatal(false, "Out of range write");
return;
}

Any other ideas? Thanks!
Tony
#34
10/17/2008 (6:57 pm)
Does it happen if sprintscale is set to 0?
#35
10/18/2008 (5:00 am)
Yes, even when SprintScale is set to 0. No build errors, just when I debug.

Tony
#36
10/18/2008 (6:28 am)
Comment out the moveSpeed *= mDataBlock->SprintScale; line and see if things work. Other wise you'll just have to debug it
#37
10/18/2008 (11:29 am)
*lol* If I knew how to debug it, I'd be in pretty good shape :)

Yes, it still has a Fatal Error in bitstream@194

Thanks for trying, at least!

I'll keep at it!
Tony
#38
10/21/2008 (11:00 am)
Well, here's an interesting development. I tried putting my original files back in, did a successful build, but I still get the "out of range" error.

So I deleted the Engine folder, and put my backup in, a successful build, same error.

So I did a fresh install, and a successful build, but STILL get an out of range error whenever I try to debug???

How is that even possible? Is there a memory cache somewhere? I'll do a cold reboot and try again, but that's just weird?

Tony
#39
10/24/2008 (1:43 pm)
OK, so the error was in my Player.cs file. It had nothing to do with Anthony's code. After uninstalling, and a fresh install, I was able to add it in with no build errors and no debug errors.

Now I guess I just have to tweak the settings. I'm not noticing any effect on speed or energyDrain yet, so I'll keep adjusting the numbers.

Anthony should release this as a resource! Prarie Games Gem-of-the-day #50?

Tony
I3D
Page«First 1 2 Next»