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 


Cheat Engine Forum Index
PostGo back to topic
panraven
Grandmaster Cheater
Reputation: 55
Joined: 01 Oct 2008
Posts: 942

PostPosted: Sun Oct 31, 2021 9:42 am    Post subject:

Something like, instead of define a regular function, you define the __call function within a table it attached, and use the table (self in the function) to store variables.
Code:

testfunction = testfunction or   -- prevent redefine the table
  setmetatable({},{ __call = function(self) -- this __call metamethod to make the table callable
    -- define the function
    self.Enabled = not self.Enabled
    if self.Enabled then print'ENABLED' else print'off'end
  end
})


alternatively, define a named context table fetching function module, and use like:
Code:

local  ctxs = "ctxs"
if type(package.loaded[ctxs])~='function'then
  local contexts = {}
  package.loaded[ctxs] = function (ctxname)
    local ctx = contexts[ctxname or '']
    if type(ctx)~='table' then
      ctx = {}
      contexts[ctxname]=ctx
    end
    return ctx
  end
end
-- test
function ctx1()
  local ctx = require(ctxs)('ctx1')-- require by string variable
  ctx.Enabled = not ctx.Enabled
  print('ctx1',tostring(ctx.Enabled))
end

function ctx2()
  local ctx = require'ctxs'('ctx2')-- require by string constant of module name
  ctx.Enabled = not ctx.Enabled
  print('\tctx2',tostring(ctx.Enabled))
end

function prtctx()
  local ctxs = require'ctxs'
  local ctx1,ctx2 = ctxs'ctx1', ctxs'ctx2'
  print(1,tostring(ctx1.Enabled),2,tostring(ctx2.Enabled))
end

prtctx()
ctx1()
ctx2()
ctx2()
ctx1()
ctx1()

The later is more flexible, for instance, if the ctx name is provided, same function can be use in difference place, eg.
Code:

function toggle(ctxname, addr)
  local ctx = require'ctxs'(ctxname)
  ctx.Enabled = not ctx.Enabled
  if ctx.Enabled then writeBytes(addr,1)else writeBytes(addr,0) end
end
-- thin in different aa scripts of a ct,
...
toggle('GodMode',bGodAddr)
...
toggle('OHKMode',bOHKAddr)
...

_________________
- Retarded.
Back to top
View user's profile Send private message
Post reviews:   Approve 1
Author Review
ByTransient
Review: Approve
Post reference:
ReviewPosted: Sun Oct 31, 2021 11:12 pm

Good ..
Back to top
View user's profile Send private message
Display:  
Cheat Engine Forum Index


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites