| View previous topic :: View next topic |
| Author |
Message |
Punz1A4 Cheater
Reputation: 0
Joined: 10 Jun 2016 Posts: 25
|
Posted: Mon Jul 25, 2016 4:01 pm Post subject: Timer function and RAM consumption |
|
|
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
Last edited by Punz1A4 on Sat Feb 18, 2017 7:13 pm; edited 1 time in total |
|
| Back to top |
|
 |
hhhuut Grandmaster Cheater
Reputation: 6
Joined: 08 Feb 2015 Posts: 607
|
Posted: Mon Jul 25, 2016 4:08 pm Post subject: |
|
|
| 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 |
|
 |
mgr.inz.Player I post too much
Reputation: 222
Joined: 07 Nov 2008 Posts: 4438 Location: W kraju nad Wisla. UTC+01:00
|
Posted: Mon Jul 25, 2016 4:16 pm Post subject: |
|
|
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 |
|
 |
Punz1A4 Cheater
Reputation: 0
Joined: 10 Jun 2016 Posts: 25
|
Posted: Mon Jul 25, 2016 5:08 pm Post subject: |
|
|
Thanks a lot for the help - it works great
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
|
|
| Back to top |
|
 |
cooleko Grandmaster Cheater
Reputation: 11
Joined: 04 May 2016 Posts: 717
|
Posted: Mon Jul 25, 2016 10:29 pm Post subject: |
|
|
| 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 |
|
 |
Punz1A4 Cheater
Reputation: 0
Joined: 10 Jun 2016 Posts: 25
|
Posted: Tue Jul 26, 2016 10:29 am Post subject: |
|
|
Thanks for taking time and explaining, now it get it
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.
|
|
| Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4724
|
|
| Back to top |
|
 |
|