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 


Code improvement

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

Joined: 17 Jan 2018
Posts: 202

PostPosted: Fri Jun 05, 2020 12:06 pm    Post subject: Code improvement Reply with quote

Is there a way to improve the following code?
Code:
  for b = 0, 8 do
  for x = 0, 15 do
  UDF1['CEImage'..145+x+b*16].OnClick = function(sender1)
  currentmat = x+b*16
  currentmataddr = x*4+b*0x84
  local valuemat1 = readBytes(basaddr + 0x005E8664+currentmataddr)
  UDF1['CEComboBox'..1].ItemIndex = valuemat1
  local ap = bAnd(0xFFFFFF, readInteger(basaddr + 0x005E8665+currentmataddr))
  UDF1['CEEdit'..1].Text = ap
  UDF1.CEImage289.setPosition(UDF1['CEImage'..145+x+b*16].Left-4, UDF1['CEImage'..145+x+b*16].Top-4)
  end
  end
  end

  UDF1['CEEdit'..1].OnKeyDown = function(sender2)
  if isKeyPressed(VK_RETURN) then
  write3bytes(UDF1['CEEdit'..1], basaddr + 0x005E8665+currentmataddr)
  UDF1['CEImage'..145+currentmat].OnClick() -- many other controls also call this function
  end
  end

screenshot: https://funkyimg.com/view/2UeTR
When CEImage is clicked, the code reads the bytes from the corresponding addresses and moves the UDF1.CEImage289 (blue square) to the position of the CEImage that was clicked. A blue square marks an image that was clicked.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Fri Jun 05, 2020 6:08 pm    Post subject: Reply with quote

My improvisation is something like this. I am focus on image click event only.

Code:
-- add your pictures image name here,  image type is png image
image_table={'pic001','pic002','pic003','pic004','pic005'}
imgstrs ={}

form = createForm(true)
form.Position = poDesktopCenter
form.Width = 236
form.Height = 80

hglight = createImage(form)  --- this is your UDF1.CEImage289 image
hglight.Height = 40
hglight.Width = 40
hglight.Left = 15
hglight.Top = 20
hglight.visible=false
hglight.sendToBack()
hglight.stretch=true
hglight.Picture.loadFromStream(findTableFile('hglight.jpg').Stream)

local cleft=15
local i
---- Create images on Form with global name as 'ing' + i = Pictures number
for i=1,#image_table do
  local img_name='img'..i
  local img=createImage(form)
  _G[img_name]=img
  img.Top = 20
  img.Left = cleft
  img.Width = 40
  img.Height = 40
  img.stretch = true
  cleft = cleft + 40

---- Assign a variable for captured Picture from table file
  local in_image

---- Add captured picture to temporary table
  if imgstrs[i] then
  in_image=imgstrs[i]
  else

---- Set captured pictures to each panels
  img.Picture.loadFromStream(findTableFile(image_table[i]..'.png').Stream)
  imgstrs[i]=img

---- Apply a function to hi-lighting picture bg when mouse clicking on it
  img.OnClick = function()
  hglight.visible=true
  hglight.setPosition(img.Left, img.Top)

  --- do something here with your ADDREES, VALUE, etc

  end
 end
end

---- Show the form
form.show()

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 202

PostPosted: Sat Jun 06, 2020 3:43 am    Post subject: Reply with quote

I read one year ago, that one programmer said: it’s better not to use _G in code. Perhaps for the same reason that we use local variables instead of global ones. But, my knowledge is not enough to say whether it is true or not.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sat Jun 06, 2020 4:18 am    Post subject: Reply with quote

Code:
I read one year ago, that one programmer said: it’s better not to use _G in code.


Then you need ask or search for the reasons not to use _G or global variable. Or you can hang it inside your mind like an unanswered question forever.

My question, have you ever use ENV statement on your Lua script?

On the sample above I use _G as a table (actually _G is a table).

Example:

Code:
-- script 1
local LocalValue = "This is not saved across scripts"
_G.GlobalValue = "This is saved across scripts"

Now if you have another script with the following:

Code:
-- script 2
print(LocalValue) --this will print nil, localvalue was only defined in the other
print(_G.GlobalValue) --this will print 'This is saved across scripts'


But the code above works on pure Lua scripting. Not work if use Cheat Engine Lua.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
exohaxor
Expert Cheater
Reputation: 1

Joined: 02 Sep 2018
Posts: 101

PostPosted: Sat Jun 06, 2020 5:34 am    Post subject: Reply with quote

local variables are usually faster like %25 more fast
source: https://www.lua.org/gems/sample.pdf

_________________
hi
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sat Jun 06, 2020 6:37 am    Post subject: Reply with quote

exohaxor wrote:
local variables are usually faster like %25 more fast
source: https://www.lua.org/gems/sample.pdf


Yes, that is true. Good to know that, thank you.
Anyhow, using local vs global variables depending on the programming scopes and the purposes of the program environment itself. For example, if we work in Lua module, then usually we need to use global variables.

