Jump to content

Splitting an elapsed time


jkub

Recommended Posts

Hy I have made a little script of a drag strip at the los santos airport and when U start it gets your time lapsed when u reach the end of the track and I use getTickCount() to do this at both the starting point and end point to do this. But the time elapsed will look like this. So for example if the time was 20.14 seconds it will say 20147 Is there a way I can get rid of that last number from the result and also is there a way I could put a dot after the first 2 numbers so it is easily readable as seconds like 20.14 instead of 20147 ? How do I do this

Link to comment

Also I just figured out that after you complete one run on the track and do another one the time does not reset and Instead accumulates after each run making each finish time larger. so How do I reset the time after each run?

Link to comment

To get results like 20.14 you divide the 20147 by 1000 (1 sec == 1000ms.) To get rid of the last digit you can use string.format() function, like:

time = RUN_TIME / 1000; -- eg. 20147 / 1000 
numberStr = string.format( "%.2f", time ); -- 20.15 -- notice 5, it round up the last digit (7 >= 5) 

Also, if you made the script then you should know where the player starts the run.

Link to comment

Ya I have tickStart = getTickCount() as soon as the player hits the beginning collision and the next tickEnd = getTickCount() is triggered when they hit the finish line collision I have set up and it works and the split thing u suggested works too:) Thanks but it Still is stacking up times and accumulating? How do I reset or perhapse destroy the first time?

Link to comment

Post some code and we may find out what's wrong. You need to have that tickStart "global" (without local when declaring it or with local but at the top of the file) so when you assign new value to that variable the old one gets deleted. If you have tickStart (as global) and a new variable in a function than things gets messed up and the global tickStart wouldn't be even used in that function...

As I said, post some code and we'll see what's wrong.

Link to comment

Here is some code im using:)

  
function trackStart ( thePlayer, matchingDimension ) 
    if source == raceTrackStagingColCube then 
        if getElementData ( thePlayer, "usingtrack", true ) then 
            if isPlayerInVehicle ( thePlayer ) then 
                local veh = getPlayerOccupiedVehicle ( thePlayer ) 
                local driver = getVehicleController ( veh ) 
                if driver then 
                    setVehicleFrozen ( veh, true ) 
                    setVehicleEngineState ( veh, false ) 
                    setTimer ( setVehicleEngineState, 5000, 1, veh, true ) 
                    setTimer ( setVehicleFrozen, 5000, 1, veh, false ) 
                    finishCol = createColCuboid ( 2051.1867, -2511.3623, 12.5, 10, 31.8, 3 )  
                    countdown () 
                end 
            else 
                outputChatBox ( "You are not running this one on foot. Get a car", thePlayer, 255, 0, 0 ) 
            end 
        else 
            outputChatBox ( "You do not have track using hax", thePlayer, 255, 0, 0 ) 
        end 
    end 
end 
  
function finish ( thePlayer, matchingDimension ) 
    if source == finishCol then 
        if isPlayerInVehicle ( thePlayer ) then 
            local veh = getPlayerOccupiedVehicle ( thePlayer ) 
            local driver = getVehicleController ( veh ) 
            if driver then 
                if getElementData ( thePlayer, "usingtrack", true ) then 
                    destroyElement ( finishCol ) 
                    removeElementData ( thePlayer, "usingtrack" ) 
                    removeElementData ( raceTrackColCube, "use" ) 
                    tickEnd = getTickCount() 
                    time = ( tickEnd - tickStart ) 
                    finalTime = ( time / 1000 ) 
                    numberStr = string.format( "%.2f", finalTime );  
                    outputChatBox ( "Run Complete. Your time was #FFFF00 " ..numberStr, thePlayer, 255, 255, 255, true ) 
                end 
            end 
        end 
    end 
end 
   
function time ( ) 
    tickStart = getTickCount() 
end 

Link to comment

You have to reset the tickStart every time player hits the start collision shape. In your code I don't see any line responsible for that.

trackStart seems to be a function that is called every time player start the track, but you never reset the tickStart, don't wonder why it's stacking up.

Also, in finish function you have variable called "time" which is also a function that you're meant to use to reset the time. When you make a "global" variable with the same name as a function, the function will no longer exist and never used.

Link to comment

How would I go about displaying a speed in mph. For instance 280mph. I pretty much used the example from the getElementVelocity part on the wiki and how would I format that long result to get 280 instead of 2.874572305969 blah blah blah

Link to comment

Didnt see anything there that would apply. I already got the speed but its in the wrong format ex: 128mph 1.28458259blahblah

I figured that string.format function could somehow work this out how would it be done through there if not then?

Link to comment

Ive been working on the same script for a while now the drag track im doing this for. anyway At the moment the script is solid in most areas and there are no small bugs that I know of. But there is a critical bug thats gonna stop me from ever legitly running it on my server. And that is somewhere on the trackstart function. Sometimes but not everytime when a vehicle pulls up to the startline it will comepletly crash the server and I have to restart it. Im pretty sure it is the script thats causing it to crash because It happens only when I pull up to the start but as I said not always. I would Really like to get this on its feet. Do u know of anything that I might be doing wrong for it do do this. The only thing I could see at the moment is that i am using alot of arguments on one function. and alot of setTimer arguments. But I don't really know if that would cause the prob

here is the trackstart part of the script again I have now

function trackStart ( thePlayer, matchingDimension ) 
    if source == raceTrackStagingColCube then 
        if getElementData ( thePlayer, "usingtrack", true ) then 
            if isPlayerInVehicle ( thePlayer ) then 
                local veh = getPlayerOccupiedVehicle ( thePlayer ) 
                local driver = getVehicleController ( veh ) 
                if driver then 
                    vehx, vehy, vehz = getElementPosition ( veh ) 
                    ------------------------------------------------------------ 
                    redLight = createMarker ( 1498.9, -2492.93, 18.6, "corona", 0.3, 255, 0, 0, 255, getRootElement() ) 
                    yellowLight1 = createMarker ( 1498.9, -2493.23, 18.05, "corona", 0.3, 255, 255, 0, 0, getRootElement() ) 
                    yellowLight2 = createMarker ( 1498.9, -2492.6, 18.05, "corona", 0.3, 255, 255, 0, 0, getRootElement() ) 
                    greenLight1 = createMarker ( 1498.9, -2492.6, 17.5, "corona", 0.3, 0, 255, 0, 0, getRootElement() ) 
                    greenLight2 = createMarker ( 1498.9, -2493.23, 17.5, "corona", 0.3, 0, 255, 0, 0, getRootElement() ) 
                        ----- 
                        triggerLights () 
                            ----- 
                            destroyLights () 
                    ------------------------------------------------------------ 
                                setVehicleFrozen ( veh, true ) 
                                setVehicleEngineState ( veh, false ) 
                                setVehicleRotation ( veh, 0, 0, -90 ) 
                                setElementPosition ( veh, 1471.8747, vehy, vehz ) 
                                setTimer ( setVehicleEngineState, 5000, 1, veh, true ) 
                                setTimer ( setVehicleFrozen, 5000, 1, veh, false ) 
                                    moveObject ( endBarrier1, 3000, 2115.0461, -2480.4794, 11.2991 ) 
                                    moveObject ( endBarrier2, 3000, 2115.0461, -2489.8593, 11.2991 ) 
                                    moveObject ( endBarrier3, 3000, 2115.0461, -2499.2392, 11.2991 ) 
                                    moveObject ( endBarrier4, 3000, 2115.0461, -2508.6191, 11.2991 ) 
                                    finishCol = createColCuboid ( 2051.1867, -2511.3623, 12.5, 25, 31.8, 5 ) 
                                    setTimer ( beginTimeHax, 5000, 1 ) 
                end 
            else 
                outputChatBox ( "You are not running this one on foot. Get a car", thePlayer, 255, 0, 0 ) 
            end 
        else 
            outputChatBox ( "You do not have track using hax", thePlayer, 255, 0, 0 ) 
        end 
    end 
end 
  
function triggerLights () 
    setTimer ( setMarkerColor, 4000, 1, redLight, 255, 0, 0, 0 ) 
    setTimer ( setMarkerColor, 4000, 1, yellowLight1, 255, 255, 0, 255 ) 
    setTimer ( setMarkerColor, 4000, 1, yellowLight2, 255, 255, 0, 255 ) 
    setTimer ( setMarkerColor, 5000, 1, yellowLight1, 255, 255, 0, 0 ) 
    setTimer ( setMarkerColor, 5000, 1, yellowLight2, 255, 255, 0, 0 ) 
    setTimer ( setMarkerColor, 5000, 1, greenLight1, 0, 255, 0, 255 ) 
    setTimer ( setMarkerColor, 5000, 1, greenLight2, 0, 255, 0, 255 ) 
end 
  
function destroyLights () 
    setTimer ( destroyElement, 10000, 1, redLight ) 
    setTimer ( destroyElement, 10000, 1, yellowLight1 ) 
    setTimer ( destroyElement, 10000, 1, yellowLight2 ) 
    setTimer ( destroyElement, 10000, 1, greenLight1 ) 
    setTimer ( destroyElement, 10000, 1, greenLight2 ) 
end 

I understand some of the way this code is written may be a little ludacris or somthin but any thoughs on how to solve this problem?

Link to comment

Ya thats what I though it would of been. I went ahead and tried spacing the timers out overa longer time like 8 seconds and Ive repeatedly stress tested the new code with the lights over a longer time and more organized so It would give the server room to breathe and It hasent crashed yet:) Thanks I hope this is fulproof. Im gonna work on reaction time now on the launch. Its all good so far:)

Link to comment

I got the reaction time going good. The only problem is if u launch before the go time it will either give not time at all or give a crazy time. Like I went 1.05 seconds before the starting time and it would say somthing like "Reaction Time: 12.48" other then that it works smooth. can u help please?

Link to comment

It only shows weird time when you start before the start? If so, try not to show that time at all or you should work out the difference between "when it's meant to start" and "when you started", eg.:

-- this somewhere in the trackStart function 
colHitTime = getTickCount() 
  
--this somewhere where you launch the car 
local diff = getTickCount() - ( colHitTime + 6000 ) -- this is difference between car's start and the end of the countdown 
outputChatBox( "*Reaction time: " .. tostring( diff ) ) 

With this code you should see negative value if you launch before you're allowed to and positive if you launched after the green light... Notice the 6000 I wrote there. It's the time of red and yellow lights (in total). For instance you have red light for 3000ms and yellow light for 3000ms (3000+3000=6000)... You have to add those 6000ms to see if player accelerated before the countdown reached 0 (green light).

I don't know if that's your problem... if not, explain a bit more and paste some code.

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...