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 that increments a variable over time

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

Joined: 20 Feb 2022
Posts: 8

PostPosted: Sun Jul 07, 2024 11:35 pm    Post subject: LUA script that increments a variable over time Reply with quote

Hello! My first contact with LUA scripting. I'm trying to make a value in my cheat table increment by some value (let's say +0.2) every second, and stop after reaching some maximum value (let's say 3).

I've found a few examples on these boards, and after some experimenting, I've found one that I can insert without generating errors:

Code:
function ItemHack(Name1, sec)
local oldv1 = AddressList.getMemoryRecordByDescription('Name1').Value
oldv1 = tonumber(oldv1)

local t = createTimer() t.Enabled=true
t.Interval = tonumber(sec)
t.OnTimer = function(t)
local mr = AddressList.getMemoryRecordByDescription('Name1')
  if not oldv1 then t.destroy(); return end
local v1 = tonumber(mr.Value)
    oldv1 = oldv1 + 0.2
    mr.Value = tostring(oldv1)
    if oldv1 == 3 then t.destroy(); end
end
end


However, it doesn't actually do anything. What am I missing? And yes, I have actually named the variable in my table "Name1." It's a double, if that matters.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4706

PostPosted: Mon Jul 08, 2024 12:17 am    Post subject: Reply with quote

When, where, and how is the function `ItemHack` called?

That function's first parameter, `Name1`, is never used

Your code isn't indented properly

Put `t.Enabled = true` on its own line (timers are enabled by default, no need for this in the first place)

Checking if `oldv1` is valid doesn't do much; instead, you should check if `mr` is valid

The local variable `v1` is never used

You set the new value of the memory record without reading the old value first. That's fine if nothing else writes to it; otherwise, that might be unintended

That comparison w/ the max value should be greater than or equal to, not just equal to. (due to floating point error, if you take 0 and add 0.2 to it 15 times, you get 3.0000000000000004440892, which is not equal to 3)

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

Joined: 20 Feb 2022
Posts: 8

PostPosted: Mon Jul 08, 2024 12:31 am    Post subject: Reply with quote

Thanks for the quick response!

I tried adding some more stuff to the end and now it works, but I don't really understand why :p

Code:
function ItemHack(Name1, sec)
local oldv1 = AddressList.getMemoryRecordByDescription('Name1').Value
oldv1 = tonumber(oldv1)

local t = createTimer() t.Enabled=true
t.Interval = tonumber(sec)
t.OnTimer = function(t)
local mr = AddressList.getMemoryRecordByDescription('Name1')
  if not oldv1 then t.destroy(); return end
local v1 = tonumber(mr.Value)
    oldv1 = oldv1 + 0.2
    mr.Value = tostring(oldv1)
    if oldv1 >= 3 then t.destroy(); end
end
end

memrec = getAddressList().getMemoryRecordByDescription
memrec('Name1').Value = "0"
memrec('Name1').Active = true
if memrec('Name1').Value <= "3" then

ItemHack("Name1", "1000") end


I don't get why I have to set the variable to 0 to get things started, but if I don't it doesn't work.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1528

PostPosted: Mon Jul 08, 2024 1:18 am    Post subject: Reply with quote

For example:
Code:
oldv1 = tonumber("3.00000022")
oldv1 = tonumber(oldv1) + tonumber("0.2")

print(oldv1)


use:

Code:
local oldv1 = tonumber(AddressList.getMemoryRecordByDescription('Name1').Value)


or :
Code:
local oldv1 = 0 -- local number..
oldv1 = AddressList.getMemoryRecordByDescription('Name1').Value
oldv1 = tonumber(oldv1) + tonumber("0.2")

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Antifringe
How do I cheat?
Reputation: 0

Joined: 20 Feb 2022
Posts: 8

PostPosted: Tue Jul 09, 2024 2:01 am    Post subject: Reply with quote

Thanks everyone, I appreciate the responses.

ArylinCE, I think this was code I lifted from one of your previous posts, so thank you for the head start!

I'm doing this now:

Code:

function ItemHack(Anchovy, sec)
local oldv1 = AddressList.getMemoryRecordByDescription('Anchovy').Value
oldv1 = tonumber(oldv1)

local t = createTimer() t.Enabled=true
t.Interval = tonumber(sec)
t.OnTimer = function(t)
local mr = AddressList.getMemoryRecordByDescription('Anchovy')
local v1 = tonumber(mr.Value)
    oldv1 = oldv1 + 0.2
    mr.Value = tostring(oldv1)
    if oldv1 >= 10 then t.destroy(); end
end
end

memrec = getAddressList().getMemoryRecordByDescription
memrec('Anchovy').Value = "0"
if memrec('Anchovy').Value <= "10" then

ItemHack("Anchovy", "1000") end


It mostly works, but there's a bug. The intended effect was that the resource (which is easy but tedious to accumulate) slowly regenerates until it hits the cap, saving me from a bit of hassle. But what is going on now is that if my resource is at, say, 7, and I spend 1 in-game, the script immediately refunds me the spent resource. So the expected sequence is:
1) I have 7 resource
2) I use one, bringing me down to 6
3) When the script timer hits, this gets incremented to 6.2

Instead, what happens is:
1) I have 7 resource
2) I use one, bringing me down to 6
3) When the script timer hits, this gets incremented to 7.2

This is more of an infinite supply hack than a slow regen hack and not what I wanted. I think I kind of get why it happens (oldvar1 isn't getting updated correctly), but I don't know how to fix that. I tried moving the part that defines and sets oldvar1 inside the timer, but that didn't help at all.

As a separate issue, I also like to be able to set the maximum value to the in-game cap, which can change as you play. Right now I have it hardcoded to a compromise value of 10. I tried defining a new variable called 'AnchovyCap' that reads the in-game cap from my table, and setting the cap check to:

if oldv1 >= AnchovyCap then t.destroy(); end

But it doesn't seem to like having a variable in the inequality. I probably need to transform the variable in some way to make it acceptable.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1528

PostPosted: Tue Jul 09, 2024 5:24 am    Post subject: Reply with quote

Code:
local Desc1 = ""
local oldv1 = 0

if ihTim1 then ihTim1.Destroy() ihTim1=nil end
ihTim1=createTimer() ihTim1.Interval = 900
ihTim1.Enabled=false

if ihTim2 then ihTim2.Destroy() ihTim2=nil end
ihTim2=createTimer() ihTim2.Interval = 1000
ihTim2.Enabled=false

function ItemHack()
oldv1 = tonumber(AddressList.getMemoryRecordByDescription(Desc1).Value)

    local mr = AddressList.getMemoryRecordByDescription(Desc1)
    oldv1 = tonumber(oldv1) + tonumber(0.2)
    mr.Value = tostring(oldv1)
    if oldv1 >= 10 then ihTim2.Enabled=false end
end

function tracking1()
   Desc1 = "Anchovy"
mem1 = getAddressList().getMemoryRecordByDescription(Desc1)
 if tonumber(mem1.Value) <= 10 then
   ihTim2.Enabled=true
 end
end

ihTim1.OnTimer = tracking1
ihTim2.OnTimer = ItemHack

-- start check ..
ihTim1.Enabled=true

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Antifringe
How do I cheat?
Reputation: 0

Joined: 20 Feb 2022
Posts: 8

PostPosted: Tue Jul 09, 2024 10:59 am    Post subject: Reply with quote

Thanks again! I needed to add an "end" to the last line, but then the script executed.

The problem is, it doesn't do anything :p. Doesn't generate LUA errors, but also doesn't actually change anything in game.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1528

PostPosted: Tue Jul 09, 2024 11:45 am    Post subject: Reply with quote

The current version of the code does not contain any deficiencies that require "end". Do not add.

Code:
local Desc1 = ""
local oldv1 = 0

if ihTim1 then ihTim1.Destroy() ihTim1=nil end
ihTim1=createTimer() ihTim1.Interval = 900
ihTim1.Enabled=false

if ihTim2 then ihTim2.Destroy() ihTim2=nil end
ihTim2=createTimer() ihTim2.Interval = 1000
ihTim2.Enabled=false

function ItemHack()
oldv1 = tonumber(AddressList.getMemoryRecordByDescription(Desc1).Value)

    local mr = AddressList.getMemoryRecordByDescription(Desc1)
    oldv1 = tonumber(oldv1) + tonumber(0.2)
    mr.Value = tostring(oldv1)
    if oldv1 >= 10 then ihTim2.Enabled=false end
end

function tracking1()
   Desc1 = "Anchovy"
mem1 = getAddressList().getMemoryRecordByDescription(Desc1)
 if tonumber(mem1.Value) <= 10 then
   ihTim2.Enabled=true
 end
end

ihTim1.OnTimer = tracking1
ihTim2.OnTimer = ItemHack

-- start check ..
ihTim1.Enabled=true


The code works properly.
It was tested with the following test and the results in the description were obtained:

1) A blank notepad is opened.
2) CE was opened and the code was pasted into the Lua script.
3) The memorandum opened during the CE transaction process has been selected.
4) 10 was searched in the float scan.
5) The first result for the example has been moved down (to the Address list).
6) The address description (Anchovy) specified in the code has been replaced with the description taken below. (No Description --> Anchovy)
7) Lua script is enabled.
Cool Double clicked on the value of the sample address and changed it to 3.

