简体   繁体   English

Lua变量范围,函数转发声明和使用'require'语句重构Lua脚本

[英]Lua variable scope, function forward declaration and refactoring a Lua script by using the 'require' statement

I am finding Lua's scoping rules a bit confusing. 我发现Lua的范围规则有点令人困惑。 I have submitted below, a snippet of a Lua script to highlight the issues I want to ask question(s) about: 我在下面提交了一个Lua脚本片段,以突出我想问的问题:

-- 'forward definitions'
-- could these be moved to the bottom of the script to 'tidy up' the script?
-- could these reside in another file imported by require 'filename' to tidy up the script even more?

[[ Note: This function is accessing variables defined later on/elsewhere in the script
         and is also setting a variable (flag) which is then used elsewhere in the script
]]

function war_is_needed()
    if (goodwill:extendsToAllMen() and peace) then
        if player:isAppointedLeader() then
           economy_is_booming = false
           return true
        else
           economy_is_booming = nil
           return true
        end           
    else
        economy_is_booming = nil
        return false
    end
end



world = WorldFactory:new('rock#3')
player = PlayerFactory:getUser('barney', world)

while (not player:isDead()) do
    peace = world:hasPeace()
    goodwill = world:getGoodwillInfo()

    if war_is_needed() then
       world:goToWar()
    else
       if (not economy_is_booming) then
           player:setNervousState(true)
           player:tryToStartAWar()
       else
           player:setNervousState(false)
           player:liveFrivously()
       end if
    end   
end

My questions are (Ignoring for now that global variables CAN be considered evil) : 我的问题是(现在忽略全局变量可以被认为是邪恶的):

  • Could the function at the top of the script be moved to the bottom of the script to 'tidy up' the script? 脚本顶部的函数是否可以移动到脚本底部以“整理”脚本?
  • Could the function at the top of the script be refactored (ie moved) into a separate file 'warfuncs.lua' and then imported into the script by replacing the function definition with a require 'warfuncs.lua'? 脚本顶部的函数是否可以重构(即移动)到单独的文件'warfuncs.lua'中,然后通过用require'warfuncs.lua'替换函数定义来导入到脚本中?

Whilst answering the above two questions, please bear in mind that the function at the top of the script is accessing variables defined later on/elsewhere in the script and is also setting a variable (flag) which is then used elsewhere in the script. 在回答上述两个问题时,请记住脚本顶部的函数是访问稍后在脚本中/其他地方定义的变量,并且还设置一个变量(标志),然后在脚本的其他地方使用。

Do the scoping rules change if Lua is embedded in C or C++ (where objects may be being created on the heap)? 如果Lua嵌入在C或C ++中(其中可能在堆上创建对象),那么范围规则是否会发生变化?

war_is_needed cannot be moved after the while loop. while循环后无法移动war_is_needed If the while loop were contained in a function, then war_is_needed could be moved after it. 如果while循环包含在函数中,则war_is_needed可以在它之后移动。 Since everything is global, the answer to the second question is yes. 由于一切都是全球性的,第二个问题的答案是肯定的。

lhf is correct, you can refactor your code and it will still work. lhf是正确的,你可以重构你的代码,它仍然可以工作。 Keep in mind, though, that while everything in this particular example script is global, it is often a good idea, if working with variables that are effectively temporary (say, within the scope of a loop or single function), to declare them as local . 但请记住,虽然这个特定示例脚本中的所有内容都是全局的,但如果使用有效临时的变量(例如,在循环或单个函数的范围内),通常是一个好主意,将它们声明为local

In regards to the third question about embedding in C/C++, there is no difference in scope rules within the Lua script, as the application would create a single Lua state, and load your script files. 关于嵌入在C / C ++中的第三个问题,Lua脚本中的范围规则没有区别,因为应用程序将创建单个Lua状态,并加载脚本文件。 Any files that perform a require do so within the scope of the state they were loaded into during the C/C++ application initialization. 执行require的任何文件都是在C / C ++应用程序初始化期间加载的状态范围内执行的。

AFAIK you just have to make sure that you define your functions/variables/ ... before using them, so there this snippet won't give problems AFAIK你必须确保在使用它们之前定义你的函数/变量/ ...所以这个片段不会给出问题

function foo()
    print(a)
end
a="Bar!"
foo()

But this will: 但这会:

a="Bar!"
foo()
function foo()
    print(a)
end

So everything needs to be defined before the function is run, and effectively uses variables. 因此,在运行函数之前需要定义所有内容,并有效地使用变量。 Whether you define them in the main script or dofile/require them doesn't matter. 无论您是在主脚本中定义它们还是dofile / require它们都无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM