Calculate time in second

Hi.

I’m trying to calculate time in second when I press button until release to calculate player acceleration.
For that (as newbee) I decide to get DeltaTime, but realise is not good practice.
Then, I want to ask, if in UE4 present any usefull work-around to deal with time (in second / milliseconds)?

This should help you.

You can also do it by adding delta time to a variable as long as the key is held down.

1 Like

MIssed that this needs to be done in C++. I would just have a bool set to represent your key being down then add delta time to a variable in your tick until the bool is false. Then use your value for acceleration. Just make sure to reset the variable on button press.

Hello Khodan,

If you are working in Blueprints, you could use the UTCNow blueprint node or the Now blueprint node. They both return the current timestamp (date and time), one is just for UTC, the other is for Local Time. You could just grab it when a button is pressed, and on release, grab it again, to determine the time the button was held down, that you are looking for.
Then you can just subtract the two date/time structures (BP supplies a node to do this) in order to get the amount of time, that the button was held down.

If you are using C++, there are some better routines to call, in that they return a more standard view of time.

Hope this helps,

There are some routines inside of UE4 itself, that you can call directly in order to get the time. That would be a much better way I would think, instead of messing around with getting library integrated, and then having to maintain it, etc. I forget the name of the routine right now, or the include files that you need to use. But I know it’s there, it returns the time more as the standard (where 0 time = Epoch Date = 1 Jan 1970). Hence just grab it twice, on button press/release, and subtract.

yes, I’m doing it right now, but what happend if on the different PC whould be different FPS?
How I understand, acceleration would be different too. I need to link this value to the seconds instead… maybe.

Sorry, Is it any C++ code or libs? I’m really don’t understand how to read ugly BPs :frowning:

I look into C++ std libs and found chrono. Is it ok?

You can use the standard UE4 C++ code for this, and the delta time would be different at different FPS but that means your acceleration would be the same for the same amount of time the button is held, regardless of FPS.

You can to as jayice said and use UTCNow() in c++ as well

You can use UtcNow() in C++, should help you get the time

FDateTime nice decision. Thanks.

I wouldn’t be using this inside of the tick (UtcNow), trap the button press/release, such that your code is called on both, the press and release. When the button is pressed, grab the the first time stamp, then when the button is released, grab the second time stamp, subtract the two, and then do whatever you wish to do with the result.

@jayice Thanks for answer. DateTime is good for me.

@HarryHighDef

How do you think, is it good to count second button pressed?
Code in Tick() function:

if (IsMoving())
	{
		this->TimeInMotion += FDateTime::UtcNow().GetMillisecond();
	}

TimeInMotion is a my own property of Character class.

I will try. Thanks.

But one problem. I need to handle time every frame, cause my acceleration depends on that time.
And if I just tike the time when I start I don’t have a variables to calculate acceleration.

If you really want to do this in the tick, I would still grab the timestamp, so I didn’t have to accumulate a total time, then use the current delta time to do whatever you wish. As well as, either use the initial timstamp as a switch, so that you don’t need a boolean to remember, nor need to query the current state of the button.

So if the timestamp is always initialized back to a -1, when there has been no button press detected, and set to the timestamp when the button press was detected, you would have a “dual” use of the time stamp value. Then when the button is released, you grab the second time stamp, for a very accurate “duration” time of the button down, and do what you wish with that, and when done, just set the button timestamp back to -1.