Have a total distance traveled constantly update?

Hello!

I originally followed this [thread][1] to be able to record the total distance traveled in game and have that working! However as of right now, it adds large integers rather than continuously counting by 1 like I want it to.

I received help from ‘mindfane’ on the forums and this was his suggestion:

“PS: If you want to increment the distance at a constant rate, the best place to do that would be in the widget’s tick or if you are using normal HUD, then within its Draw event. Instead of displaying the current value of total distance travelled, interpolate between the currently displayed value and the present value of TotalDistance travelled and display the result.”

Unfortunately, I just don’t really know how to get running what he has suggested. Below is what I started to come up with but just can’t figure out how to get the displayed TotalDistance traveled from the ‘Total Distance’ function from within the widget.

I’m not sure if I’m misunderstanding, but couldn’t you keep track of the ‘last position’ since updating and add the difference between the current position and the last to the total? (eg. totalDist = (currentPosition-lastPosition).Size())

Sorry, I forgot to include the thread so you could see the original Distance Traveled blueprint in there but I have now updated it. So far the image below is what I have for calculating the Distance Traveled.

Is your suggestion meant for the widget or just for the character blueprint itself for tracking the distance? Because I am already adding the difference between the ‘last position’ and my current position to get the distance traveled so far.

My problem is that it adds chunks of numbers rather than adding 1 at a constant rate. For example in this video.

Ah, my suggestion was simply for recording the actual distance travelled.

So from my understanding, you’re looking to display the distance travelled no more than 1m every constant update (ie. not every tick)? This would mean that the actual distance travelled might be out of sync with the displayed value.

If this is the case, then you can store the time elapsed and only update one it’s passed it (for constant rate), and only display a single meter toward the actual distance when it does update.

Off the top of my head, it might look like this:
eg. for 1 update every half second:

DisplayedMeters = 6;
ActualMetersTravelled = 10;
CurrentElapsed = 0.35s;

OnTick: 
CurrentElapsed = CurrentElapsed+Delta
if(CurrentElapsed > 0.5)
{
CurrentElapsed = 0;
DisplayedMeters  = Min(ActualMetersTravelled, DisplayedMeters +1);
}

Yes, as quickly as possible though probably through every constant update though if I could do every tick then I wouldn’t mind that. If the displayed distance is a little behind the actual distance traveled, that shouldn’t be too bad as the player wouldn’t know. Unless it really effects something then it shouldn’t be a problem.

Sorry but I somewhat understand the code, I just don’t know how this translates into Blueprint? I’m setting it up right now to the best of my ability and this is what I have so far and I don’t know if I am understanding this correctly.

The CurrentElapsed should be updated every frame with the delta, the “SetCurrentElapsed” should come before the branch in this case

CurrentElapsed = 0;
DisplayedMeters = Min(ActualMetersTravelled, DisplayedMeters +1);
}

I don’t fully understand this part or where exactly I put it? Right now the blueprint scripting I have above is in the event graph of my on screen HUD widget. I was going to place this second bit in the actual Total Distance Function I have that binds it to the on screen number.

So that bit of code is executed if the branch for (CurrentElapsed > 0.5) returns true.

What it is doing is resetting the timer to 0, so it can start it’s countdown anew (CurrentElapsed = 0), and then updating the displayed number by +1 (the min makes sure that we don’t update passed the actual meters travelled)

An alternative to this is to use a looping timer that triggers every 0.5 seconds to update (this might be easier to read, and it doesn’t require storing any values)

I haven’t tested this, but off the top of my head using a timer might look like this:

I’m curious as to where exactly that is in the blueprint? Should I be adding that to my Character Blueprint it would seem?

It could probably be anywhere that can access your HUD(maybe even in the HUD)

Sorry, I’ve kind of gotten jumbled up and lost now lol. By the way thanks for keeping track of this so quickly and answering all of my questions!

  1. I have the current elapsed setup in the HUD blueprint
  2. I now have the UpdateDisplayed setup in my HUD blueprint though I’ve had to add a Cast to (My Character blueprint) with a Get Player Character so that the Total Distance Traveled has a target.

How do these two actually interact? As of right now I don’t know how to call this so that I can update it to the HUD so it shows on screen.

By the way I replaced your ActualMetersTraveled with my Total Distance Traveled as that is connected to the blueprint in my character that is actually calculating the distance.

What I have so far:

Updated: I have to have the UpdateDisplayed setup in my character as I can’t access it from within the HUD unless it is in there. So it can’t be in the HUD itself.

Oy vey I’ve made a mess of things haha.

The UpdateDisplayed setup REPLACES the current elapsed setup.

All you should need is the blueprint pic I showed above, Call the SetTimer function whenever you want the counter to start, and to print out the “DisplayedMeters” value.

With this, every .5seconds the counter will increment by 1, until it reaches the actual meters travelled

No problem, just a little mix up on my end haha.

So I have the UpdateDisplayed setup in my Character blueprint and I have it connected to a copy of the Event Begin Play. I then am calling the DisplayedMeters in my HUD blueprint below. However the number is just staying 0 at the moment.

I’ve got it working now! I just used a sequence for having multiple inputs into the Event Begin Play then I forgot to check looping on the timer!

The only thing is that after I die, the distance I guess is still updating to see if it is the actual distance. Is there a way I could make it stop even if it hasn’t I guess technically reached the actual distance traveled?

stuck on 0