Results:
After changing to 3, the value reached "10.+" with "0.2" increments and the increase stopped.
The value was set to 5 again for testing and continued to increase with "5.2".
When the value is "10.+", "ihTim2" in the code stops and "ihTim1", which constantly follows the value, continues to work.

Test the code using the same method and see where you went wrong or indicate what you need from now on. Wink

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Antifringe
How do I cheat?
Reputation: 0

Joined: 20 Feb 2022
Posts: 8

PostPosted: Tue Jul 09, 2024 12:55 pm    Post subject: Reply with quote

Hey, thanks, I really appreciate this!

I updated Cheat Engine, and now things work perfectly. I suspect that the problem was simply just parts of my old (and bad) scripts that I hadn't properly purged, and the process of updating cleaned things up as a side effect.

I'm sorry to have wasted your time on a ghost hunt like that. this was all extremely helpful.
Back to top
View user's profile Send private message
Antifringe
How do I cheat?
Reputation: 0

Joined: 20 Feb 2022
Posts: 8

PostPosted: Sun Jul 28, 2024 11:08 am    Post subject: Reply with quote

Hello again! Ive been playing around with scaling up the script to handle multiple variables, and it works great. Thanks so much to everyone that helped.

I'm trying to make things a bit more sophisticated now, and am running into trouble. Previously, I was telling the script to increment the value until it hits a cap, and then stop. I was inputting the cap value by hand, which works, but is clunky and requires constant editing as the game goes on. I'd rather dynamically read the in-game cap and use that instead. But I ger nil value errors for my new capacity variable when I try this.

Code:
local Desc1 = ""
local DescCap = ""
local oldv1 = 0

if ihTim1 then ihTim1.Destroy() ihTim1=nil end
ihTim1=createTimer() ihTim1.Interval = 900
ihTim1.Enabled=false

if ihTim2 then ihTim2.Destroy() ihTim2=nil end
ihTim2=createTimer() ihTim2.Interval = 1000
ihTim2.Enabled=false

function ItemHack()
oldv1 = tonumber(AddressList.getMemoryRecordByDescription(Desc1).Value)
DescCap = tonumber(AddressList.getMemoryRecordByDescription(DescCap).Value)
    local mr = AddressList.getMemoryRecordByDescription(Desc1)
    local mrcap = AddressList.getMemoryRecordByDescription(DescCap)
    oldv1 = tonumber(oldv1) + tonumber(1.5)
    mr.Value = tostring(oldv1)
    mrcap.Value = tostring(DescCap)
    if oldv1 >= DescCap then ihTim2.Enabled=false end
end

function tracking1()
   Desc1 = "Gold"
   DescCap = "GoldMax"
mem1 = getAddressList().getMemoryRecordByDescription(Desc1)
memcap = getAddressList().getMemoryRecordByDescription(DescCap)
 if tonumber(mem1.Value) <= tonumber(memcap.Value) then
   ihTim2.Enabled=true
 end
end

ihTim1.OnTimer = tracking1
ihTim2.OnTimer = ItemHack

-- start check ..
ihTim1.Enabled=true


I'm trying to follow the example, but I guess this isn't going to be as simple as just adding a few extra lines. Not sure what I'm doing wrong, but it's obviously something.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1528

PostPosted: Sun Jul 28, 2024 2:34 pm    Post subject: Reply with quote

Code:
local Desc1 = ""
local DescCap = ""
local oldv1 = 0

if ihTim1 then ihTim1.Destroy() ihTim1=nil end
ihTim1=createTimer() ihTim1.Interval = 900
ihTim1.Enabled=false

if ihTim2 then ihTim2.Destroy() ihTim2=nil end
ihTim2=createTimer() ihTim2.Interval = 1000
ihTim2.Enabled=false

function ItemHack()
oldv1 = tonumber(AddressList.getMemoryRecordByDescription(Desc1).Value)

    local mr = AddressList.getMemoryRecordByDescription(Desc1)
    oldv1 = tonumber(oldv1) + tonumber(1.5)
    mr.Value = tostring(oldv1)
    if oldv1 >= tonumber(DescCap.Value) then ihTim2.Enabled=false end
    -- If the sum of the increased value exceeds the maximum value; Round it up to the maximum value!
    if tonumber(mr.Value) > tonumber(DescCap.Value) then
     mr.Value = tostring(DescCap.Value)
    end
end

function tracking1()
   Desc1 = "Anchovy"
   DescCap = getAddressList().getMemoryRecordByDescription("GoldMax")
mem1 = getAddressList().getMemoryRecordByDescription(Desc1)
 if tonumber(mem1.Value) <= tonumber(DescCap.Value) then
   ihTim2.Enabled=true
 end
end

ihTim1.OnTimer = tracking1
ihTim2.OnTimer = ItemHack

-- start check ..
ihTim1.Enabled=true

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
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