 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
Razi Expert Cheater
Reputation: 1
Joined: 17 Jan 2018 Posts: 205
|
Posted: Fri Aug 03, 2018 3:49 am Post subject: How to correctly use the function to optimize code? |
|
|
How to correctly use the function to optimize code? I need to write the following code 20 times. What do I need to do, to avoid writing the following code 20 times?
For example:
Code: | endata_DextLuck = {}
for x = 1, 12 do
endata_DextLuck[x] = UDF1["CEEdit"..x+532]
end
--write 1 byte
for x = 0, 11 do
local offset = 104*(x // 2) + (x % 2)
local current_endata_DextLuck = endata_DextLuck[x+1]
endata_DextLuck[x+1].OnKeyPress = function(sender, key)
timer.Enabled = false
local a = tonumber(sender.Text)
if (a~=nil) and isKeyPressed(VK_RETURN) then
writeBytes(0x00A445C8+offset, current_endata_DextLuck.Text)
timer.Enabled = true
end
return key
end
end |
and other similar code
Code: | endata_AttMatt = {}
for x = 1, 14 do
endata_AttMatt[x] = UDF1["CEEdit"..x+512]
end
--write 1 byte
for x = 0, 13 do
local offset = 68*(x // 2) + (x % 2)
local current_endata_AttMatt = endata_AttMatt[x+1]
endata_AttMatt[x+1].OnKeyPress = function(sender, key)
timer.Enabled = false
local a = tonumber(sender.Text)
if (a~=nil) and isKeyPressed(VK_RETURN) then
writeBytes(0x00A44D30+offset, current_endata_AttMatt.Text)
timer.Enabled = true
end
return key
end
end |
Only two parameters are changed, the address (0x00A445C8 => 0x00A44D30) and the name of the table (endata_DextLuck => endata_AttMatt). (Perhaps I need to create a function with parameters).
Already figured out. Answer to my own question:
Code: | local function write1byte(tablename, address)
local current = tablename
tablename.OnKeyPress = function(sender, key)
timer.Enabled = false
local a = tonumber(sender.Text)
if (a~=nil) and isKeyPressed(VK_RETURN) then
writeBytes(address, current.Text)
timer.Enabled = true
end
return key
end
end
for x = 1, 135 do
local offset = 0x84*((x-1) // 15) + (((x-1) % 15) + 1)
write1byte(cacheTable_CEEdits[x], 0x009E8624+offset)
end |
|
|
Back to top |
|
 |
FreeER Grandmaster Cheater Supreme
Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Fri Aug 03, 2018 9:14 am Post subject: |
|
|
Quote: | What do I need to do, to avoid writing the following code 20 times? |
Since you already found a specific answer (congrats btw), the general one is loops, functions, maybe a few tables. Maybe an object if you're into OOP
btw. tablename should probably be table (or tbl etc to avoid overwriting lua's table inside the function) since if it really was a "name" it'd be a string and a string doesn't have a .OnKeyPressed property.
Write1Byte also isn't the most descriptive name, for one thing it doesn't write one, or any, bytes. It creates an event listener that will write a byte in the future. Perhaps EventSetup would be a more descriptive name, but only a bit...
also, current should be unnecessary, it should be able to access the table using tablename (or whatever you renamed it, if you did)... not sure if there's any difference in performance (though it'd really only matter if it was being called very quickly). Is it the same as sender? Because you check sender.Text to see if it can be converted to a number and then write current.Text... :shrug: If it is the same as the sender then you could use the exact same handler and just have it lookup the address in a table or... i think many components can have an extra pointer-sized Tag property that you are free to store data in, so you could have a loop over all the components setting the event handler to one global function and the Tag to the address that should be used for that sender.
as for the offset calculation... that seems a bit complex to me Testing what it generates with Code: | for x = 1, 135 do
local offset = 0x84*((x-1) // 15) + (((x-1) % 15) + 1)
if (x-1)%15 == 0 then print('') end
print(offset)
end | I see Code: | 1
...
15
+0x84 from start, 0x76 from end
133
...
147
+0x84
265
...
279
+0x84
397
...
411
529
...
543
661
...
675
793
...
807
925
...
939
+0x84
1057
...
1071 |
so I'd probably implement it as a simple nested loop Code: | for i = 0, 8 do
for j = 1, 15 do
local offset = 0x84*i + j
print(offset)
if j%15 == 0 then print('') end
end
end |
slightly longer code yes, but also more obvious that there are 9 groups of 15 elements without having to divide 135 by 15 (which involves making a guess based on the // 15 and % 15).
_________________
|
|
Back to top |
|
 |
Razi Expert Cheater
Reputation: 1
Joined: 17 Jan 2018 Posts: 205
|
Posted: Fri Aug 03, 2018 1:57 pm Post subject: |
|
|
@FreeER, Thanks for the constructive analysis. That's why I wrote the answer here, to find small errors and inconsistencies in the code, and also, to improve the code.
Edited: Formula changed for the offset calculation, may be it will be a bit simpler:
Code: | for x = 0, 134 do
local offset = 0x84*(x // 15) + x % 15
write1byte(cacheTable_CEEdits[x+1], 0x009E8625+offset)
end | address changed (0x009E8624 => 0x009E8625)
|
|
Back to top |
|
 |
|
|
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
|
|