View previous topic :: View next topic |
Author |
Message |
Frouk Master Cheater Reputation: 5
Joined: 22 Jun 2021 Posts: 489 Location: mov dword ptr [Ukraine]
|
Posted: Sun Oct 31, 2021 6:55 am Post subject: Call function 2 times |
|
|
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(__cdecl *Haxing)(HWND hGameWindow) |
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 941
|
Posted: 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 |
|
|
Dark Byte Site Admin Reputation: 457
Joined: 09 May 2003 Posts: 25262 Location: The netherlands
|
Posted: Sun Oct 31, 2021 10:34 am Post subject: |
|
|
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 |
|
|
atom0s Moderator Reputation: 198
Joined: 25 Jan 2006 Posts: 8516 Location: 127.0.0.1
|
Posted: Sun Oct 31, 2021 12:34 pm Post subject: Re: Call function 2 times |
|
|
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 |
|
|
Frouk Master Cheater Reputation: 5
Joined: 22 Jun 2021 Posts: 489 Location: mov dword ptr [Ukraine]
|
Posted: Sun Oct 31, 2021 2:43 pm Post subject: |
|
|
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(__cdecl *Haxing)(HWND hGameWindow) |
|
Back to top |
|
|
lolmanurfunny How do I cheat? Reputation: 0
Joined: 04 Dec 2021 Posts: 1
|
Posted: Sun Dec 05, 2021 12:17 am Post subject: Re: Call function 2 times |
|
|
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 |
|
|
|