Trying to access variables in a different module file

So I was following this tutorial: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

I’ve encountered a problem with the following error message: “module ‘mydata’ not found:resource(mydata.lu) does not exist in archive.” I made sure that I didn’t misstype anything, but the error still persist.

I have two files; main.lua and mydata.lua. In the mydata.lua file, I have this:

[lua]

local M = {
    M.Playerscore = 0;
    }
return M;

[/lua]

and in the main.lua I have this:

[lua]

local data = require(“mydata”)

function main()

    print("Playe score: " … data.Playerscore);

end

main()

[/lua]

Can anyone help me with this problem of access variables in a different module file? One thing to note is that the mydata.lua file is in the same place as the main.lua directory

Where you have

M.Playerscore = 0;

you want to remove the “M.” part. Apart from table M not even existing yet (it’s still being constructed, after all), the prefixed form  isn’t legal Lua, either.

The rest looks right. The console might have given you a more insightful message, like line numbers in the file in question. It’s true that the module wasn’t found… because it didn’t compile successfully. But that’s usually not much help.  :slight_smile:

So I changed the stuff in mydata.lua file, now it is:

[lua]

local M = {
        Playerscore = 0;
    }
return M;

[/lua]

But I’m still getting the error, “module ‘mydata’ not found:resource (mydata.lu) does not exist in archive”. As for line number, it’s refering to the ‘require’ function in main (line 1)

In that case, maybe there’s a typo in the name of the module file?

change your mydata module to this :

local M = {}

M.Playerscore = 0

return M

also,you don’t need to have semi-colons at the end of the lines.  you can, but they are not needed

also, star crunch way works also, this is just another way to code that mydata file.

yes, your mydata.lua should be in same directory as the main.lua  … and needs to be spelled exact and is case sensitive.

good luck

No typo

Didn’t work either.

