Jump to content

How to freeze getTickCount() inside render function and then continue from the frozen time


Dzsozi (h03)

Recommended Posts

Hello!

The title almost explains everything I want to do. I can't get my script working, I would like to freeze/get a specific time whenever a vehicle stops, and when it starts again continue counting back from that time. Something like storing the "time" whenever a vehicle stops and when it starts moving again start from where it stopped. Here's my partial function:

if isVehicleOnGround(vehicle) then
	if getVehicleSpeed(vehicle) >= 10 then
		currentTime = -- return from the stored time/tick count, how can I do that?!?!?

		if currentTime - lastTimeAdded[vehicle] >= 5000 then
			if someVariable < #someTable then
				someVariable = someVariable+1
				lastTimeAdded[vehicle] = getTickCount()
			else
				someVariable = #someTable
			end
		end
	else
		-- how can i store and freeze currentTime variable? it is a getTickCount(), how can I store and then return it above?????
	end
end

This is a kind of timer, since I am using onClientRender I thought that this would be the most efficient, also, setTimer functions look ugly to me so I didn't even try them.

Is there any other way to do this inside an onClientRender function? I am cracking my head over this since like 2 days but can't get the result I want. How can I do that? Here's an example:

Let's say I go forward with the vehicle and the currentTime variable starts counting from 0, when the counter is at 2500 (milliseconds) I stop with the vehicle. I want the timer to stay 2500 and then when I move again start the timer from 2500 until it reaches 5000.

Hope you understand my problem!

Thank you for your reply in advance!

Edited by Dzsozi (h03)
Link to comment
  • Moderators
local lastCheckTime


-- function ()
-- ...

if isVehicleOnGround(vehicle) then
	if getVehicleSpeed(vehicle) >= 10 then
		currentTime = lastCheckTime or getTickCount()-- if lastCheckTime is nil, then use the current tickCount

		if currentTime - lastTimeAdded[vehicle] >= 5000 then
			if someVariable < #someTable then
				someVariable = someVariable+1
				lastTimeAdded[vehicle] = getTickCount()
			else
				someVariable = #someTable
			end
		end
	else
		lastCheckTime = getTickCount()
	end
end

-- ...
-- end function

 

Link to comment
33 minutes ago, IIYAMA said:

local lastCheckTime


-- function ()
-- ...

if isVehicleOnGround(vehicle) then
	if getVehicleSpeed(vehicle) >= 10 then
		currentTime = lastCheckTime or getTickCount()-- if lastCheckTime is nil, then use the current tickCount

		if currentTime - lastTimeAdded[vehicle] >= 5000 then
			if someVariable < #someTable then
				someVariable = someVariable+1
				lastTimeAdded[vehicle] = getTickCount()
			else
				someVariable = #someTable
			end
		end
	else
		lastCheckTime = getTickCount()
	end
end

-- ...
-- end function

 

This does not function the way I would like it to be.

Link to comment
  • Moderators
-- clear those once you stop the timer it.
local lastCheckTime
local newStartTime
--

-- function ()
-- ...

if isVehicleOnGround(vehicle) then
	if getVehicleSpeed(vehicle) >= 10 then
    	local currentTime
    	if lastCheckTime then
      		currentTime = lastCheckTime
			if not newStartTime then
				newStartTime = getTickCount()
			else
				currentTime = currentTime + (getTickCount() - newStartTime)
			end
		else
      		currentTime = getTickCount()
		end
		if currentTime - lastTimeAdded[vehicle] >= 5000 then
			if someVariable < #someTable then
				someVariable = someVariable+1
				lastTimeAdded[vehicle] = getTickCount()
			else
				someVariable = #someTable
			end
		end
	else
		lastCheckTime = getTickCount()
	end
end

-- ...
-- end function

 

Link to comment

Thank you for your reply, but this doesn't work as well. I tried using outputChatBox to see the tick count, and I get a wrong tick number and also, the process doesn't start from the tick where it was paused. Is there any other way to make something similar? Or is there any solution for this one?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...