1. 02-05-2019, 21:59
    #1

    Junior Member
    67 • 141 • 0
    In-game: dnlk<>

    easeGameSpeed
    This function changes the players game speed step by step (that way it appears smoother)
    I'm using this in my maps a lot and in my opinion it makes a huge difference


    Difference between setGameSpeed and easeGameSpeed:



    Code:
    local ease = {}
    
    function easeGameSpeed(targetSpeed, overTime, callback)
    	ease.startedAt 		= 	getTickCount()
    	ease.startSpeed		= 	getGameSpeed()
    	ease.targetSpeed 	= 	math.round(targetSpeed, 4, "floor")
    	ease.overTime 		= 	overTime
    	ease.callback		=	(type(callback) == "function" or type(callback) == "nil") and (type(callback) == "function" and callback or nil) or nil
    	ease.isActive 		= 	true
    end
    
    addEventHandler("onClientRender", root, function()
    	if ease.isActive then
    	    local progress = (getTickCount() - ease.startedAt) / ((ease.startedAt + ease.overTime) - ease.startedAt)
    	    local currentSpeed = math.round(getGameSpeed(), 1, (ease.startSpeed > ease.targetSpeed and "ceil" or "floor"))
    
    		local X, Y, Z = interpolateBetween(currentSpeed, 0.0, 0.0, ease.targetSpeed, 0.0, 0.0, progress, "InOutQuad")
    		X = math.round(X, 4, (ease.startSpeed > ease.targetSpeed and "floor" or "ceil"))
    		if ease.startSpeed > ease.targetSpeed and X <= ease.targetSpeed then
    			X = math.round(X, 1, "ceil")
    		elseif ease.startSpeed < ease.targetSpeed and X >= ease.targetSpeed then
    			X = math.round(X, 1, "floor")
    		end
    		
    		setGameSpeed(X)
    		if X == ease.targetSpeed then
    			ease.isActive = false
    			if type(ease.callback) == "function" then
    				ease.callback();
    			end
    		end
    	end
    end)
    
    function math.round(number, decimals, method)
        decimals = decimals or 0
        local factor = 10 ^ decimals
        if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor
        else return tonumber(("%."..decimals.."f"):format(number)) end
    end
    ^ Paste this code somewhere in your script file ^






    Example Usage
    Code:
    local Marker = createMarker(X, Y, Z, "corona", SIZE, 0, 0, 0)
    
    addEventHandler("onClientMarkerHit", getRootElement(), function(playerid)
        if playerid == getLocalPlayer() then
            if isPedInVehicle(playerid) then
                local vehicleid = getPedOccupiedVehicle(playerid)
                
                if source == Marker then
                    -- Params:        (targetSpeed, overTime, callback)
                    easeGameSpeed(0.6, 1500, function()
                    	-- Target game speed reached
    
                    	-- Now you can either delay the reversal by using setTimer:
                    	-- This example waits 1500ms before it starts reversing the game speed back to 1.0 over a time of 1000ms
                    	setTimer(function()
                        	    easeGameSpeed(1.0, 1000) end, 1500, 1)
    
                    	--OR instantly start the reversal back to 1.0:
                    	easeGameSpeed(1.0, 1500)
                    end)
    
                end
            end
        end
    end)
    Last edited by dnlk; 08-05-2019 at 17:20. Reason: Function rewritten


  2. 03-05-2019, 00:38
    Junior Member
    113 • 272 • 1
    In-game: xS|#SHI!X100PRE
    Re: [FUNC] easeGameSpeed 03-05-2019, 00:38 • #2
    Why don't you just use onClientRender + interpolateBetween for even a smoother result ?
    You can start the even when the player hits the marker and stop it when the variable reaches the value.
    3 Likes


  3. 03-05-2019, 09:50
    Junior Member
    67 • 141 • 0
    In-game: dnlk<>
    Re: [FUNC] easeGameSpeed 03-05-2019, 09:50 • #3
    Originally Posted by Overkillz
    Why don't you just use onClientRender + interpolateBetween for even a smoother result ?
    You can start the even when the player hits the marker and stop it when the variable reaches the value.
    Didn't even think of onClientRender... wow.

    // Edit

    Updated the post with the rewritten function, now using onClientRender & interpolateBetween
    Thanks @Overkillz for that suggestion
    Last edited by dnlk; 03-05-2019 at 11:34.
    1 Like


  4. 03-05-2019, 15:13
    Premium User
    24 • 180 • 5
    In-game: Jimmeh1998
    Re: [FUNC] easeGameSpeed 03-05-2019, 15:13 • #4
    easeGameSpeed much better! Nice
    1 Like


  5. 04-05-2019, 20:46
    Junior Member
    67 • 141 • 0
    In-game: dnlk<>
    Re: [FUNC] easeGameSpeed 04-05-2019, 20:46 • #5
    I've updated the code once again because apparently there was a small issue when using a low overTime value (like ~500ms), which would sometimes lead to the ease process not ending cuz it, why the fuck ever, could overshoot sometimes.


  6. 08-05-2019, 04:36
    Junior Member
    26 • 34 • 0
    In-game: Overlord
    Re: [FUNC] easeGameSpeed 08-05-2019, 04:36 • #6
    Pretty useful, thanks for sharing
    1 Like


  7. 08-05-2019, 11:50
    Junior Member
    121 • 510 • 1
    In-game: lv*airplaNe
    Re: [FUNC] easeGameSpeed 08-05-2019, 11:50 • #7
    1 Like


  8. 08-05-2019, 12:36
    WFF Squad

    365 • 3,068 • 3
    In-game: -ffs-Eyecache
    Re: [FUNC] easeGameSpeed 08-05-2019, 12:36 • #8
    Originally Posted by airplaNe
    Omg :3
    In the right words order so powerful are.
    3 Likes

1 Users reading this thread (0 Members and 1 Guests)