| View previous topic :: View next topic |
| Author |
Message |
StarryMic How do I cheat?
Reputation: 0
Joined: 16 Apr 2020 Posts: 5
|
Posted: Thu Apr 16, 2020 1:15 pm Post subject: I need some help with LUA Scripting |
|
|
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! _________________
Here to learn and use LUA in cheat engine for dumb things for streaming. |
|
| Back to top |
|
 |
daspamer Grandmaster Cheater Supreme
Reputation: 54
Joined: 13 Sep 2011 Posts: 1588
|
Posted: Thu Apr 16, 2020 3:10 pm Post subject: |
|
|
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 |
|
 |
StarryMic How do I cheat?
Reputation: 0
Joined: 16 Apr 2020 Posts: 5
|
Posted: Thu Apr 16, 2020 4:22 pm Post subject: Is there something I am doing wrong? |
|
|
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 |
|
 |
daspamer Grandmaster Cheater Supreme
Reputation: 54
Joined: 13 Sep 2011 Posts: 1588
|
Posted: Thu Apr 16, 2020 5:04 pm Post subject: Re: Is there something I am doing wrong? |
|
|
| 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 |
|
 |
StarryMic How do I cheat?
Reputation: 0
Joined: 16 Apr 2020 Posts: 5
|
Posted: Thu Apr 16, 2020 5:28 pm Post subject: |
|
|
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 |
|
 |
daspamer Grandmaster Cheater Supreme
Reputation: 54
Joined: 13 Sep 2011 Posts: 1588
|
Posted: Thu Apr 16, 2020 6:28 pm Post subject: |
|
|
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 |
|
 |
StarryMic How do I cheat?
Reputation: 0
Joined: 16 Apr 2020 Posts: 5
|
Posted: Thu Apr 16, 2020 6:39 pm Post subject: |
|
|
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 |
|
 |
daspamer Grandmaster Cheater Supreme
Reputation: 54
Joined: 13 Sep 2011 Posts: 1588
|
Posted: Thu Apr 16, 2020 8:11 pm Post subject: |
|
|
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 |
|
 |
StarryMic How do I cheat?
Reputation: 0
Joined: 16 Apr 2020 Posts: 5
|
Posted: Thu Apr 16, 2020 8:36 pm Post subject: |
|
|
How do I allocate memory? _________________
Here to learn and use LUA in cheat engine for dumb things for streaming. |
|
| Back to top |
|
 |
|