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 


Issues Enabling/Disabling a script that handles inputs

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
a_person99
How do I cheat?
Reputation: 0

Joined: 02 Jul 2023
Posts: 3

PostPosted: Sun Jul 02, 2023 5:51 pm    Post subject: Issues Enabling/Disabling a script that handles inputs Reply with quote

I added the following auto assembly script to the table. however the timer seems to always be running and the inputs are always being read even when the checkbox in the table is not checked.

The issue is the timer is not getting destroyed, it's just always running in the background as soon as I add the script to the table.

Code:

{$LUA}
local isScriptEnabled = true
local thread = nil

function enableScript()
    thread = createThread(function()
        while isScriptEnabled do
            local myVar = tonumber(AddressList.getMemoryRecordByDescription('Var').Value)

            if myVar == 0 and isKeyPressed(0x51) then
                keyDown(0x59)
                sleep(50)
                keyUp(0x59)
            end

            sleep(10)
        end
    end)
end

function disableScript()
    isScriptEnabled = false
    if thread ~= nil then
        thread.terminate()
        thread = nil
    end
end

[ENABLE]
enableScript()

[DISABLE]
disableScript()


I have also tried:

Code:

{$LUA}
local timer = nil
local isScriptEnabled = true

function enableScript()
    timer = createTimer(getMainForm())
    timer.Interval = 10
    timer.OnTimer = function(timer)
        if isScriptEnabled then
            local myVar = tonumber(AddressList.getMemoryRecordByDescription('Var').Value)

            if myVar == 0 and isKeyPressed(0x51) then
                keyDown(0x59)
                sleep(50)
                keyUp(0x59)
            end
        end
    end
end

function disableScript()
    if timer ~= nil then
        timer.destroy()
        timer = nil
    end
end

[ENABLE]
enableScript()

[DISABLE]
isScriptEnabled = false
disableScript()



I've tried a bunch of different generated suggestions and they all have the same issue, can't disable the timer when the box is unchecked in the table. How do I get the script to completely deactivate when the box in the table is not checked/only run the timer when the box is checked? Any help appreciated.



table.png
 Description:
 Filesize:  51.6 KB
 Viewed:  1650 Time(s)

table.png


Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4724

PostPosted: Sun Jul 02, 2023 7:55 pm    Post subject: Reply with quote

{$lua} blocks are run independently when the scripts are enabled and disabled. Think of [ENABLE] and [DISABLE] like C preprocessor macros.
Code:
{$lua}
local foo = 1
[ENABLE]
foo = 2
print(foo)  -- prints 2
[DISABLE]
print(foo)  -- prints 1

When the script is enabled, this gets run:
Code:
{$lua}
local foo = 1
foo = 2
print(foo)  -- prints 2
And when it's disabled:
Code:
{$lua}
local foo = 1
print(foo)  -- prints 1
Basically, the locals are independent of each other.
In your [DISABLE] code, ` isScriptEnabled = false` does not change the same variable that's being read by the thread, and `timer` is nil even in the `disableScript` function.

You forgot `if syntaxcheck then return end`
`AddressList.getMemoryRecordByDescription(...)` - Don't access the GUI in a separate thread.
`sleep(50)` - You shouldn't sleep in the main thread (timers use the main thread)
`isKeyPressed(0x51)` / `keyDown(0x59)` - Please use VK_Q / VK_Y instead of raw integers. These globals are defined in "defines.lua" in the main CE directory.

Is the delay between keyDown and keyUp vitally important for you to control? If not, just use createHotkey and doKeyPress:
Code:
{$lua}
if syntaxcheck then return end

if uniqueHotkeyName then
  uniqueHotkeyName.destroy()
  uniqueHotkeyName = nil
end

[ENABLE]

uniqueHotkeyName = createHotkey(function()
  if tonumber(AddressList.getMemoryRecordByDescription'Var'.Value) == 0 then
    doKeyPress(VK_Y)
  end
end, VK_Q)

[DISABLE]
-- hotkey destroyed above

If you need to control that delay, use a thread to handle pressing the key and `synchronize` to read the other memory record's value.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
a_person99
How do I cheat?
Reputation: 0

Joined: 02 Jul 2023
Posts: 3

PostPosted: Mon Jul 03, 2023 1:47 am    Post subject: Reply with quote

Thanks for replying but I haven't been able to solve the issue.

