LUA Timer to read changing values in memory

Want Cheat Engine to do something specific and no idea how to do that, ask here. (From simple scripts to full trainers and extensions)
Hereisme
Noobzor
Noobzor
Posts: 9
Joined: Thu Jan 02, 2020 5:55 am
Reputation: 0

LUA Timer to read changing values in memory

Post by Hereisme »

So im making this D3D menu and i don't understand how to create timer and to read memory address with it every 3 seconds or so. I have a pointer to it, i made it with AOB scan.

Code: Select all

local RacePointer = getAddress("[Racetime]")
RacePointer = readPointer(RacePointer + 0x1C)
local function MyTimer()
if timer ~= nil
then
timer.Enabled = true
else
timer = createTimer()
timer.Interval = 3000
end
timer.Enabled = false
end

Option2=d3dhook_createTextContainer(fontmap,1,50+lineheight,'Race Timer: '..RacePointer)
The value shows up correctly but its only read if i click the execute.

TimFun13
Expert Cheater
Expert Cheater
Posts: 1354
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 6

Re: LUA Timer to read changing values in memory

Post by TimFun13 »

You never actually create the timer with that code, since you never call mytimer. What you need to do is put the update code inside the timer function and the timer create code outside the function.

Here are some eample timers. viewtopic.php?f=11&t=6492
Just note that the first example is a one shot timer, the third one will run as long as the memory record script is enabled.

Hereisme
Noobzor
Noobzor
Posts: 9
Joined: Thu Jan 02, 2020 5:55 am
Reputation: 0

Re: LUA Timer to read changing values in memory

Post by Hereisme »

ShyTwig16 wrote:
Thu Jan 02, 2020 10:44 am
You never actually create the timer with that code, what you need to do is put the update code inside the timer function and the timer create code outside the function.

Here are some eample timers. viewtopic.php?f=11&t=6492
I actually got this working

Code: Select all

al = getAddressList()
mr = al.getMemoryRecordByDescription("My RaceTimer")
mra = al.getMemoryRecordByDescription("My RaceName")
mrb = al.getMemoryRecordByDescription("Player Size")
mrc = al.getMemoryRecordByDescription("Player Size Starter")
mr.OnGetDisplayValue = function(mr,valuestring)
mra.OnGetDisplayValue = function(mra,valuestringa)
mrb.OnGetDisplayValue = function(mrb,valuestringb)

  Option1.Text = 'Player Size: '..valuestringb
  Option2.Text = 'Race Timer: '..valuestring -- print value
  Option3.Text = 'Race Name: '..valuestringa
end
end
end
but now my problem is that when i load the CT file, i need to manually activate my Auto Assembler Scripts and then execute the LUA code. But i need the LUA to activate/enable my Auto Assembler scripts because i need to read stuff from my AOB Injections.

TimFun13
Expert Cheater
Expert Cheater
Posts: 1354
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 6

Re: LUA Timer to read changing values in memory

Post by TimFun13 »

Is there a reason you have all the OnGetDisplayValue nested together like that? Seems like you should create one function for each that updates that one record using the "valuestring" parameter. With that the first two just sets the others OnGetDisplayValue repetedly and only the third one actually updates the option text. But to activate a memory record just set mr.Active to true. You can look in the celua.txt file in the CE folder for documentation, and the CE Wiki has a fair amount of this stuff documented with examples.

Hereisme
Noobzor
Noobzor
Posts: 9
Joined: Thu Jan 02, 2020 5:55 am
Reputation: 0

Re: LUA Timer to read changing values in memory

Post by Hereisme »

ShyTwig16 wrote:
Thu Jan 02, 2020 10:58 am
Is there a reason you have all the OnGetDisplayValue nested together like that? Seems like you should create one function for each that updates that one record using the "valuestring" parameter. With that the first two just sets the others OnGetDisplayValue repetedly and only the third one actually updates the option text. But to activate a memory record just set mr.Active to true. You can look in the celua.txt file in the CE folder for documentation, and the CE Wiki has a fair amount of this stuff documented with examples.
Okay i changed it so it uses just one function

Code: Select all

mr.OnGetDisplayValue = function(mr,mra,mrb,mrc,valuestring,valuestringa,valuestringb)

