Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


ProgressBar as a health indicator

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Johnden
Cheater
Reputation: 0

Joined: 30 Jan 2018
Posts: 25

PostPosted: Tue Jan 30, 2018 10:33 am    Post subject: ProgressBar as a health indicator Reply with quote

I'm trying to make health indicators for an RPG game and so far I've made 3 labels showing HP values using Dark Byte's code.

Code:
function CETimer1Timer(sender)
local memrec=addresslist_getMemoryRecordByDescription(getAddressList(), "Enemy 1")
local value=memoryrecord_getValue(memrec)
control_setCaption(CETrainer_CELabel1, value)
end

function CETimer2Timer(sender)
local memrec=addresslist_getMemoryRecordByDescription(getAddressList(), "Enemy 2")
local value=memoryrecord_getValue(memrec)
control_setCaption(CETrainer_CELabel2, value)
end

function CETimer3Timer(sender)
local memrec=addresslist_getMemoryRecordByDescription(getAddressList(), "Enemy 3")
local value=memoryrecord_getValue(memrec)
control_setCaption(CETrainer_CELabel3, value)
end


Is there a way to do the same thing but with progress bars?
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Tue Jan 30, 2018 10:53 am    Post subject: Reply with quote

sure, it's not much different

Code:
function CETimer1Timer(sender)
  local memrec=addresslist_getMemoryRecordByDescription(getAddressList(), "Enemy 1")
  local value=memoryrecord_getValue(memrec)
  --progressbar_setMin(CETrainer_CEProgressbar1, 0) -- defaults, but I wanted to show them in case max is 1000 or something
  --progressbar_setMax(CETrainer_CEProgressbar1, 100)
  progressbar_setPosition(CETrainer_CEProgressbar1,value)
end
Back to top
View user's profile Send private message
Johnden
Cheater
Reputation: 0

Joined: 30 Jan 2018
Posts: 25

PostPosted: Tue Jan 30, 2018 1:25 pm    Post subject: Reply with quote

Every enemy has different max HP. So I need to change the max every time I encounter an enemy. Value = ?? if there are no encounters.
I need to also make a condition such that if the progress goes below 50% for example it changes color.
Lua is new to me, so sorry if I ask too much.
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Tue Jan 30, 2018 4:24 pm    Post subject: Reply with quote

I don't believe changing the color is supported... though perhaps there's an undocumented way that someone else knows.

The value being ??? shouldn't matter, at least in my testing it should be treated the same as being the minimum value.

And the max isn't a problem unless you have no way to read read it, otherwise you just set it like the value (just a different function call). If you don't have a way to read it then you either have to guess some max value that'll hopefully be reasonable or not use a progress bar.


well, I ended up essentially creating my own progressbar using a Panel object lol Obviously it's much more complex code because I'm doing most of the work myself now but...

https://github.com/FreeER/CE-Examples/blob/master/custom%20progress%20bar.CT (may be updated at some point)


Code:
CustomProgressBar = function(pb, value, min, max)
  value = tonumber(value) -- make sure it's a number before doing math
  if not value then return end
  -- custom functions used
  local function clamp(value,min,max) return math.max(math.min(value,max),min) end
  local function lerp(a,b,t) return math.floor(a+(b-a)*t) end
  local function lerpRGB(r,g,b,r2,g2,b2, t)
    return lerp(r,r2,t), lerp(g,g2,t), lerp(b,b2,t)
  end
  local function makeColor(r,g,b) return r | (g<<8) | (b<<16) end
  local function ColorLuminance(r,g,b, lum) -- https://www.sitepoint.com/javascript-generate-lighter-darker-color/
    local function applyLum(c,l) return math.round(math.min(math.max(0, c + (c * l)), 255)) end
    return applyLum(r,lum), applyLum(g,lum), applyLum(b,lum)
  end

  -- if outside expected range color math will do unexpected things :)
  value = clamp(value,min,max)

  -- fill background with some default color
  pb.Canvas.Brush.Color = makeColor(0,0,0)
  pb.Canvas.fillRect(0,0,pb.Width, pb.Height)

  -- determine percent to fill
  local percent = (value-min) / (max-min)

  -- set color via lerping ("linear interpolation")
  -- simple red to green doesn't look good ~50% so use yellow to green and red to yellow
  -- https://answers.unity.com/questions/646272/lerp-between-3-colors-based-on-energy-amount.html
  if percent > 0.5 then
    pb.Canvas.Brush.Color = makeColor(lerpRGB(255,255,0, 0,255,0, (percent-0.5)*2))
  else
    pb.Canvas.Brush.Color = makeColor(lerpRGB(255,0,0, 255,255,0, percent*2))
  end

  -- fill "bar" based on percent
  pb.Canvas.fillRect(0,0,pb.Width * percent, pb.Height)

  -- set text style for writing health value
  pb.Canvas.Brush.Style = 1 -- bsClear, no background color, alternatively set your favorite color
  pb.Canvas.Font.Color = makeColor(255,0,0)
  pb.Canvas.Font.Size = 16

  -- format the number so you dn't get 97.5648316878 or something lol
  local svalue = ('%1.2f'):format(value)
  local width = pb.Canvas.getTextWidth(svalue)
  local height = pb.Canvas.getTextHeight(svalue)
  local centerX = (pb.Width-width)/2
  local centerY = (pb.Height-height)/2-2 -- slight adjustment for personal preference
  -- write value centered
  pb.Canvas.textOut(centerX, centerY, svalue)