Let's just forget about the exact purpose of the code within the script, just that it involves a timer that runs every 100 millseconds that checks the value of a certain variable and presses a button if that check is true. It just automates an input.

The issue I'm having is the disabling of the script/disabling of the timer. I pull up the auto assembly window, put the code in there, and then add the script with the checkbox next to it to the table.

The script is inactive until I first check the box, then it works, which is fine. However, when I uncheck the box, the script is still running. The timer is still running. The automated action is still being performed when the variable equals that specified value. When the box next to the script is not checked, the script should be off. But unchecking the box does not disable the script. It's like its permanently stuck on/enabled.

So how do I turn off/disable/deactivate the script when the checkbox is not checked, so I can for example, stop the script, make changes to it using the "change script" button, and then restart it again as a new version without the old version running in the background still?

Here is the basic logic of what I'm trying to accomplish:

Code:

{$lua}

if syntaxcheck then return end

local timer = nil

[ENABLE]
timer = createTimer(getMainForm())
timer.Interval = 100
timer.OnTimer = function(timer)
    local myValue = tonumber(AddressList.getMemoryRecordByDescription('myValue').Value)

    if myValue == 100 then
        keyDown(0x59)
        sleep(50)
        keyUp(0x59)
    end
end

[DISABLE]
if timer ~= nil then
    timer.destroy()
    timer = nil
end


Is it something with the scope or declaration of the timer variable? I've tried moving it around and no luck. Seems like it should be easy but very stuck on it. To get around this I've been restarting CE every time I've had to make a change to the script because the old version just runs persistently even when the box is not checked in the table. I've tried using hotkeys to enable/disable the autoassembly script and it does not work either.

My robot friend keeps suggesting these types of fixes but still the same issue with the script always being stuck on and unable to stop it.

Code:

if syntaxcheck then return end

local timer = nil
local isScriptEnabled = true

function enableScript()
    timer = createTimer(getMainForm())
    timer.Interval = 100
    timer.OnTimer = function(timer)
        if isScriptEnabled then
            local myValue = tonumber(AddressList.getMemoryRecordByDescription('myValue').Value)

            if myValue == 100 then
                keyDown(0x59)
                sleep(50)
                keyUp(0x59)
            end
        end
    end
end

function disableScript()
    if timer ~= nil then
        timer.destroy()
        timer = nil
    end
end

[ENABLE]
enableScript()

[DISABLE]
disableScript()
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25840
Location: The netherlands

PostPosted: Mon Jul 03, 2023 2:50 am    Post subject: Reply with quote

local like isScriptEnabled can't be used outside of a function in aa scripts as the script gets reparsed each time
(also check if the function is already defined and if so, don't redefine it)

e.g in this case disableScript will see a different isScriptEnabled than the enable did

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
a_person99
How do I cheat?
Reputation: 0

Joined: 02 Jul 2023
Posts: 3

PostPosted: Mon Jul 03, 2023 3:11 am    Post subject: Reply with quote

Why does the script above that one not work?

This script:
Code:

if syntaxcheck then return end

local timer = nil

[ENABLE]
timer = createTimer(getMainForm())
timer.Interval = 100
timer.OnTimer = function(timer)
    local myValue = tonumber(AddressList.getMemoryRecordByDescription('myValue').Value)

    if myValue == 100 then
        keyDown(0x59)
        sleep(50)
        keyUp(0x59)
    end
end

[DISABLE]
if timer ~= nil then
    timer.destroy()
    timer = nil
end



If the timer is first declared outside of the enable and disable blocks shouldn't the disable block be able to destroy it? I think the problem is the disable block can't access the timer that's created in the enable block? So its always seeing it as nil and then not destroying it? How would I make that possible, what would I have to do differently?

***EDIT
Managed to fix it. Had to get rid of that initial local declaration and the nil check in the disable block. Could have sworn I tried this before and it didn't work which led me to the other solutions but it's working now.

Code:

{$lua}
if syntaxcheck then return end

[ENABLE]
timer = createTimer(getMainForm())
timer.Interval = 10
timer.OnTimer = function(timer)
    local myValue = tonumber(AddressList.getMemoryRecordByDescription('myValue').Value)
    if myValue == 1000 then
        keyDown(0x59)
        sleep(50)
        keyUp(0x59)
    end
end

[DISABLE]
timer.destroy()
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 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