如何忽略“如果”争论lua

发布于 2025-02-12 17:45:47 字数 471 浏览 0 评论 0原文

我想绕过/忽略Global中的“ if”参数的回报。
例子。 (xy代表MISC ARG)

全局脚本第1行1

if x == y then
os.exit()
return
end

function HOME() 
    -- some codes
end 

HOME()

-- end script

我将使用PCALL从另一个脚本加载此脚本(..

我想知道,如何忽略所有全局参数,因此,当该脚本直接到HOMER(),如果此脚本()甚至可能。 我知道如果我可以在主脚本中写入

pcall("script above")
HOME()

我可以得到的home(),但是只有上面的脚本完全正确运行(编辑脚本也不是一个选项,我可以做,

os.exit=print
pcall(

但“返回”仍然退出脚本

I want to bypass/ignore the returns from "if" arguments in global.
example. (x y represent misc arg)

global script line 1

if x == y then
os.exit()
return
end

function HOME() 
    -- some codes
end 

HOME()

-- end script

I'll be loading this script from another script by using pcall(..

I want to know, how to ignore all global arguments, so when that script goes straight to HOME() if this is even possible. also, I'll be doing this from android phone, using a program that has limited lua functions, things like popen, execute are disabled
I know if I write in the main script

pcall("script above")
HOME()

I can goto HOME() but only if the script above runs completely through correctly (editing the script is also not an option, I could do

os.exit=print
pcall(

but the "return" still exits the script

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2025-02-19 17:45:47

您想为功能设置环境。

local function some_script()
    if x == y then
        print("Bye!")
        os.exit()
        return
    end

    function HOME()
        print("HOME!")
    end

    HOME()
end

-- code calling the "script"

local fn = some_script -- alt: use loadfile/load

local dummy = 1
local myenv = {}
setmetatable(myenv, {
    __index = function (self, key)
        -- check here if you want to "overwrite" the variables
        if key == "x" or key == "y" then
            dummy = dummy + 1
            return dummy
        end

        return _G[key]
    end
})

setfenv(fn, myenv)
fn() -- prints: HOME!

You want to set the environment for the function.

local function some_script()
    if x == y then
        print("Bye!")
        os.exit()
        return
    end

    function HOME()
        print("HOME!")
    end

    HOME()
end

-- code calling the "script"

local fn = some_script -- alt: use loadfile/load

local dummy = 1
local myenv = {}
setmetatable(myenv, {
    __index = function (self, key)
        -- check here if you want to "overwrite" the variables
        if key == "x" or key == "y" then
            dummy = dummy + 1
            return dummy
        end

        return _G[key]
    end
})

setfenv(fn, myenv)
fn() -- prints: HOME!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文