How to get values from other lua files.

I made a game playable from the console, but I’d like to turn it into an app.

I have one file called “classMgmt.lua” which pulls the specific values pertaining to each class from .txt files, then makes appropriate adjustments to these values as the game goes along.

If I wanted to access this file in a different lua file, is simply do:

require(“classMgmt”)
Then I’ll be good to go.

When working from my main.lua file, I did that and tried to access a variables and tables from classMgmt.lua and everything just kept appearing as nil.

What I’d like to do is be able to create display.newText with the “text” being strings that are stored in the classMgmt file.
Any help?

I would just go to an ‘init’ scene that does all your initialisation and sets up your newText, and then goes straight on to your first ‘proper’ scene. 

If you want to make variables in one file, or module, available in others you should follow good programming principles and expose them properly. The key issue here is the difference between these:

someLib.lua... -- start of my file local aVariable = "a" bVariable = "b" local lib = {} lib.cVariable = "c" return lib -- end of my file

aVariable is only visible to the code inside this file. In fact, if that were declared inside a loop it would only be visible inside the loop.

bVariable is visible to all code and is known as a global variable. You should avoid these as much as possible because it can cause:

  • Memory problems, like leaks (excessive memory usage consumption which is hard to track down)
  • Overwriting other variables or vice-versa (also hard to track down)
  • Name clashes where you want to use the same name in different files but realise you can’t (usually leads to the above 2 problems)
  • Performance issues (global variables incur a search of all variables declared until the one you want is found)

cVariable is the way to expose a set of variables. Here you are declaring a variable as a value on a table, then you simply return the table at the end of the module file. This allows you to encapsulate all of your module’s functionality in one place and make it reusable - a primary (if not THE primary) tenet of programming.

Thanks for the help everyone. Figured out the global issues I had. But here’s a more specific one, in the pastebin link below is the file that I’m trying to require from and a function that pulls class information for the game from .txt files. This code works perfectly fine in my IDE alone, but when I try to retrieve the variables assigned within the classMgmt.loadStats function, corona is just giving me a bunch of nil’s. 

https://pastebin.com/Kk1sTuux

I know for a fact the issue lies somewhere along the lines of the linesFrom and fileExists functions not working when I run them through main.lua. But I don’t know why.

Why not make each file either a JSON file or a Lua module. Going with the latter, you could do something like:

classMgmt.class = require "classes.class" 

Then classes/class.lua could be:

local class = {} class[1] = { name="Fighter", maxHP = 80, HP = 80, attackDmg = 10, accuracy = 0.5, critChance = 0.05, dodgeChance = 0.25, ability = "whatever you are needing here" },  class[2] = { name="Wizard", maxHP = 20, HP = 10, attackDmg = 1, accuracy = 0.05, critChance = 0.05, dodgeChance = 0.25, ability = "whatever you are needing here" }, return class 

 etc.  Then you simply require the class file in and assign it to your classMgmt structure. This removes all of your need to parse the data and you have a simple Lua file that you can edit in your text editor.

It’s all loaded at once.

Rob

I would just go to an ‘init’ scene that does all your initialisation and sets up your newText, and then goes straight on to your first ‘proper’ scene. 

If you want to make variables in one file, or module, available in others you should follow good programming principles and expose them properly. The key issue here is the difference between these:

someLib.lua... -- start of my file local aVariable = "a" bVariable = "b" local lib = {} lib.cVariable = "c" return lib -- end of my file

aVariable is only visible to the code inside this file. In fact, if that were declared inside a loop it would only be visible inside the loop.

bVariable is visible to all code and is known as a global variable. You should avoid these as much as possible because it can cause:

  • Memory problems, like leaks (excessive memory usage consumption which is hard to track down)
  • Overwriting other variables or vice-versa (also hard to track down)
  • Name clashes where you want to use the same name in different files but realise you can’t (usually leads to the above 2 problems)
  • Performance issues (global variables incur a search of all variables declared until the one you want is found)

cVariable is the way to expose a set of variables. Here you are declaring a variable as a value on a table, then you simply return the table at the end of the module file. This allows you to encapsulate all of your module’s functionality in one place and make it reusable - a primary (if not THE primary) tenet of programming.

Thanks for the help everyone. Figured out the global issues I had. But here’s a more specific one, in the pastebin link below is the file that I’m trying to require from and a function that pulls class information for the game from .txt files. This code works perfectly fine in my IDE alone, but when I try to retrieve the variables assigned within the classMgmt.loadStats function, corona is just giving me a bunch of nil’s. 

https://pastebin.com/Kk1sTuux

I know for a fact the issue lies somewhere along the lines of the linesFrom and fileExists functions not working when I run them through main.lua. But I don’t know why.

Why not make each file either a JSON file or a Lua module. Going with the latter, you could do something like:

classMgmt.class = require "classes.class" 

Then classes/class.lua could be:

local class = {} class[1] = { name="Fighter", maxHP = 80, HP = 80, attackDmg = 10, accuracy = 0.5, critChance = 0.05, dodgeChance = 0.25, ability = "whatever you are needing here" },  class[2] = { name="Wizard", maxHP = 20, HP = 10, attackDmg = 1, accuracy = 0.05, critChance = 0.05, dodgeChance = 0.25, ability = "whatever you are needing here" }, return class 

 etc.  Then you simply require the class file in and assign it to your classMgmt structure. This removes all of your need to parse the data and you have a simple Lua file that you can edit in your text editor.

It’s all loaded at once.

Rob