DakiLLa Posted January 7, 2010 Share Posted January 7, 2010 hello guys, i want to make my own hud elements and i started from health bar. Well, everything is going good but i have one problem. How do i make that red line will show my current health percentage? I know, it requires some math skills but i have no any ideas... Anybody can help me? Here is a little piece of code ofcourse. addEventHandler( 'onClientRender', gRoot, function() local scX, scY = guiGetScreenSize(); local lineX = scX/1.1725; local lineEndX = scX/1.053; local lineY = scY/6.5; dxDrawLine( lineX, lineY, lineEndX, lineY, tocolor( 0, 0, 0, 255 ), 17 ); dxDrawLine( lineX+5, lineY, lineEndX-5, lineY, tocolor( 150, 0, 0, 255 ), 9 ); dxDrawLine( lineX+5, lineY, lineEndX-5, lineY, tocolor( 255, 0, 0, 255 ), 9 ); end ); So it looks like: Thx in advance Link to comment
DakiLLa Posted January 7, 2010 Author Share Posted January 7, 2010 hello guys, i want to make my own hud elements and i started from health bar. Well, everything is going good but i have one problem. How do i make that red line will show my current health percentage? I know, it requires some math skills but i have no any ideas... Anybody can help me? Here is a little piece of code ofcourse. addEventHandler( 'onClientRender', gRoot, function() local scX, scY = guiGetScreenSize(); local lineX = scX/1.1725; local lineEndX = scX/1.053; local lineY = scY/6.5; dxDrawLine( lineX, lineY, lineEndX, lineY, tocolor( 0, 0, 0, 255 ), 17 ); dxDrawLine( lineX+5, lineY, lineEndX-5, lineY, tocolor( 150, 0, 0, 255 ), 9 ); dxDrawLine( lineX+5, lineY, lineEndX-5, lineY, tocolor( 255, 0, 0, 255 ), 9 ); end); So it looks like: Thx in advance Link to comment
robhol Posted January 7, 2010 Share Posted January 7, 2010 (edited) This, to be honest, is math of the kind a 13 year old can do... "Ending point" X = Offset for X + (Max width / 100 * current health) Assuming 100 is the actual maximum, of course. Edited January 7, 2010 by Guest Link to comment
robhol Posted January 7, 2010 Share Posted January 7, 2010 (edited) This, to be honest, is math of the kind a 13 year old can do... "Ending point" X = Offset for X + (Max width / 100 * current health) Assuming 100 is the actual maximum, of course. Edited January 7, 2010 by Guest Link to comment
50p Posted January 7, 2010 Share Posted January 7, 2010 First thing you need to know is Max length of the health bar (when health is full). Then, you need to calculate what percentage of health player currently have (getElementHealth divided by max health(eg.100)). Once you get the percentage, multiply that number by max health bar length and you will get the length of the health bar to show. This is very simple math: maxBarLength = 100; maxHealth = 100; local percentage = getElementHealth( player ) / maxHealth; -- between 0 and 1 local currentBarLength = percentage * maxHealth; -- length of the bar to display Link to comment
50p Posted January 7, 2010 Share Posted January 7, 2010 First thing you need to know is Max length of the health bar (when health is full). Then, you need to calculate what percentage of health player currently have (getElementHealth divided by max health(eg.100)). Once you get the percentage, multiply that number by max health bar length and you will get the length of the health bar to show. This is very simple math: maxBarLength = 100; maxHealth = 100; local percentage = getElementHealth( player ) / maxHealth; -- between 0 and 1 local currentBarLength = percentage * maxHealth; -- length of the bar to display Link to comment
Vivalavido Posted January 7, 2010 Share Posted January 7, 2010 First thing you need to know is Max length of the health bar (when health is full). Then, you need to calculate what percentage of health player currently have (getElementHealth divided by max health(eg.100)). Once you get the percentage, multiply that number by max health bar length and you will get the length of the health bar to show.This is very simple math: maxBarLength = 100; maxHealth = 100; local percentage = getElementHealth( player ) / maxHealth; -- between 0 and 1 local currentBarLength = percentage * maxHealth; -- length of the bar to display Have you just been busy with PHP maybe? Since you use the ";" tags at the end haha. Link to comment
Vivalavido Posted January 7, 2010 Share Posted January 7, 2010 First thing you need to know is Max length of the health bar (when health is full). Then, you need to calculate what percentage of health player currently have (getElementHealth divided by max health(eg.100)). Once you get the percentage, multiply that number by max health bar length and you will get the length of the health bar to show.This is very simple math: maxBarLength = 100; maxHealth = 100; local percentage = getElementHealth( player ) / maxHealth; -- between 0 and 1 local currentBarLength = percentage * maxHealth; -- length of the bar to display Have you just been busy with PHP maybe? Since you use the ";" tags at the end haha. Link to comment
50p Posted January 7, 2010 Share Posted January 7, 2010 First thing you need to know is Max length of the health bar (when health is full). Then, you need to calculate what percentage of health player currently have (getElementHealth divided by max health(eg.100)). Once you get the percentage, multiply that number by max health bar length and you will get the length of the health bar to show.This is very simple math: maxBarLength = 100; maxHealth = 100; local percentage = getElementHealth( player ) / maxHealth; -- between 0 and 1 local currentBarLength = percentage * maxHealth; -- length of the bar to display Have you just been busy with PHP maybe? Since you use the ";" tags at the end haha. No, I've been busy with other programming languages (C#, ActionScript and MAXScript) and it's just fine to use semicolons in Lua, so I don't care Link to comment
50p Posted January 7, 2010 Share Posted January 7, 2010 First thing you need to know is Max length of the health bar (when health is full). Then, you need to calculate what percentage of health player currently have (getElementHealth divided by max health(eg.100)). Once you get the percentage, multiply that number by max health bar length and you will get the length of the health bar to show.This is very simple math: maxBarLength = 100; maxHealth = 100; local percentage = getElementHealth( player ) / maxHealth; -- between 0 and 1 local currentBarLength = percentage * maxHealth; -- length of the bar to display Have you just been busy with PHP maybe? Since you use the ";" tags at the end haha. No, I've been busy with other programming languages (C#, ActionScript and MAXScript) and it's just fine to use semicolons in Lua, so I don't care Link to comment
DakiLLa Posted January 8, 2010 Author Share Posted January 8, 2010 well, thx a lot for your help, ive resolved this problem by the next way: ive divided width of bar by 1000 and multyplied it with current health. local hp = ( hp_Width / 1000 )*getElementHealth( gMe ); Link to comment
DakiLLa Posted January 8, 2010 Author Share Posted January 8, 2010 well, thx a lot for your help, ive resolved this problem by the next way: ive divided width of bar by 1000 and multyplied it with current health. local hp = ( hp_Width / 1000 )*getElementHealth( gMe ); Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now