Jump to content

smoothly adding money


Matevsz

Recommended Posts

Posted

Hello everyone. How can I make it add smoothly when adding an amount, e.g. $10? $1, $2, $3, $4... and not $10 right away?

hud = {}
moneyHUD = {"money"}
hud.font = dxCreateFont("font.ttf", 32.5, false, "antialiased")

addEventHandler("onClientResourceStart", getResourceRootElement, function()
	setPlayerHudComponentVisible(moneyHUD, false)
end)

addEventHandler("onClientRender", root, funtion()
  local money = getPlayerMoney(localPlayer)
  dxDrawText("$000000000", 1305, 278, 1598, 328, tocolor(255, 255, 255, 255), 1.00, hud.font, "center", "center", false, false, false, false, false)
end)

 

Posted (edited)
10 hours ago, FileEX said:

Use interpolateBetween

hud={}
moneyHUD={"money"}
hud.font = dxCreateFont("font.ttf", 32.5, false, "antialiased")

local lastMoney = 0
local moneyChangeStartTime = 0
local moneyChangeDuration = 2000

addEventHandler("onClientResourceStart", getResourceRootElement, function()
	setPlayerHudComponentVisible(moneyHUD, false)
end)


addEventHandler("onClientRender", root, function()
	local money = getPlayerMoney(localPlayer)
	money = math.min(money, 99999999)

	if money ~= lastMoney then
		lastMoney = money
		moneyChangeStartTime = getTickCount()
	end

	local elapsedTime = getTickCount() - moneyChangeStartTime

	local progress = elapsedTime / moneyChangeDuration
	local money = interpolateBetween(0, 0, 0, money, 0, 0, progress, "Linear")
    
    dxDrawText("$00000000", 1305, 278, 1598, 328, tocolor(255, 255, 255, 255), 1.00, hud.font, "center", "center", false, false, false, false, false)
    if elapsedTime >= moneyChangeDuration  then
		lastMoney = money
	end
end)

adds money smoothly, but when you add an amount greater than the maximum (99999999) it starts adding again from zero

Edited by Matevsz
  • Moderators
Posted (edited)

As you can see, you start the interpolation from 0. Write down the current balance of money before you start the interpolation and start from there.

You can use e.g. a ticks to see if the amount of money has changed

local fromMoney, toMoney, visibleMoney = 0,0,0;
local moneyCheckTick,moneyAnimTick = getTickCount();

if (moneyCheckTick and not moneyAnimTick) then
 	if (getTickCount() - moneyCheckTick > 1000) then -- check every 1 s
    	local money = getPlayerMoney(localPlayer);
    	if (visibleMoney ~= money) then
      		moneyAnimTick = getTickCount();
      		fromMoney, toMoney = visibleMoney, money;
      	end
    	moneyCheckTick = getTickCount();
   	end
 end

if (moneyAnimTick) then
	local progress = (getTickCount() - moneyAnimTick) / 1000;
  	visibleMoney = interpolateBetween(fromMoney, 0, 0, toMoney, 0, 0, progress, 'Linear');
  	
  	if (progress > 1) then
    	moneyAnimTick = nil;
    end
end

-- dxDrawText for visibleMoney variable

 

Sorry for the broken indentation in the code, but the code editor on the forum is strange and I don't know why it happens.

Edited by FileEX
Posted
18 hours ago, FileEX said:

As you can see, you start the interpolation from 0. Write down the current balance of money before you start the interpolation and start from there.

You can use e.g. a ticks to see if the amount of money has changed

local fromMoney, toMoney, visibleMoney = 0,0,0;
local moneyCheckTick,moneyAnimTick = getTickCount();

if (moneyCheckTick and not moneyAnimTick) then
 	if (getTickCount() - moneyCheckTick > 1000) then -- check every 1 s
    	local money = getPlayerMoney(localPlayer);
    	if (visibleMoney ~= money) then
      		moneyAnimTick = getTickCount();
      		fromMoney, toMoney = visibleMoney, money;
      	end
    	moneyCheckTick = getTickCount();
   	end
 end

if (moneyAnimTick) then
	local progress = (getTickCount() - moneyAnimTick) / 1000;
  	visibleMoney = interpolateBetween(fromMoney, 0, 0, toMoney, 0, 0, progress, 'Linear');
  	
  	if (progress > 1) then
    	moneyAnimTick = nil;
    end
end

-- dxDrawText for visibleMoney variable

 

Sorry for the broken indentation in the code, but the code editor on the forum is strange and I don't know why it happens.

No problem, nothing happened. 
Now it adds and subtracts money smoothly, but after exceeding the maximum amount (99999999) the amount is 100000000. I added it to the code (math.min)

  • Moderators
Posted

I don't understand what the problem is. If you exceed the maximum amount, it is normal that the number is higher

Posted
1 hour ago, FileEX said:

I don't understand what the problem is. If you exceed the maximum amount, it is normal that the number is higher

When I did not use the interpolateBetween function, after adding an amount larger than the maximum, e.g. 111111111, the amount appeared 99999999 and with the interpolateBetween function the animation ends at 100000000

  • Moderators
Posted

Sometimes interpolateBetween does some weird rounding. Try manual interpolation

money = lastMoney + (newMoney - lastMoney) * progress;

Or use what Tekken provided

Posted

I used the above examples and now in the code:

hud = {}
moneyHUD = {"money"}

hud.font = dxCreateFont("font.ttf", 32.5, false, "antialiased")

local moneyMin = 0
local moneyMax = 99999999
local currentMoney = getPlayerMoney(localPlayer)

addEventHandler("onClientResourceStart", resourceRoot, function()
    setPlayerHudComponentVisible(moneyHUD[1], false) 
end)

function lerp(a, b, t)
    return a + (b - a) * t
end

addEventHandler("onClientRender", root, function()
    local money = getPlayerMoney(localPlayer)
	money = math.max(moneyMin, math.min(moneyMax, money))
    
    currentMoney = lerp(currentMoney, money, 0.05)
    -- dxDrawText line
end)

after adding money, e.g. 25555, the player gets 25554 instead of 25555

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