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 


how do I run a script on checkbox and disable when unchecked

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Sat Jan 27, 2018 11:56 am    Post subject: Reply with quote

You'll need to set the OnChange property of the checkbox itself, two ways to do that:
1. In the form editor select the checkbox and click the "Events" tab in the Objec Inspector and give the name of the function in the OnChange edit box
2. use formname.checkboxname.OnChange = the function

Also CE 6.7 supports a disable section in a script by returning a table of information (registered symbols, allocated memory etc) that can be passed with the same script to disable it.

eg.

Code:
local disableInfo
local script = [[[ENABLE]
aobscan(color, CD CC 4C 3F B4 C8 46 3F B1 BF 3C 3F 00 00 80 3F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CD CC 4C 3F B4 C8 46 3F B1 BF 3C 3F 00 00 80 3F)
label(_color)
registersymbol(_color)

color:
_color:
db 00 00 C8 42

[DISABLE]
_color:
db CD CC 4C 3F

unregistersymbol(_color)]]

CETrainer.CECheckBox1.OnChange = function CheckBoxOnChange(sender)
  -- disable if enabled to prevent getting out of sync with gui somehow
  -- eg. CETrainer.CECheckBox1.state = 1 does not trigger OnChange due to lazarus bug
  if disableInfo then
    autoAssemble(script, disableInfo)
    disableInfo = nil
  end
  if sender.Checked then -- try to enable if asking
    local success, extra -- extra is error message on failure, disableInfo on success
    success, extra = autoAssemble(script)
    if success then disableInfo = extra
    else sender.Checked = false end
  end
end


Though if you keep it as a script in the table then you can just enable/disable it using that

Code:
local script = getAddressList().getMemoryRecordByDescription('description of script')
CETrainer.CECheckBox1.OnChange = function CheckBoxOnChange(sender)
  script.Active = sender.Checked
  if not script.Active then sender.Checked = false end -- if failed to activate keep checkbox off
end


Though, if you have any asynchronous scripts then you'd need something better to keep the actual states in sync
Code:
local script = getAddressList().getMemoryRecordByDescription('description of script')
CETrainer.CECheckBox1.OnChange = function(sender)
  script.Active = sender.Checked
  if not sender.Checked then return end -- if turning off we're done
  -- otherwise check if the script actually was enabled and update checkbox if not
  if script.Async then -- if async we can't know the result immediately so create a timer to check
    sender.state = cbGrayed -- grayout checkbox to prevent clicking a lot during checking
    local t = createTimer()
    t.Interval = 100
    t.OnTimer = function(t)
      if not script.AsyncProcessing then -- AsyncProcessing is true when an asyc script is processing
        t.destroy()
        -- don't want to get in a loop by changing the state which calls onchange which tries to change the state :)
        -- disable OnChange event by saving, setting it to nil, then resetting it
        local event = sender.OnChange
        sender.OnChange = nil
        sender.Checked = script.Active -- update gui state to match script state
        sender.OnChange = event
      --elseif script.AsyncProcessingTime > 60*1000 then -- if over a minute...
      end
    end
  elseif not script.Active then -- if not asyc we can check immediately because everything will be stalled until script is done :)
    sender.Checked = false -- if failed to activate keep checkbox off, shouldn't cause any meaningful loops since script is already off
  end
end


of course if you just want to assume success then the one line event script.Active = sender.Checked is enough Very Happy

async example
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