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 


Lua Script I made freezes Cheat Engine while running

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

Joined: 07 Nov 2022
Posts: 2

PostPosted: Mon Nov 07, 2022 9:33 pm    Post subject: Lua Script I made freezes Cheat Engine while running Reply with quote

Hi!

I don't know much of scripting, but I was trying to make a script to log the RNG and it works.

The problem is that while the scrip is running Cheat Engine (7.4) freezes, my script only stops running when I close the game, and only then Cheat Engine resumes to work.

I couldn't find anything upon search that could fix this issue, maybe because I still naive into scripting/CE and things like this. Can I be helped out? thx

Code:

local number1 = readInteger("ePSXe.exe+A8B030")
showMessage(number1)
file = io.open("J:/RNG_Log.txt", "a")
date = os.date("   Session: %d/%m/%Y - %X")
file:write(date, "\n")
::loop::
local function isempty(s)
  return s == nil or s == ''
end
if isempty(number1) then
  goto fim
end
number2 = readInteger("ePSXe.exe+A8B030")
if (number1 ~= number2) then
    file:write(number1, "\n")
    sleep(100)
    number1 = number2
    goto loop
    return
end
sleep(100)
goto loop
::fim::
io.close(file)
return


Thanks for helping.



dwm_vsP3435Cf4.png
 Description:
Print Scrn of CE frozen
 Filesize:  71.08 KB
 Viewed:  2564 Time(s)

dwm_vsP3435Cf4.png


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

Joined: 06 Jul 2014
Posts: 4709

PostPosted: Mon Nov 07, 2022 11:29 pm    Post subject: Reply with quote

goto? There's goto in Lua? Huh, easy to forget...

Anyway, Lua code is run in the main thread by default. Only the main thread is allowed to access the GUI. If you block the main thread (i.e. via `sleep`), the GUI stops working.

Don't use goto- Lua isn't an assembly language. Use things like if...elseif...else, for, while, etc. Only use goto if one of these can't solve your problem. e.g. Lua has `break` but not `continue` for `for` loops

You shouldn't keep a file handle open for longer than you need to. You probably don't need to keep it open all the time in this case.

A timer would probably be the easiest way of doing this. Example:
Code:
local function logval(...)
  local file = assert(io.open('J:/RNG_Log.txt', 'a'))
  assert(file:write(...))
  file:close()
end

local oldval = readInteger('ePSXe.exe+A8B030')
if not oldval then return end
logval(os.date('   Session: %d/%m/%Y - %X\n'), oldval, '\n')

if valLogTimer then valLogTimer.destroy() end
valLogTimer = createTimer()
valLogTimer.Interval = 100
valLogTimer.OnTimer = function(t)
  local newval = readInteger('ePSXe.exe+A8B030')
  if not newval then
    t.Enabled = false
    return
  end

  if oldval ~= newval then
    oldval = newval
    logval(newval, '\n')
  end
end

I made a GUI for this a long time ago here:
https://forum.cheatengine.org/viewtopic.php?p=5701879#5701879

_________________
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
Hidrio
How do I cheat?
Reputation: 0

Joined: 07 Nov 2022
Posts: 2

PostPosted: Tue Nov 08, 2022 10:41 am    Post subject: Reply with quote

Thanks alot! Works very smoothly Very Happy

I struggle with these, learning on my pace and enjoying the way up Very Happy

Thanks again mate, giving me abit more of insight get to learn new stuff, cheers Very Happy
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