mr.Active=true
and i added the mr.Active=true but i still keep getting error of it not figuring out what the address name is (it can't read it because the AA scripts aren't enabled by the LUA even though i added the mr.Active=true.

TimFun13
Expert Cheater
Expert Cheater
Posts: 1354
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 6

Re: LUA Timer to read changing values in memory

Post by TimFun13 »

That function only has two parameters "memoryrecord" and "valuestring", so "mr" will be "memoryrecord" and "mra" will be "valuestring". and with "mr.Active" "mr" needs to be the memory record that is the AA script.
Here's an example of one I use to format a game time record from seconds to a readable time.

Code: Select all

TIME_MR.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' and tonumber(valuestring) ~= nil then
		local secs = tonumber(valuestring)
		if secs < 0 then
			return false, ''
		end
		local h = math.floor(secs / (60 * 60))
		local m = math.floor(secs / 60 % 60)
		local s = math.floor(secs % 60)
		return true, string.format("%.2d:%.2d:%.2d", h, m, s)
	else
		return false, valuestring
	end
end
EDIT:
and here's an example of one script enabling another. Basically "No Reload" enabling "Infinite Ammo".

Code: Select all

{$lua}
if syntaxcheck then return end
local mr = AddressList.getMemoryRecordByID(78796)
------------------------------ ENABLE ------------------------------
[ENABLE]
if not mr.Active then
    AmmoClipWrtHook_InfiniteAmmo_Active = true
    mr.Active = true
end
------------------------------ DISABLE ------------------------------
[DISABLE]
if AmmoClipWrtHook_InfiniteAmmo_Active then
    AmmoClipWrtHook_InfiniteAmmo_Active = nil
    mr.Active = false
end
I have to get to work, so hopefully someone else will help. If not I'll check back after work today.

Hereisme
Noobzor
Noobzor
Posts: 9
Joined: Thu Jan 02, 2020 5:55 am
Reputation: 0

Re: LUA Timer to read changing values in memory

Post by Hereisme »

ShyTwig16 wrote:
Thu Jan 02, 2020 11:17 am
That function only has two parameters "memoryrecord" and "valuestring", so "mr" will be "memoryrecord" and "mra" will be "valuestring". and with "mr.Active" "mr" needs to be the memory record that is the AA script.
Here's an example of one I use to format a game time record from seconds to a readable time.

Code: Select all

TIME_MR.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' and tonumber(valuestring) ~= nil then
		local secs = tonumber(valuestring)
		if secs < 0 then
			return false, ''
		end
		local h = math.floor(secs / (60 * 60))
		local m = math.floor(secs / 60 % 60)
		local s = math.floor(secs % 60)
		return true, string.format("%.2d:%.2d:%.2d", h, m, s)
	else
		return false, valuestring
	end
end
EDIT:
and here's an example of one script enabling another. Basically "No Reload" enabling "Infinite Ammo".

Code: Select all

{$lua}
if syntaxcheck then return end
local mr = AddressList.getMemoryRecordByID(78796)
------------------------------ ENABLE ------------------------------
[ENABLE]
if not mr.Active then
    AmmoClipWrtHook_InfiniteAmmo_Active = true
    mr.Active = true
end
------------------------------ DISABLE ------------------------------
[DISABLE]
if AmmoClipWrtHook_InfiniteAmmo_Active then
    AmmoClipWrtHook_InfiniteAmmo_Active = nil
    mr.Active = false
end
I have to get to work, so hopefully someone else will help. If not I'll check back after work today.
So you put lua code in the AOB? Also whats that getMemoryRecordByID(78796) ID value here?

Hereisme
Noobzor
Noobzor
Posts: 9
Joined: Thu Jan 02, 2020 5:55 am
Reputation: 0

Re: LUA Timer to read changing values in memory

Post by Hereisme »

ShyTwig16 wrote:
Thu Jan 02, 2020 11:17 am
That function only has two parameters "memoryrecord" and "valuestring", so "mr" will be "memoryrecord" and "mra" will be "valuestring". and with "mr.Active" "mr" needs to be the memory record that is the AA script.
Here's an example of one I use to format a game time record from seconds to a readable time.

Code: Select all

TIME_MR.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' and tonumber(valuestring) ~= nil then
		local secs = tonumber(valuestring)
		if secs < 0 then
			return false, ''
		end
		local h = math.floor(secs / (60 * 60))
		local m = math.floor(secs / 60 % 60)
		local s = math.floor(secs % 60)
		return true, string.format("%.2d:%.2d:%.2d", h, m, s)
	else
		return false, valuestring
	end
end
EDIT:
and here's an example of one script enabling another. Basically "No Reload" enabling "Infinite Ammo".

Code: Select all

{$lua}
if syntaxcheck then return end
local mr = AddressList.getMemoryRecordByID(78796)
------------------------------ ENABLE ------------------------------
[ENABLE]
if not mr.Active then
    AmmoClipWrtHook_InfiniteAmmo_Active = true
    mr.Active = true
end
------------------------------ DISABLE ------------------------------
[DISABLE]
if AmmoClipWrtHook_InfiniteAmmo_Active then
    AmmoClipWrtHook_InfiniteAmmo_Active = nil
    mr.Active = false
end
I have to get to work, so hopefully someone else will help. If not I'll check back after work today.

Okay i tried these now and its super glitchy [Link] and when i click ok to script i get [Link]

and the Lua script just prints same value for different strings [Link] EDIT: i fixed the button click for the script but putting that mr.Active=true inside that script im trying to enable with it is not going to work so i need to put this somehow inside my LUA script that has the timer. Also it still prints those string names but they have same values, does this work? [Link]

TimFun13
Expert Cheater
Expert Cheater
Posts: 1354
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 6

Re: LUA Timer to read changing values in memory

Post by TimFun13 »

Hereisme wrote:
Thu Jan 02, 2020 4:08 pm
...
Okay i tried these now and its super glitchy [Link] and when i click ok to script i get [Link]

and the Lua script just prints same value for different strings [Link] EDIT: i fixed the button click for the script but putting that mr.Active=true inside that script im trying to enable with it is not going to work so i need to put this somehow inside my LUA script that has the timer. Also it still prints those string names but they have same values, does this work? [Link]
Try something more like this:

Code: Select all

al = getAddressList()
mrHS = al.getMemoryRecordByDescription('HamsterBall Size')
mrRT = al.getMemoryRecordByDescription('My RaceTimer')
mrRN = al.getMemoryRecordByDescription('My RaceName')
mrHS.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option1.Text = 'Player Size: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
mrRT.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option2.Text = 'Race Timer: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
mrRN.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option3.Text = 'Race Name: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end

Hereisme
Noobzor
Noobzor
Posts: 9
Joined: Thu Jan 02, 2020 5:55 am
Reputation: 0

Re: LUA Timer to read changing values in memory

Post by Hereisme »

ShyTwig16 wrote:
Thu Jan 02, 2020 9:25 pm
Hereisme wrote:
Thu Jan 02, 2020 4:08 pm
...
Okay i tried these now and its super glitchy [Link] and when i click ok to script i get [Link]

and the Lua script just prints same value for different strings [Link] EDIT: i fixed the button click for the script but putting that mr.Active=true inside that script im trying to enable with it is not going to work so i need to put this somehow inside my LUA script that has the timer. Also it still prints those string names but they have same values, does this work? [Link]
Try something more like this:

Code: Select all

al = getAddressList()
mrHS = al.getMemoryRecordByDescription('HamsterBall Size')
mrRT = al.getMemoryRecordByDescription('My RaceTimer')
mrRN = al.getMemoryRecordByDescription('My RaceName')
mrHS.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option1.Text = 'Player Size: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
mrRT.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option2.Text = 'Race Timer: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
mrRN.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option3.Text = 'Race Name: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
Okay that almost fixes all my problems, so it does create these texts and give them values properly though some of these don't work (mainly the race timer) [Link] And i still don't know where i activate my Player Size AA AOB script? Also not sure if i should improve these

Code: Select all

lineheight=d3dhook_texture_getHeight(fontmap) --fontmap inherits from texture so this can be used
Option1=d3dhook_createTextContainer(fontmap,1,50,'Player Size: '..PlayerSize)
Option2=d3dhook_createTextContainer(fontmap,1,50+lineheight,'Race Timer: '..RacePointer)
Option3=d3dhook_createTextContainer(fontmap,1,50+lineheight*2,'Race Name: '..RaceName)
In a sense that the concatenate strings are the functions that get the addresses... EDIT: hmm the race timer actually works now,weird so my current problem would be starting the AA AOB script after i click execute, also wanted to ask that how would i setup something like this [Link] so that it would print text 'Camera Positions: (x,y,z)' ?

TimFun13
Expert Cheater
Expert Cheater
Posts: 1354
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 6

Re: LUA Timer to read changing values in memory

Post by TimFun13 »

Hereisme wrote:
Thu Jan 02, 2020 10:07 pm
...

Okay that almost fixes all my problems, so it does create these texts and give them values properly though some of these don't work (mainly the race timer) [Link] And i still don't know where i activate my Player Size AA AOB script? Also not sure if i should improve these

Code: Select all

lineheight=d3dhook_texture_getHeight(fontmap) --fontmap inherits from texture so this can be used
Option1=d3dhook_createTextContainer(fontmap,1,50,'Player Size: '..PlayerSize)
Option2=d3dhook_createTextContainer(fontmap,1,50+lineheight,'Race Timer: '..RacePointer)
Option3=d3dhook_createTextContainer(fontmap,1,50+lineheight*2,'Race Name: '..RaceName)
In a sense that the concatenate strings are the functions that get the addresses... EDIT: hmm the race timer actually works now,weird so my current problem would be starting the AA AOB script after i click execute, also wanted to ask that how would i setup something like this [Link] so that it would print text 'Camera Positions: (x,y,z)' ?
No sure where to put the activator for the AA script. How are you attaching to the process, and where is the rest of the script, is this a table or a trainer? You could try a timer and check for the process, then enable the AA script. Or just have the AA script also run the Lua code, and just nane the script "Enable This" or something like that.

As for the coordinates, just use "string.format".

So something like this.

Code: Select all

//------------------------------ ENABLE ------------------------------
[ENABLE]
// AOB script enable section here
{$lua}
al = getAddressList()
mrHS = al.getMemoryRecordByDescription('HamsterBall Size')
mrRT = al.getMemoryRecordByDescription('My RaceTimer')
mrRN = al.getMemoryRecordByDescription('My RaceName')
mrHS.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option1.Text = 'Player Size: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
mrRT.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option2.Text = 'Race Timer: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
mrRN.OnGetDisplayValue = function(memoryrecord, valuestring)
	local x = readFloat('[playerpos]+758')
	local y = readFloat('[playerpos]+75C')
	local z = readFloat('[playerpos]+760')
	if x ~= nil and y ~= nil and z ~= nil then
		OptionPOS.Text = string.format('Camera Position: %.2f, %.2f, %.2f', x, y, z)
	end
	if type(valuestring) == 'string' then
		Option3.Text = 'Race Name: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
{$asm}
//------------------------------ DISABLE ------------------------------
[DISABLE]
// AOB script disable section here

Hereisme
Noobzor
Noobzor
Posts: 9
Joined: Thu Jan 02, 2020 5:55 am
Reputation: 0

Re: LUA Timer to read changing values in memory

Post by Hereisme »

ShyTwig16 wrote:
Thu Jan 02, 2020 11:06 pm
Hereisme wrote:
Thu Jan 02, 2020 10:07 pm
...

Okay that almost fixes all my problems, so it does create these texts and give them values properly though some of these don't work (mainly the race timer) [Link] And i still don't know where i activate my Player Size AA AOB script? Also not sure if i should improve these

Code: Select all

lineheight=d3dhook_texture_getHeight(fontmap) --fontmap inherits from texture so this can be used
Option1=d3dhook_createTextContainer(fontmap,1,50,'Player Size: '..PlayerSize)
Option2=d3dhook_createTextContainer(fontmap,1,50+lineheight,'Race Timer: '..RacePointer)
Option3=d3dhook_createTextContainer(fontmap,1,50+lineheight*2,'Race Name: '..RaceName)
In a sense that the concatenate strings are the functions that get the addresses... EDIT: hmm the race timer actually works now,weird so my current problem would be starting the AA AOB script after i click execute, also wanted to ask that how would i setup something like this [Link] so that it would print text 'Camera Positions: (x,y,z)' ?
No sure where to put the activator for the AA script. How are you attaching to the process, and where is the rest of the script, is this a table or a trainer? You could try a timer and check for the process, then enable the AA script. Or just have the AA script also run the Lua code, and just nane the script "Enable This" or something like that.

As for the coordinates, just use "string.format".

So something like this.

Code: Select all

//------------------------------ ENABLE ------------------------------
[ENABLE]
// AOB script enable section here
{$lua}
al = getAddressList()
mrHS = al.getMemoryRecordByDescription('HamsterBall Size')
mrRT = al.getMemoryRecordByDescription('My RaceTimer')
mrRN = al.getMemoryRecordByDescription('My RaceName')
mrHS.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option1.Text = 'Player Size: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
mrRT.OnGetDisplayValue = function(memoryrecord, valuestring)
	if type(valuestring) == 'string' then
		Option2.Text = 'Race Timer: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
mrRN.OnGetDisplayValue = function(memoryrecord, valuestring)
	local x = readFloat('[playerpos]+758')
	local y = readFloat('[playerpos]+75C')
	local z = readFloat('[playerpos]+760')
	if x ~= nil and y ~= nil and z ~= nil then
		OptionPOS.Text = string.format('Camera Position: %.2f, %.2f, %.2f', x, y, z)
	end
	if type(valuestring) == 'string' then
		Option3.Text = 'Race Name: ' .. valuestring
		return true, valuestring
	else
		return false, valuestring
	end
end
{$asm}
//------------------------------ DISABLE ------------------------------
[DISABLE]
// AOB script disable section here
Okay one more thing, how do i hide/disable all these texts if i press button lets say tilde button (0xc0)?

TimFun13
Expert Cheater
Expert Cheater
Posts: 1354
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 6

Re: LUA Timer to read changing values in memory

Post by TimFun13 »

You can use a script with a hotkey that toggles the script then in that script set the fonmap's "Visible" property to false if true and true if false (or a simple "fm.Visible = not fm.Visible") in the main section of the script so it runs when enabled and disabled. Not really sure thought, never messed with the D3D mutch.

Hereisme
Noobzor
Noobzor
Posts: 9
Joined: Thu Jan 02, 2020 5:55 am
Reputation: 0

Re: LUA Timer to read changing values in memory

Post by Hereisme »

ShyTwig16 wrote:
Fri Jan 03, 2020 12:06 am
You can use a script with a hotkey that toggles the script then in that script set the fonmap's "Visible" property to false if true and true if false (or a simple "fm.Visible = not fm.Visible") in the main section of the script so it runs when enabled and disabled. Not really sure thought, never messed with the D3D mutch.
Thank you so much for helping me with everything so far, i tried

Code: Select all

function keydown(virtualkey,char)
  if (virtualkey==VK_UP) then
  fontmap.destroy=true
  else
  fontmap=d3dhook_createFontmap(font)
end
end
as i can't find fontmap function in any cheat engine wiki page and even this sadly doesn't work..

TimFun13
Expert Cheater
Expert Cheater
Posts: 1354
Joined: Fri Mar 03, 2017 12:31 am
Reputation: 6

Re: LUA Timer to read changing values in memory

Post by TimFun13 »

Hereisme wrote:
Fri Jan 03, 2020 8:18 am
ShyTwig16 wrote:
Fri Jan 03, 2020 12:06 am
You can use a script with a hotkey that toggles the script then in that script set the fonmap's "Visible" property to false if true and true if false (or a simple "fm.Visible = not fm.Visible") in the main section of the script so it runs when enabled and disabled. Not really sure thought, never messed with the D3D mutch.
Thank you so much for helping me with everything so far, i tried

Code: Select all

function keydown(virtualkey,char)
  if (virtualkey==VK_UP) then
  fontmap.destroy=true
  else
  fontmap=d3dhook_createFontmap(font)
end
end
as i can't find fontmap function in any cheat engine wiki page and even this sadly doesn't work..
Yeah, sometimes it's best to look in the "celua.txt" file in the CE folder, it has a list of functions and classes. Then I hit ctrl-f and enter what I'm looking for. The return from "createD3DHook" has a "OnKeyDown" property as well. And see if toggling the fontmap's "Visible" property works, you could create the fontmap then set visible to false and only show the text after the key is pressed. And in the code above you'll be creating a new fontmap every time a key is pressed except when it's the up key, so you'll want to move where you create the fontmap, or at least check for nil and create a font map only if it hasn't been created yet.

Post Reply

Who is online

Users browsing this forum: No registered users