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 


Timer function and RAM consumption

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Punz1A4
Cheater
Reputation: 0

Joined: 10 Jun 2016
Posts: 25

PostPosted: Mon Jul 25, 2016 4:01 pm    Post subject: Timer function and RAM consumption Reply with quote

So I usually use assembly to write scripts in CE but lately I got a bit more interested in lua and tried writing some basic scripts.
I wrote a script utilizing timer function that increases money by x amount after you press a specific keyboard button.

Code:
{$lua}
[ENABLE]
local x=readInteger("[money_address1]")
function main()
if (isKeyPressed(VK_P)) then
   x=x+1000
   writeInteger("[money_address1]",x)
end
end

t = createTimer(nil, false)
timer_onTimer(t, main)
timer_setInterval(t, 50)
timer_setEnabled(t, true)

[DISABLE]
timer_setEnabled(t, false)

(Ignore the [money_address1], it's obtained in a different scirpt and it is the actual address where the money amount is stored)

Now my problem is that when I run the script and leave it on, the RAM consumed by CE is continuously increasing, leading to ~100MB consumed after 10-15 minutes (the normal amount of memory consumed usually stays at around 50MB).
Is this the way it should be or am I doing something inefficiently?

PS. Is there a way in lua to check if a key has been pressed instead of checking if it's being pressed atm?

EDIT
Thanks to ParkourPenguin for the info 'bout garbage collector function.
Seems like issue is fixed by replacing
Code:
timer_onTimer(t, main)

with
Code:
t.onTimer=function(t) main(); collectgarbage()  end

Cheers Very Happy


Last edited by Punz1A4 on Sat Feb 18, 2017 7:13 pm; edited 1 time in total
Back to top
View user's profile Send private message
hhhuut
Grandmaster Cheater
Reputation: 6

Joined: 08 Feb 2015
Posts: 607

PostPosted: Mon Jul 25, 2016 4:08 pm    Post subject: Reply with quote

I'm not a Lua expert, but I gues your RAM amount keeps increasing because you create a massive amount of timers without destroying one ...
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 218

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Mon Jul 25, 2016 4:16 pm    Post subject: Reply with quote

Just use hotkey instead of timer. Add this to the "Lua Script: Cheat Table"
Code:
function moneyIncreaseHotkeyFunc(hk)
  local x=readInteger("[money_address1]")
  if x then writeInteger("[money_address1]",x+1000) end
end

if moneyIncreaseHotkey~=nil then moneyIncreaseHotkey.destroy(); moneyIncreaseHotkey=nil end
moneyIncreaseHotkey = createHotkey(moneyIncreaseHotkeyFunc,VK_P)
moneyIncreaseHotkey.DelayBetweenActivate = 200




Or this to the addresslist (AA Script):
Code:
{$Lua}

[ENABLE]
function moneyIncreaseHotkeyFunc(hk)
  local x=readInteger("[money_address1]")
  if x then writeInteger("[money_address1]",x+1000) end
end

if moneyIncreaseHotkey~=nil then moneyIncreaseHotkey.destroy(); moneyIncreaseHotkey=nil end
moneyIncreaseHotkey = createHotkey(moneyIncreaseHotkeyFunc,VK_P)
moneyIncreaseHotkey.DelayBetweenActivate = 200


[DISABLE]
if moneyIncreaseHotkey~=nil then moneyIncreaseHotkey.destroy(); moneyIncreaseHotkey=nil end

_________________
Back to top
View user's profile Send private message MSN Messenger
Punz1A4
Cheater
Reputation: 0

Joined: 10 Jun 2016
Posts: 25

PostPosted: Mon Jul 25, 2016 5:08 pm    Post subject: Reply with quote

Thanks a lot for the help - it works great Very Happy

I mostly understand how it works, now that I looked up some stuff though I still don't get few things.

What is the purpose of the if here:
Code:
  if x then writeInteger("[money_address1]",x+1000) end

Since x is assigned earlier isn't this always true?

Also why is this in enable section?
Code:
if moneyIncreaseHotkey~=nil then moneyIncreaseHotkey.destroy(); moneyIncreaseHotkey=nil end

From what I understand it checks if moneyIncreaseHotkey is not undeclared (i.e. is declared?) and then clears it.

And finally what was wrong in the timer function? I don't quite get the part about creating massive amount of timers - there is only one timer with a set interval right?

Sorry for being persistent but I'm just trying to learn Smile
Back to top
View user's profile Send private message
cooleko
Grandmaster Cheater
Reputation: 11

Joined: 04 May 2016
Posts: 717

PostPosted: Mon Jul 25, 2016 10:29 pm    Post subject: Reply with quote

Punz1A4 wrote:
I mostly understand how it works, now that I looked up some stuff though I still don't get few things.

What is the purpose of the if here:
Code:
  if x then writeInteger("[money_address1]",x+1000) end

Since x is assigned earlier isn't this always true?

If the money address doesnt return an integer (invalid address), it will return nil, you cant write to a nil address, so the check stops the code from executing if no address was accessed in the read.

Also why is this in enable section?
Code:
if moneyIncreaseHotkey~=nil then moneyIncreaseHotkey.destroy(); moneyIncreaseHotkey=nil end

From what I understand it checks if moneyIncreaseHotkey is not undeclared (i.e. is declared?) and then clears it.

It ensures no other hotkey is acting on the function call. If one is, it is removed, and then set to the parameters in the next two lines.


And finally what was wrong in the timer function? I don't quite get the part about creating massive amount of timers - there is only one timer with a set interval right?

If you toggle the script twice, you will have two timers created and running, toggle again three. This is because you only disable, not destroy the timer on deactivate.
Back to top
View user's profile Send private message
Punz1A4
Cheater
Reputation: 0

Joined: 10 Jun 2016
Posts: 25

PostPosted: Tue Jul 26, 2016 10:29 am    Post subject: Reply with quote

Thanks for taking time and explaining, now it get it Very Happy

As for the timer though
Quote:
If you toggle the script twice, you will have two timers created and running, toggle again three. This is because you only disable, not destroy the timer on deactivate.

I think I understand but it seems not to be the main case with RAM consumption as it happens after I enable timer and leave it that way. If I deactivate the script it stops draining RAM and so if I choose to destroy timer in [DISABLE] it doesn't change anything, as the issue seems to be in the [ENABLE] section. Confused
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Tue Jul 26, 2016 11:40 am    Post subject: Reply with quote

http://lua-users.org/wiki/GarbageCollectionTutorial
http://www.lua.org/manual/5.3/manual.html#2.5

_________________
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
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