Besides, there are a technical to optimizing global variables as fast as local variables. See:

http://lua-users.org/wiki/OptimisingUsingLocalVariables

Back to @Razi topic, I am imagine if we need make 100 or 200 components for a CE trainer, say it as buttons or panels. Etc. Then, should we make it all one by one?. For me, I will use script like on my example above, OR if not want use _G then I will make it like this, for example (using local):

Code:
gameMain = createForm()
gameMain.setSize(400,300)
gameMain.BorderStyle = 'bsToolWindow'
gameMain.Position = 'poScreenCenter'

local rows, cols = 1, 5
local width, height = 50, 50
local gap = 15

local img = {}

for row = 1, rows do
  img[row] = {}
  for col = 1, cols do
    local x = (col - 1) * (width + gap) -- x offset
    local y = (row - 1) * (height + gap) -- y offset
    local newImg = createPanel(gameMain)
    newImg.width = 50
    newImg.height = 50
    newImg.top = y + 15
    newImg.left = x + 15
    newImg.color = '0xF0' -- red
    img[row][col] = newImg
  end
end


Now, that depending on how to improvise the script so that effective to our needed.

Note:
One of main reason I am using CE Lua scripting because that complete with GUI Editor (Form Designer) and that have a compiler to run the Lua script. Of course, I can use others built-in Lua Editor like ZB Studio, wxWidget, Etc. But CE Lua Scripting Editor is very simple and I think I am use CE about 2% to doing game hacks and the remains is for others purpose.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 202

PostPosted: Sat Jun 06, 2020 10:06 am    Post subject: Reply with quote

Corroder wrote:
then I will make it like this, for example (using local):

Trying to combine the first and second of your examples, but a little confused. I'm doing something wrong. Can you fix the mistake?
Code:
-- add your pictures image name here,  image type is gif image
image_table={'pic001','pic002','pic003','pic004','pic005'}
imgstrs ={}

form = createForm(true)
form.Position = poDesktopCenter
form.Width = 436
form.Height = 280

hglight = createImage(form)  --- this is your UDF1.CEImage289 image
hglight.Height = 40
hglight.Width = 40
hglight.Left = 15
hglight.Top = 20
hglight.visible=false
hglight.sendToBack()
hglight.stretch=true
hglight.Picture.loadFromStream(findTableFile('highlight.png').Stream)

local rows, cols = 1, 4
local width, height = 50, 50
local gap = 15

local img = {}

for row = 1, rows do
  img[row] = {}
  for col = 1, cols do
    local x = (col - 1) * (width + gap) -- x offset
    local y = (row - 1) * (height + gap) -- y offset
    local newImg = createImage(form)
    newImg.width = 50
    newImg.height = 50
    newImg.top = y + 15
    newImg.left = x + 15
    img[row][col] = newImg


---- Assign a variable for captured Picture from table file
  local in_image

---- Add captured picture to temporary table
  if imgstrs[i] then
  in_image=imgstrs[i]
  else

---- Set captured pictures to each panels
  newImg.Picture.loadFromStream(findTableFile(image_table[i]..'.gif').Stream)
  imgstrs[i]=newImg

---- Apply a function to hi-lighting picture bg when mouse clicking on it
  newImg.OnClick = function()
  hglight.visible=true
  hglight.setPosition(img.Left, img.Top)

  --- do something here with your ADDREES, VALUE, etc
  end
  end
  end
end

---- Show the form
form.show()
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sat Jun 06, 2020 6:15 pm    Post subject: Reply with quote

Another which more easy to understands.

Code:
image_table={'pic001','pic002','pic003','pic004','pic005'}

if form then form.destroy() end

form = createForm(true)
form.Position = poDesktopCenter
form.Width = 436
form.Height = 280

hglight = createImage(form)  --- this is your UDF1.CEImage289 image
hglight.Height = 50
hglight.Width = 50
hglight.Left = 15
hglight.Top = 20
hglight.visible=false
hglight.sendToBack()
hglight.stretch=true
hglight.Picture.loadFromStream(findTableFile('hglight.jpg').Stream)

local cleft = 15
local ctop = 15

for i = 1, 5 do   --- change 5 to maximum images num you have
 local newImg = createImage(form)
 newImg.width = 50
 newImg.height = 50
 newImg.top = ctop
 newImg.left = cleft
 newImg.Stretch = true
 newImg.Picture.loadFromStream(findTableFile(image_table[i]..'.png').Stream)
 x = newImg.left + newImg.Width + 15
 newImg.Name = 'pic00'..i
 cleft = cleft + 50 -- adjust the left position as images width

 if i == 3 then   -- or i = 7 or i = x (x = how many images per each row)
    cleft = 15
    ctop = ctop + 60 -- adjust the top position as images height
 end

 newImg.OnClick = function()
 hglight.visible=true
 hglight.setPosition(newImg.Left, newImg.Top)
 end

end

form.Show()


This is my way as a noob. I hope some expert masters will show other better ways than this.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
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