Sharing code between games

Hello,

We are developing a number of individual games (that will be wrapped up in mobile apps using CoronaCards) but intend that they all share some common components.

So we have this structure:

game1/main.lua

game2/main.lua

common/menu.lua

common/highscore.lua

Where common/menu.lua and common/highscore.lua needs to be accessible from game1/main.lua and game2/main.lua.

It’s easy if common/ lives under game1/ but then this falls apart when trying to share these with game2/ 

Any tips for achieving this?  

Thanks in advance.

OK, I found a way to do it.  At the top of my main.lua I add this line:

package.path = system.pathForFile(’…/libs’) … ‘/?.lua;’ … package.path

Where libs is the relative folder containing my components folder. Then I can access my components like this:

local menu = require(“components.menu”)

However, this doesn’t work when using CoronaCards as throws a en error due to concatenating null values.

That may work for the simulator, but I seriously doubt it will build.

To ensure you don’t run into problems later, you should maintain per-game build structures.

Having said that, you can keep your shared code in one place.

To do this, put the shared code in a single folder (not in your game structure) then soft link to it.

Softlinking varies by OS.

Here is what I’m saying structurally:

c:\CoronaWork\ | |- game1\ | | | |- main.lua | |- common1\ - soft link to common1 | |- common2\ - soft link to common2 | |- game2\ | | | |- main.lua | |- common1\ - soft link to common1 | |- common3\ - soft link to common3 | |- shared\ | | | |- common1\ - shared folder | |- common2\ - shared folder | |- common3\ - shared folder

Windows Linking Example

cd C:\CoronaWork\game1\ mklink /j .\common1 C:\CoronaWork\shared\common1 mklink /j .\common2 C:\CoronaWork\shared\common2

On Windows, you cannot link files, just folders.  At least not reliably (IMHO).

Someone else will have to show you how to do it on OS X, but I believe it is the ln command.

Also note, using the above system. Builds will get the source as needed, but changes to common** anyplace will change the shared copy.

Lastly, if you push and pull to a git, the pull may replace softlinks with copies.  So be aware of this.

On OSX it’s normally called symlinking. Google will get you results, but here’s one I use:

http://osxdaily.com/2015/08/06/make-symbolic-links-command-line-mac-os-x/

Thanks for the replies folks.  I’m on OSX and am an experienced *nix user so symbolic links are an old friend. :slight_smile:

Unfortunately, that won’t work when deploying to Android or iOS using CoronaCards so I will have to come up with something else.  Most likely I’m thinking it’ll be a git submodule but I loath to use them.

OK, I found a way to do it.  At the top of my main.lua I add this line:

package.path = system.pathForFile(’…/libs’) … ‘/?.lua;’ … package.path

Where libs is the relative folder containing my components folder. Then I can access my components like this:

local menu = require(“components.menu”)

However, this doesn’t work when using CoronaCards as throws a en error due to concatenating null values.

That may work for the simulator, but I seriously doubt it will build.

To ensure you don’t run into problems later, you should maintain per-game build structures.

Having said that, you can keep your shared code in one place.

To do this, put the shared code in a single folder (not in your game structure) then soft link to it.

Softlinking varies by OS.

Here is what I’m saying structurally:

c:\CoronaWork\ | |- game1\ | | | |- main.lua | |- common1\ - soft link to common1 | |- common2\ - soft link to common2 | |- game2\ | | | |- main.lua | |- common1\ - soft link to common1 | |- common3\ - soft link to common3 | |- shared\ | | | |- common1\ - shared folder | |- common2\ - shared folder | |- common3\ - shared folder

Windows Linking Example

cd C:\CoronaWork\game1\ mklink /j .\common1 C:\CoronaWork\shared\common1 mklink /j .\common2 C:\CoronaWork\shared\common2

On Windows, you cannot link files, just folders.  At least not reliably (IMHO).

Someone else will have to show you how to do it on OS X, but I believe it is the ln command.

Also note, using the above system. Builds will get the source as needed, but changes to common** anyplace will change the shared copy.

Lastly, if you push and pull to a git, the pull may replace softlinks with copies.  So be aware of this.

On OSX it’s normally called symlinking. Google will get you results, but here’s one I use:

http://osxdaily.com/2015/08/06/make-symbolic-links-command-line-mac-os-x/

Thanks for the replies folks.  I’m on OSX and am an experienced *nix user so symbolic links are an old friend. :slight_smile:

Unfortunately, that won’t work when deploying to Android or iOS using CoronaCards so I will have to come up with something else.  Most likely I’m thinking it’ll be a git submodule but I loath to use them.