Need Help Working With Multiple Files

I built a turn-based game engine for class, but it plays through command prompt, just for proof of concept. But now, I’d like to use Corona to build the actual app.

Is there a way I can write my main.lua file to execute certain strings based on outcomes of functions and variables in other .lua files?

My current problem is that syntax in the other files, are being read by Corona and I’m getting a plethora of errors.

sachinseem,

these links I think answer your question ;

https://www.youtube.com/watch?v=L7NvOH7eLAI

https://docs.coronalabs.com/api/type/EventDispatcher/dispatchEvent.html

Good Luck

Bob

I think I explained myself poorly. Lemme give a basic example of a problem I’m having.

In other.lua:
playerName = “Bob”

In main.lua I wanna be able to do:
label = display.newText( playerName, x, y, font, size)

I’ve ofcourse required other.lua but I keep getting playerName as nil

There are several ways to handle this problem.  The links I game you before is just one way, and a good way.  But you could also do any of these:

make ‘playerName’ global in main.  It is then accessible in both other.lua and main.lua and any other module of the project

(not recommended - but has been done)

make a ‘global module’. A module that holds any variables that you want to be accessed in some or all of your modules. Developers often call these modules ‘common.lua’ ‘global.lua’ etc…

--============================= -- global.lua -- simple module to share variables between modules --============================= local M = {} M.playerName = "any name" M.score = 1000 M.life = 100 return M --============================= -- other.lua --============================= local g = require("global") g.playerName = "Bob" --============================= -- main.lua --============================= local g = require("global") print(" name from global module is " .. g.playerName)

use the 'eventDispatch as noted in previous response

use composer,  see this link :

https://docs.coronalabs.com/api/library/composer/gotoScene.html#going-to-another-scene

@cyberparkstudio’s advice is solid. Personally I would avoid the names “global.lua” and “g” for the table name because if you ask for help in the future, it won’t be obvious to those in the forums that you’re referring to a local data table vs. the system global table and it’s variable _G. 

I do the exact same thing but I call my lua file: mydata.lua and I require it to a local variable named “myData”. It’s purely a personal preference as to what you call it.

Rob

Managed to figure out all the things after you guys helped. Thank you!

I ended up using composer, which is absolutely amazing. Makes things SO MUCH EASIER and more efficient.

sachinseem,

these links I think answer your question ;

https://www.youtube.com/watch?v=L7NvOH7eLAI

https://docs.coronalabs.com/api/type/EventDispatcher/dispatchEvent.html

Good Luck

Bob

I think I explained myself poorly. Lemme give a basic example of a problem I’m having.

In other.lua:
playerName = “Bob”

In main.lua I wanna be able to do:
label = display.newText( playerName, x, y, font, size)

I’ve ofcourse required other.lua but I keep getting playerName as nil

There are several ways to handle this problem.  The links I game you before is just one way, and a good way.  But you could also do any of these:

make ‘playerName’ global in main.  It is then accessible in both other.lua and main.lua and any other module of the project

(not recommended - but has been done)

make a ‘global module’. A module that holds any variables that you want to be accessed in some or all of your modules. Developers often call these modules ‘common.lua’ ‘global.lua’ etc…

--============================= -- global.lua -- simple module to share variables between modules --============================= local M = {} M.playerName = "any name" M.score = 1000 M.life = 100 return M --============================= -- other.lua --============================= local g = require("global") g.playerName = "Bob" --============================= -- main.lua --============================= local g = require("global") print(" name from global module is " .. g.playerName)

use the 'eventDispatch as noted in previous response

use composer,  see this link :

https://docs.coronalabs.com/api/library/composer/gotoScene.html#going-to-another-scene

@cyberparkstudio’s advice is solid. Personally I would avoid the names “global.lua” and “g” for the table name because if you ask for help in the future, it won’t be obvious to those in the forums that you’re referring to a local data table vs. the system global table and it’s variable _G. 

I do the exact same thing but I call my lua file: mydata.lua and I require it to a local variable named “myData”. It’s purely a personal preference as to what you call it.

Rob

Managed to figure out all the things after you guys helped. Thank you!

I ended up using composer, which is absolutely amazing. Makes things SO MUCH EASIER and more efficient.