Setting Variables on Required Modules and Retrieving Later

I don’t quite understand how come when I set a variable in one file that required my module and then try to retrieve that variable in another file that I don’t get the changed variable. Here is what I mean:

--scoring.lua local scoring = {} scoring.score = 0 return scoring --file1.lua local scoring = require("scoring") local scoreDisplay = display.newText("", irrelevant) function AddPoint()     scoring.score = scoring.score + 1          scoreDisplay.text = scoring.score end --file2.lua local scoring = require("scoring") --This scene is for when the game is over and the score has been changed alot function scene:enterScene(event)     print(scoring.score) --PRINTS 0 !!! Even though the score is not end  

Is there something obvious I’m not seeing?

I’ve tried out a similar situation right now and it works as expected.
Try printing out just scoring in both modules and see if the table is the same, just to be sure the module is not getting cleaned out somewhere. 

I can’t find any problem with your code. I modified it a little bit and this code works without any problems:

-- Scoring local scoring = {} local score = 0 function scoring.addPoint() print("addPoint") score = score + 1 end function scoring.printScore() print(score) end return scoring -- Main.lua -- Your code here local file1 = require("file1") local file2 = require("file2") -- file1.lua local scoring = require("scoring") scoring.addPoint() -- file2.lua local scoring = require("scoring") scoring.printScore()

I also added score into the table (scoring.score) and it worked as well.

Best regards,

Tomas

I feel so stupid right now. I re read all of my code since I saw that it was working for both of you and realized that I was resetting my score at the end of each game to 0. I really appreciate all of your help. Also, Tom in your code you required both file1 and file2 in main.lua is there a reason for doing that or only for the case that I should want to call something from either of those files there.

I just used them because you had them. I would include “addPoints” in scoring.lua and name my scene “scene_game” or something like that.

Best regards,

Tomas

I’ve tried out a similar situation right now and it works as expected.
Try printing out just scoring in both modules and see if the table is the same, just to be sure the module is not getting cleaned out somewhere. 

I can’t find any problem with your code. I modified it a little bit and this code works without any problems:

-- Scoring local scoring = {} local score = 0 function scoring.addPoint() print("addPoint") score = score + 1 end function scoring.printScore() print(score) end return scoring -- Main.lua -- Your code here local file1 = require("file1") local file2 = require("file2") -- file1.lua local scoring = require("scoring") scoring.addPoint() -- file2.lua local scoring = require("scoring") scoring.printScore()

I also added score into the table (scoring.score) and it worked as well.

Best regards,

Tomas

I feel so stupid right now. I re read all of my code since I saw that it was working for both of you and realized that I was resetting my score at the end of each game to 0. I really appreciate all of your help. Also, Tom in your code you required both file1 and file2 in main.lua is there a reason for doing that or only for the case that I should want to call something from either of those files there.

I just used them because you had them. I would include “addPoints” in scoring.lua and name my scene “scene_game” or something like that.

Best regards,

Tomas