end

function CETimer1Timer(sender)
  local memrec=addresslist_getMemoryRecordByDescription(getAddressList(), "Enemy 1")
  local value=memoryrecord_getValue(memrec)
  local memrec2=addresslist_getMemoryRecordByDescription(getAddressList(), "Enemy 2")
  local value2=memoryrecord_getValue(memrec2)
  --progressbar_setMin(CETrainer_CEProgressbar1, 0) -- defaults, but I wanted to show them in case max is 1000 or something
  --progressbar_setMax(CETrainer_CEProgressbar1, 100)
  progressbar_setPosition(CETrainer_CEProgressbar1,value)

  -- .OnPaint exists but does not seem to be called automatically as I'd expect.... maybe a bug?
  -- but that's ok, if we're calling it ourselves then we can pass whatever we want
  -- instead of having to get the values inside the drawing function itself
  CustomProgressBar(CETrainer_CEPanel1, value, 0, 4000)
  CustomProgressBar(CETrainer_CEPanel2, value2, 0, 100)
end


edit: you could also do this with images rather than a panel's canvas. The concept is much the same however, you'd just only change the image's width rather than draw things.


Last edited by FreeER on Wed Jan 31, 2018 11:46 am; edited 1 time in total
Back to top
View user's profile Send private message
Johnden
Cheater
Reputation: 0

Joined: 30 Jan 2018
Posts: 25

PostPosted: Wed Jan 31, 2018 10:08 am    Post subject: Reply with quote

Thank you I'll test it out
Back to top
View user's profile Send private message
Sondio
Newbie cheater
Reputation: 1

Joined: 07 Mar 2019
Posts: 18

PostPosted: Wed Jul 03, 2019 7:06 pm    Post subject: This post has 1 review(s) Reply with quote

hey there,

my try :

BASED ON FreeER SCRIPT/CODE

mega.nz/#!MB9jASST!pa4lKy3ep5veVA2bgSiQn9rAv3U6wlWJ3Xdea5LQFt8

add h-t-t-p-s:// at start of the link without '-'

Cool enjoy with your mind Cool

edit:

#-INFO:
this script is to quickly create the customized progress bar;
for first step insert pb(top, left, height, width);
the progressbar showed is for example

#-MODES
0) random rgb
1) 1 color
2) 2 colors
3) 3 colors
4) 7 colors (rainbow?)

#-TEXT
show/hide percentage



snap.png
 Description:
 Filesize:  26.32 KB
 Viewed:  6781 Time(s)

snap.png



_________________
DaSpamer
I am the leader of the lazy and with the books I made filters !!!Cool


Last edited by Sondio on Thu Jul 04, 2019 10:08 am; edited 2 times in total
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Thu Jul 04, 2019 6:41 am    Post subject: Reply with quote

I just use a simple code for custom progress bar with the color changed.

Code:
frm=createForm()
pbar1=createPanel(frm)
pbar1.setPosition(10,10)
pbar1.Font.Size = 16
pbar1.Visible=false

local pmax,step = 300,10

function setpb()
 pbar1.Visible=true
 pbar1.width = pmax
 pbar1.caption = pmax
 pmax = pmax - step -- can use (Maxhp - damage), etc

 if pmax < 0 then pbtimer.enabled = false showMessage('You are dead') end

 pbar1.width = pmax
 pbar1.caption = pmax

 if pmax < 300 and pmax > 200 then
  pbar1.color = '25600'
 elseif pmax <= 200 and pmax > 100 then
  pbar1.color = '4033390'
 elseif pmax <= 100 then
  pbar1.color = '10210715'
 end

end

pbtimer = createTimer(frm)
pbtimer.Interval = 500
pbtimer.enabled=false
pbtimer.OnTimer = setpb

function start()
 pbtimer.enabled=true
end

frm.show()
frm.onDblClick=start

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites