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 


Do something if value changed

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

Joined: 22 Jun 2021
Posts: 489
Location: mov dword ptr [Ukraine]

PostPosted: Wed Aug 25, 2021 3:54 am    Post subject: Do something if value changed Reply with quote

i've tried to make lua script based on value that changing, but if value is 1 script always adds 5 to other value
_________________
void(__cdecl *Haxing)(HWND hGameWindow)
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1055
Location: 0x90

PostPosted: Wed Aug 25, 2021 5:42 am    Post subject: Reply with quote

I think you will have to use either a timer or a thread to check if the value has changed and if so, follow on with what you want to do with the value.
Back to top
View user's profile Send private message
Frouk
Master Cheater
Reputation: 5

Joined: 22 Jun 2021
Posts: 489
Location: mov dword ptr [Ukraine]

PostPosted: Wed Aug 25, 2021 6:05 am    Post subject: Reply with quote

LeFiXER wrote:
I think you will have to use either a timer or a thread to check if the value has changed and if so, follow on with what you want to do with the value.

I've used timer and thread, but maybe i'm coding wrong?

_________________
void(__cdecl *Haxing)(HWND hGameWindow)
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1055
Location: 0x90

PostPosted: Wed Aug 25, 2021 6:30 am    Post subject: Reply with quote

Show us what you have Smile Also, you will only require one or the other (timer/thread)
Back to top
View user's profile Send private message
Birdi
Expert Cheater
Reputation: 0

Joined: 08 Jun 2020
Posts: 122
Location: Migrating

PostPosted: Wed Aug 25, 2021 6:48 am    Post subject: Reply with quote

Code:

if syntaxcheck then return end

ExitTimer = true
oldValue = nil
myAddress = "This Description" --Description of the memrec you want to check.. you can use something other than a memrec's .Description if you want, however you find it is up to you.

function checkValue()
  local al = getAddressList()
  if oldValue == nil then  --first execution, no "Old Value" yet
    for i=0,al.Count-1 do
      if al[i].Description == myAddress then --See if any of all memrecs match a given description
        oldValue = al[i].Value --Since it's the first run through, define the old value automatically
      end
    end
  else
    for i=0,al.Count-1 do
      if al[i].Description == myAddress then --Check all memrecs' descriptions for the one you want
        if al[i].Value ~= oldValue then --If current value is not equal to the old one...
           --Your Code Here
           print("Changed Value!!!\n   Old Value: "..oldValue.."\n   New Value: "..al[i].Value)
           oldValue = al[i].Value --Make sure you change the old value to the current one
        end
      end
    end
  end
  if ExitTimer == true then
    cycleTimer.destroy() --Destroy timer
    --You can reset oldValue here if you need to, otherwise it'll hold the Old Value from when it was destroyed
  end
end


Main script above, here's a toggle script:

Code:

[Enable]
{$lua}
--ExitTimer = !ExitTimer? Idk the syntax lol
--memrec.Active = false would be nice if it worked, so you wouldn't need the Disable section. Maybe somebody knows how to auto-disable enabled scripts.
if ExitTimer == false then                      -- Stop Timer
  ExitTimer = true                              -- Change state
  memrec.Description = "Start Timer"
else                                            -- Start Timer
  ExitTimer = false                             -- Change state
  cycleTimer = createTimer(MainForm, false)     -- Create Timer
  cycleTimer.Interval = 10                      -- ...
  cycleTimer.OnTimer = checkValue               -- ...
  cycleTimer.Enabled = true                     -- ...
  memrec.Description = "Stop Timer"
end
{$asm}
[Disable]
{$lua}
if ExitTimer == false then                      -- Stop Timer
  ExitTimer = true                              -- Change state
  memrec.Description = "Start Timer"
else                                            -- Start Timer
  ExitTimer = false                             -- Change state
  cycleTimer = createTimer(MainForm, false)     -- Create Timer
  cycleTimer.Interval = 10                      -- ...
  cycleTimer.OnTimer = checkValue               -- ...
  cycleTimer.Enabled = true                     -- ...
  memrec.Description = "Stop Timer"
end
{$asm}

_________________
Trying to learn!

Add me on Discord if you want hands-on help: Birdi#0007
Back to top
View user's profile Send private message Visit poster's website
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1055
Location: 0x90

PostPosted: Wed Aug 25, 2021 7:46 am    Post subject: Reply with quote

Birdi wrote:

--memrec.Active = false would be nice if it worked, so you wouldn't need the Disable section. Maybe somebody knows how to auto-disable enabled scripts.


You can set a timer to disable the script after a set time

Code:

autoDisableTimer = createTimer(getMainForm())
autoDisableTimer.Interval = 500
autoDisableTimer.OnTimer = function(autoDisableTimer)
                        local al = getAddressList()
                           local mr = al.getMemoryRecordByDescription('description')
                           if mr ~= nil and mr.Active == true then
                               mr.Active = false
                                memrec.Color = 0x000000FF
                               autoDisableTimer.destroy()
                           end
                        end


Another way would be to add this code to the Lua section of the table:
Code:

function getMemRec(s)
   local al = getAddressList()
   local s = al.getMemoryRecordByDescription(tostring(s))
   if s ~= nil then
      return s
   else
      error(showMessage("Couldn't find the script :("))
   end
end

function toggleScript(scr)
   local x = getMemRec(scr)
   if x ~= nil then
      x.Active = not x.Active
      if x.Active == true then
          -- Set the enabled colour here, this is a medium-green
         x.Color = 0x00AA00
      else
          -- Set the disabled colour here, this is a medium-red
         x.Color = 0x0000AA
      end
   end
end


Then in any script you wish to toggle you can use this:
Code:

[ENABLE]
...
{$LUA}
toggleScript('description of the script you wish to toggle here')
{$ASM}
[DISABLE]
...
{$LUA}
toggleScript('You get the idea')
Back to top
View user's profile Send private message
Birdi
Expert Cheater
Reputation: 0

Joined: 08 Jun 2020
Posts: 122
Location: Migrating

PostPosted: Wed Aug 25, 2021 8:17 am    Post subject: Reply with quote

LeFiXER wrote:

...
Then in any script you wish to toggle you can use this:
Code:

[ENABLE]
...
{$LUA}
toggleScript('description of the script you wish to toggle here')
{$ASM}
[DISABLE]
...
{$LUA}
toggleScript('You get the idea')


Does that still function if you just use toggleScript(memrec), as it should target itself? Assuming it isn't looking at the description but you can change the function to just check memrec to memrec; wondering if it'll auto-disable itself though, and why memrec.Active = false doesn't work.. activation takes priority over setting its own flag? /shrug

_________________
Trying to learn!

Add me on Discord if you want hands-on help: Birdi#0007
Back to top
View user's profile Send private message Visit poster's website
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1055
Location: 0x90

PostPosted: Wed Aug 25, 2021 8:41 am    Post subject: Reply with quote

ToggleScript basically sets a specified script to disabled if it is enabled already, or, if the script is disabled then set it to enabled.

Say you have a script you have enabled but you want to disable it after enabling. You can just add toggleScript to the end of the enable section of the script like so:

Code:

[ENABLE]
...
// enabled script stuff here
{$LUA}
toggleScript('my test script')
-- This will disable the script after the initial ENABLE section script stuff has finished.
{$ASM}
[DISABLE]
....
// disable script stuff here
Back to top
View user's profile Send private message
Birdi
Expert Cheater
Reputation: 0

Joined: 08 Jun 2020
Posts: 122
Location: Migrating

PostPosted: Wed Aug 25, 2021 8:43 am    Post subject: Reply with quote

Thanks for the info LeFiXER, I'll try and keep it in mind. Also sorry for kinda hijacking the thread there lol
_________________
Trying to learn!

Add me on Discord if you want hands-on help: Birdi#0007
Back to top
View user's profile Send private message Visit poster's website
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1055
Location: 0x90

PostPosted: Wed Aug 25, 2021 1:14 pm    Post subject: Reply with quote

No problem and you didn't Smile.
Back to top
View user's profile Send private message
Frouk
Master Cheater
Reputation: 5

Joined: 22 Jun 2021
Posts: 489
Location: mov dword ptr [Ukraine]

PostPosted: Wed Aug 25, 2021 2:25 pm    Post subject: Reply with quote

thx for help y'all
_________________
void(__cdecl *Haxing)(HWND hGameWindow)
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