Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6106833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:07:18+00:00 2026-05-23T14:07:18+00:00

Clearly I have some mixed up, but I figured that by using something like

  • 0

Clearly I have some mixed up, but I figured that by using something like this in “main.lua”:

local module = require("module")
local var = "I should be global?"
printthis()

with module.lua containing something like:

function printthis()
    print(var)
end

that printthis(var) would work fine, because now the module.lua code is inside main.lua, no? Instead, printthis has no idea what var is. I read it’s good practice to use “local” on Lua variables when possible, but in this case, do I have to make var global or is there a way for module.lua’s printthis() function to read var properly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:07:19+00:00Added an answer on May 23, 2026 at 2:07 pm

    No. That’s not at all how it works.

    The Lua interpreter provides one global table, referred to as _G normally, unless you’re doing something kinky.

    function printthis()
        print(var)
    end
    

    This translates to, in reality

    _G.printthis = function()
        _G.print(_G.var);
    end
    

    And your code in main is equal to

    local module = _G.require("module")
    local var = "I should be global?"
    _G.printthis()
    

    But when you call printthis– where did _G.var get set? Nowhere. So the variable is nil, like all other accesses to a table where there is nothing at that key.

    It might be inconvenient, but it’s a much better idea in the long run to pass arguments than to set global variables instead. Once you come to change anything about the program, it’s going to be completely impossible to make any changes, as the logic has no structure and you have no idea what happens where without reading every single line of code and understanding it at once. In addition, you can only use each key in one place, because it’s a global table- so I sure hope nobody else wanted to use “var” as a variable name and you don’t mind your code silently failing because you got a global name wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hope I can explain this clearly enough. I have my main form (A)
I have some JavaScript code that is supposed to run on document.ready but I
This is likely something easy to accomplish, but I'm having difficulty even articulating clearly,
Does HUnit have some way of doing approximate equalities? Clearly, this fails: test1 =
Not sure if the question is titled clearly, but here goes. I have some
Assume that I have some HTML page, and a corresponding CSS file. I'd like
I'm not sure if I can explain this clearly. I have some simple HTML
I have some classes that i'd like chain methods of to provide a fluent
I have some code in one of my android apps that catches SQLException, but
Hey php gurus. I'm running into some bizarre class scope problems that clearly have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.