1

I am finding Lua's scoping rules a bit confusing. I have submitted below, a snippet of a Lua script to highlight the issues I want to ask question(s) about:

-- '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 (i.e. moved) into a separate file 'warfuncs.lua' and then imported into the script by replacing the function definition with a 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)?

3 Answers 3

2

war_is_needed cannot be moved after the while loop. If the while loop were contained in a function, then war_is_needed could be moved after it. Since everything is global, the answer to the second question is yes.

Sign up to request clarification or add additional context in comments.

Comments

1

lhf is correct, you can refactor your code and it will still work. 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.

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. Any files that perform a require do so within the scope of the state they were loaded into during the C/C++ application initialization.

Comments

1

AFAIK you just have to make sure that you define your functions/variables/ ... before using them, so there this snippet won't give problems

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.