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 


I need some help with LUA Scripting

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

Joined: 16 Apr 2020
Posts: 5

PostPosted: Thu Apr 16, 2020 1:15 pm    Post subject: I need some help with LUA Scripting Reply with quote

Hey there! I'm new to LUA (and somewhat used to CE) and I'm attempting to make a script to output an address' value to a txt file but it doesn't seem to work. So far both PreviousValue and CurrentValue output Nil (even when set to be 0) and it doesn't seem to grab the address I need.

Here's what I need help with

    1. Help with identifying and grabbing the correct address
    2. Updating the Previous and Current values so they aren't Nil
    3. Outputting the values to a txt file successfully so it can be used in OBS as a health display.


Here's the code (CE Tutorial used as a way to test the code)
Code:

getAutoAttachList().add("Tutorial-i386.exe")
previousValue = 0
currentValue = 0

function readValue()
    io.open( "ValueOutput.txt", "w+" )
    io.output( "ValueOutput.txt" )
    currentValue = readFloat(ADDRESS TO HEALTH)
    if previousValue ~= currentValue then
        previousValue = currentValue
        io.write(tostring(previousValue))
    elseif tonumber(currentValue) <= 0 then
        previousValue = currentValue
        io.write("Player has died.")
        sleep(15000)
    end
    io.close()
end
readValue()

Code can be downloaded in the attachment below

Edit: If I could also get some help with making it loop endlessly (without crashing CE or overloading my PC) using a timer then that'd help as well!

Hope I can get some help!



ReadCheatEngineValues.lua
 Description:
Unfinished and buggy code.

Download
 Filename:  ReadCheatEngineValues.lua
 Filesize:  534 Bytes
 Downloaded:  203 Time(s)


_________________
Here to learn and use LUA in cheat engine for dumb things for streaming.
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Thu Apr 16, 2020 3:10 pm    Post subject: Reply with quote

This should work, added some comments.
Code:
function readFile(a,b)local c=io.open(a,'r')if not c then return false end;local d=c:read('*all')c:close()if b then local e,f={},0;for g in d:gmatch('[^\n]+')do e[#e+1]=g end;return function()f=f+1;return e[f]end end;return d end
function writeFile(a,b,c)if type(b)=='string'then local d=c and'a'or'w'local e=io.open(a,d)e:write(b)e:close()return true end;return false end


local filePath = "C:\\test\\ValueOutput.txt"               -- path or name; update to your;
local previousValue
local addressToRead = 0xA289113C

function readValue(self)
   local currentValue = readFloat(addressToRead);
   if (not previousValue) then                           -- no valid value; maybe a new instance of the script or so
      local fileContent = readFile(filePath);
      previousValue = tonumber(fileContent) or currentValue    -- assuming we store in plain file the hp value or a string representing player has died.
   end
   if (previousValue ~= currentValue) then                  -- a change has been detected;
      if (currentValue <= 0) then
         writeFile(filePath,"Player has died.")            -- 3rd parameter is to append it to file;
         -- self.enabled = false;                     -- disable timer as player has died;
      else                                       -- if previousValue is bigger than 0
         writeFile(filePath,tostring(currentValue))         -- writes current health
      end
      previousValue = currentValue; -- update it in any case;
   end
end

healthTimer = createTimer(getMainForm());
healthTimer.Interval = 100;                            -- 1 sec;
healthTimer.onTimer = readValue;

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
StarryMic
How do I cheat?
Reputation: 0

Joined: 16 Apr 2020
Posts: 5

PostPosted: Thu Apr 16, 2020 4:22 pm    Post subject: Is there something I am doing wrong? Reply with quote

The text file isn't updating with the values and is still blank. I have updated the lines that I thought needed to be updated but nothing happens. I have the address in the code and the process is attached to CE. Was there something else I needed to do?

For reference I'm trying to grab the health of my character from Hotdogs, Horseshoes, and Hand Grenades and output it to a txt file.

_________________
Here to learn and use LUA in cheat engine for dumb things for streaming.
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Thu Apr 16, 2020 5:04 pm    Post subject: Re: Is there something I am doing wrong? Reply with quote

StarryMic wrote:
The text file isn't updating with the values and is still blank. I have updated the lines that I thought needed to be updated but nothing happens. I have the address in the code and the process is attached to CE. Was there something else I needed to do?

For reference I'm trying to grab the health of my character from Hotdogs, Horseshoes, and Hand Grenades and output it to a txt file.

Did you modify the path in the script?
You can try print what to lua when it writes stuff to file, simple change writeFile with print.

Are you trying to writing 3 values into a single file? writeFile overwrites everything in the file when called.

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
StarryMic
How do I cheat?
Reputation: 0

Joined: 16 Apr 2020
Posts: 5

PostPosted: Thu Apr 16, 2020 5:28 pm    Post subject: Reply with quote

Yep. Changed the path and made the io.open go from r to r+ so that it makes the txt file on run.
I'm only trying to write 1 address (the player's health) to the txt file.

Sidenote: You got the healthTimer.Interval wrong. 1000 ms = 1 second but instead it was 100 ms.

I've modified the code to be the following (using the address from the CE Tutorial to test writing values to file).
Code:
function readFile(a,b)local c=io.open(a,'r+')if not c then return false end;local d=c:read('*all')c:close()if b then local e,f={},0;for g in d:gmatch('[^\n]+')do e[#e+1]=g end;return function()f=f+1;return e[f]end end;return d end
function writeFile(a,b,c)if type(b)=='string'then local d=c and'a'or'w'local e=io.open(a,d)e:write(b)e:close()return true end;return false end


local filePath = "ValueOutput.txt"               -- path or name; update to your;
local previousValue
local addressToRead = 0x014F1C00

function readValue(self)
   local currentValue = readFloat(addressToRead);
   if (not previousValue) then                           -- no valid value; maybe a new instance of the script or so
      local fileContent = readFile(filePath);
      previousValue = tonumber(fileContent) or currentValue    -- assuming we store in plain file the hp value or a string representing player has died.
   end
   if (previousValue ~= currentValue) then                  -- a change has been detected;
      if (currentValue <= 0) then
         print("Player has died.")            -- 3rd parameter is to append it to file;
         -- self.enabled = false;                     -- disable timer as player has died;
      else                                       -- if previousValue is bigger than 0
         print((tostring(currentValue)))         -- writes current health
      end
      previousValue = currentValue; -- update it in any case;
   end
end

healthTimer = createTimer(getMainForm());
healthTimer.Interval = 1000;                            -- 1 sec;
healthTimer.onTimer = readValue;


I see getMainForm() in the code. Do I need to make a form to make this work? If so, how should it be set up?

_________________
Here to learn and use LUA in cheat engine for dumb things for streaming.
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Thu Apr 16, 2020 6:28 pm    Post subject: Reply with quote

readFile && writeFile each do not keep any of file handles, it simply open a file reads/writes content to it and closes the handle.
the only reason I've added the readFile is just in case you restart the process and wish to continue from where you left, this section that uses it can be removed.

when you use use writeFile you need to supply a path, text to write, and optionally appendMode, if file does not exists opening with 'w' or 'a' mode will create the file.

getMainForm() returns cheat engine main window, and set it as the timer parent, you put any other parent.


The script does seem to work tho?

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
StarryMic
How do I cheat?
Reputation: 0

Joined: 16 Apr 2020
Posts: 5

PostPosted: Thu Apr 16, 2020 6:39 pm    Post subject: Reply with quote

Sadly, no. One thing I have noticed is that at the bottom of the script it has this line
Code:
healthTimer.onTimer = readValue;

Is this meant to be readValue() or is that intentional?

Edit: If we can't get this working then do you mind hopping into a discord call and helping out there? I have an alternative method I could do if needed but I've already come this far so I might as well keep going.

_________________
Here to learn and use LUA in cheat engine for dumb things for streaming.
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Thu Apr 16, 2020 8:11 pm    Post subject: Reply with quote

readValue is the function name, that is the function the timer executing.
Try create new folder at your local drive (adjust path according to it).
Allocate some memory,update the address to the new memory allocated, and change the memory value (float), and check if a new file has been made?

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
StarryMic
How do I cheat?
Reputation: 0

Joined: 16 Apr 2020
Posts: 5

PostPosted: Thu Apr 16, 2020 8:36 pm    Post subject: Reply with quote

How do I allocate memory?
_________________
Here to learn and use LUA in cheat engine for dumb things for streaming.
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