Jump to content

dxDrawRectangle question


TrickyTommy

Recommended Posts

Posted

Hi. I want to make a rectangle, that displays the whole amount of ammunition you have, but if i have more ammo, than the rectangle's width, it will stick out from it's border. Here, take  a look:

FjgE1al.png

I want it to be within it's black border. As you can see, it is sticking out.

Code:

 

local MaxClipAmmo = getWeaponProperty (tonumber(getPedWeapon(localPlayer)), "std", "maximum_clip_ammo")
local AmmoInClip = getPedAmmoInClip(localPlayer)
local AllAmmo = getPedTotalAmmo (localPlayer)

local FullAmmoBar = dxDrawRectangle (1701/x*jx, 65/y*jy, AllAmmo/x*jx, 8/y*jy, tocolor(227, 160, 93, 255))

x is my resolution = 1920, 1080
and jx is player's resolutiion.
so the rectangles will be the same on each monitor.

  • Moderators
Posted (edited)
local maxAmmo = 300
math.min(AllAmmo, maxAmmo)/x*jx 

 

Edited by IIYAMA

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • Moderators
Posted

show updated code.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted (edited)
local MaxClipAmmo = getWeaponProperty (tonumber(getPedWeapon(localPlayer)), "std", "maximum_clip_ammo")
local AmmoInClip = getPedAmmoInClip(localPlayer)
local AllAmmo = getPedTotalAmmo (localPlayer)
local maxAmmo = 300

local FullAmmoBar = dxDrawRectangle (1701/x*jx, 65/y*jy, math.min(AllAmmo, maxAmmo)/x*jx, 8/y*jy, tocolor(227, 160, 93, 255))

 

Edited by TrickyTommy
  • Moderators
Posted

Try this.

local maxAmmo = 1000

dxDrawRectangle (1701/x*jx, 65/y*jy, (math.min(AllAmmo, maxAmmo) / maxAmmo * 100)/x*jx, 8/y*jy, tocolor(227, 160, 93, 255))
-- this is useless unless you know what you are doing: local FullAmmoBar = 

 

The bar length is based on the value 100? Else you have to change the value 100 to the correct value.

 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • Moderators
Posted

Because you have to adjust the max value for that.

 

You could try something like this:

local maxAmmoTable = {}

-- function
-- ...
local weapon = getPedWeapon(localPlayer)

local maxAmmo = maxAmmoTable[weapon] or 1


if AllAmmo > maxAmmo then
	maxAmmoTable[weapon] = AllAmmo
end

-- ...
-- end

-- reset
addEventHandler("onClientPlayerWasted", localPlayer,
function ()
	maxAmmoTable = {}
end)

 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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