I’ll post the full code in case it’s something else that’s messing it up. Bare in mind that this isn’t really a complete game. Something to note, I’m using the graphicCompantibility = 1 (legacy? since I’m more familiar with that

main.lua

[lua]



– main.lua


– requirements
local data = require(“mydata”)
require (“physics”)

– globals
_W = display.contentWidth;
_H = display.contentHeight;

– tables
local availableObj = {}

– definitions
local bg = display.newImage(“images/live_game_bg.png”,0,0);

– physics-obj propeties
local minVelY = 850
local maxVelY = 1100
local minVelX = -200
local maxVelX = 200
local minAngularVelocity = 100
local maxAngularVelocity = 200

– Main code
– ###################

physics.start();

function main()
    print(“Hello World!”);
    print("Width: " … _W … " Height: " … _H);
    print("Playe score: " … data.Playerscore);
    setupBG();
    initJuggleObjects()
end

function initJuggleObjects()
    
    – put green onion object with the table of available objects
    local greenOnion = {}
    greenOnion.image = “images/green_onion.png”
    table.insert(availableObj, greenOnion)
    
end

– Setup the background
function setupBG()
    bg:setReferencePoint(display.CenterReferencePoint);
    bg.x = _W/2;    bg.y = _H/2;
    bg.width = _W;        bg.height = _H;
end

main();

[/lua]

mydata.lua

[lua]

local M = {}
    M.Playerscore = 0;
return M

[/lua]

if it is still giving same error, double check to see that it is in the same directory as main.lua  … if you have a few other projects, be sure you do not have it accidentally in the directory of another project.

Just a quick sanity check: when I refer to a typo, I don’t mean in the require(), but rather for the file itself. Is that one okay?

The other code in main.lua shouldn’t matter, since you’re never getting that far.

6lOmjtq.png

Here’s where the file is at. Unfortunately, I’m going to head off now, this thing is giving me a bit of a headache. I might be good if I took a rest now. Thanks for the help guys, will come back on to check tomorrow

I am guessing you have likely started several projects in the course of trying to learn coding in Corona.  This image does show the mydata.lua is in ‘a’ directory with ‘a’ main.lua, but that doesn’t mean it is in the directory for the app you are presently running in your simulator.  It is possible you had another apps directory highlighted when created and saved mydata.lua.

As it looks now, your spelling and case-sensitivity match what you have in main.lua, so I am still leaning toward, this directory you are showing a screen shot of may be the directory for another project.

Make sure the correct project is highlighted in the directory tree on the left.  That is what I would check first.

The error msg, (if it is hte same as you noted in your first post) is very clear…   it can not find that file… mydata.lua

Stock up on some aspirin … if you are going to code apps, there is likely to be many headaches in the future… but it can all be worth it once you get an app completed and can marvel at you awesome game.

Alright, back again. I know I am not running from a different projects because the deve stuff is on a fresh new harddrive. However, based on what cyberparkstudios said, I decided to make a new temporary project, copy the main and mydata files as well as images folder into this temp project and run the simulator. Lo’ and behold it decides to work. I am going to facedesk for a while, many thanks

Edit: Just in case, the moral of the story here is to make another project with Corona, copy over your files and see if that works

Where you have

M.Playerscore = 0;

you want to remove the “M.” part. Apart from table M not even existing yet (it’s still being constructed, after all), the prefixed form  isn’t legal Lua, either.

The rest looks right. The console might have given you a more insightful message, like line numbers in the file in question. It’s true that the module wasn’t found… because it didn’t compile successfully. But that’s usually not much help.  :slight_smile:

So I changed the stuff in mydata.lua file, now it is:

[lua]

local M = {
        Playerscore = 0;
    }
return M;

[/lua]

But I’m still getting the error, “module ‘mydata’ not found:resource (mydata.lu) does not exist in archive”. As for line number, it’s refering to the ‘require’ function in main (line 1)

In that case, maybe there’s a typo in the name of the module file?

change your mydata module to this :

local M = {}

M.Playerscore = 0

return M

also,you don’t need to have semi-colons at the end of the lines.  you can, but they are not needed

also, star crunch way works also, this is just another way to code that mydata file.

yes, your mydata.lua should be in same directory as the main.lua  … and needs to be spelled exact and is case sensitive.

good luck

No typo

Didn’t work either.

I’ll post the full code in case it’s something else that’s messing it up. Bare in mind that this isn’t really a complete game. Something to note, I’m using the graphicCompantibility = 1 (legacy? since I’m more familiar with that

main.lua

[lua]



– main.lua


– requirements
local data = require(“mydata”)
require (“physics”)

– globals
_W = display.contentWidth;
_H = display.contentHeight;

– tables
local availableObj = {}

– definitions
local bg = display.newImage(“images/live_game_bg.png”,0,0);

– physics-obj propeties
local minVelY = 850
local maxVelY = 1100
local minVelX = -200
local maxVelX = 200
local minAngularVelocity = 100
local maxAngularVelocity = 200

– Main code
– ###################

physics.start();

function main()
    print(“Hello World!”);
    print("Width: " … _W … " Height: " … _H);
    print("Playe score: " … data.Playerscore);
    setupBG();
    initJuggleObjects()
end

function initJuggleObjects()
    
    – put green onion object with the table of available objects
    local greenOnion = {}
    greenOnion.image = “images/green_onion.png”
    table.insert(availableObj, greenOnion)
    
end

– Setup the background
function setupBG()
    bg:setReferencePoint(display.CenterReferencePoint);
    bg.x = _W/2;    bg.y = _H/2;
    bg.width = _W;        bg.height = _H;
end

main();

[/lua]

mydata.lua

[lua]

local M = {}
    M.Playerscore = 0;
return M

[/lua]

if it is still giving same error, double check to see that it is in the same directory as main.lua  … if you have a few other projects, be sure you do not have it accidentally in the directory of another project.

Just a quick sanity check: when I refer to a typo, I don’t mean in the require(), but rather for the file itself. Is that one okay?

The other code in main.lua shouldn’t matter, since you’re never getting that far.

6lOmjtq.png

Here’s where the file is at. Unfortunately, I’m going to head off now, this thing is giving me a bit of a headache. I might be good if I took a rest now. Thanks for the help guys, will come back on to check tomorrow

I am guessing you have likely started several projects in the course of trying to learn coding in Corona.  This image does show the mydata.lua is in ‘a’ directory with ‘a’ main.lua, but that doesn’t mean it is in the directory for the app you are presently running in your simulator.  It is possible you had another apps directory highlighted when created and saved mydata.lua.

As it looks now, your spelling and case-sensitivity match what you have in main.lua, so I am still leaning toward, this directory you are showing a screen shot of may be the directory for another project.

Make sure the correct project is highlighted in the directory tree on the left.  That is what I would check first.

The error msg, (if it is hte same as you noted in your first post) is very clear…   it can not find that file… mydata.lua

Stock up on some aspirin … if you are going to code apps, there is likely to be many headaches in the future… but it can all be worth it once you get an app completed and can marvel at you awesome game.