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 


Call function 2 times

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

Joined: 22 Jun 2021
Posts: 232

PostPosted: Sun Oct 31, 2021 6:55 am    Post subject: Call function 2 times Reply with quote

So i found a little trick that allows you to call function without specified functions local variable:
Code:
function testFunction()
Enabled = not Enabled
if Enabled then
print('l')
else
print('L')
end
end

it does work,but isn't working if i make Enabled local variable

_________________
void(*Haxing)(int Game)
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 40

Joined: 01 Oct 2008
Posts: 860

PostPosted: Sun Oct 31, 2021 9:42 am    Post subject: This post has 1 review(s) Reply with quote

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
Dark Byte
Site Admin
Reputation: 421

Joined: 09 May 2003
Posts: 24034
Location: The netherlands

PostPosted: Sun Oct 31, 2021 10:34 am    Post subject: Reply with quote

so this doesn't work?
Code:

local Enabled
function testFunction()
Enabled = not Enabled
if Enabled then
print('l')
else
print('L')
end
end

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 183

Joined: 25 Jan 2006
Posts: 8370
Location: 127.0.0.1

PostPosted: Sun Oct 31, 2021 12:34 pm    Post subject: Re: Call function 2 times Reply with quote

Frouk wrote:
So i found a little trick that allows you to call function without specified functions local variable:
Code:
function testFunction()
Enabled = not Enabled
if Enabled then
print('l')
else
print('L')
end
end

it does work,but isn't working if i make Enabled local variable


This isn't a trick, this is how Lua works. If you do not tell it to create a variable with 'local', then it creates it in the global namespace. This is generally not ideal or suggested to do and is seen as 'polluting the global namespace'.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Frouk
Expert Cheater
Reputation: 0

Joined: 22 Jun 2021
Posts: 232

PostPosted: Sun Oct 31, 2021 2:43 pm    Post subject: Reply with quote

Dark Byte wrote:
so this doesn't work?
Code:

local Enabled
function testFunction()
Enabled = not Enabled
if Enabled then
print('l')
else
print('L')
end
end

works fine(btw i didn't know that we can create local Enabled and use it in function without local),thank you

_________________
void(*Haxing)(int Game)
Back to top
View user's profile Send private message
lolmanurfunny
How do I cheat?
Reputation: 0

Joined: 04 Dec 2021
Posts: 1

PostPosted: Sun Dec 05, 2021 12:17 am    Post subject: Re: Call function 2 times Reply with quote

Frouk wrote:
So i found a little trick that allows you to call function without specified functions local variable:
Code:
function testFunction()
Enabled = not Enabled
if Enabled then
print('l')
else
print('L')
end
end

it does work,but isn't working if i make Enabled local variable


Lua was intended to operate this way. You're declaring a new global variable since there was no local variable with the name "Enabled" already declared within that function's scope and not having a local variable above the function now means you are by default declaring it within that function which had it not been a global variable but instead a local variable then you would be declaring the variable within the closure of the function. I recommend you don't use the global table unless you 'have' to since globals are less efficient than locals/take longer to index plus any incorrect spelling of your globals will create new ones if you aren't careful. If you're making a global of any kind, you can declare and index it with '_G' so you can more easily differentiate your variables. (example, '_G.Enabled = true' is the same as 'Enabled = true' except you don't exactly know if "Enabled" is a variable from the local scope or the _G/global table when you are altering the value from within a function/closure.)
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