Reputation: 6834
I am new to developing with the Corona SDK as well as Lua. Currently i work strictly with the main.lua file. Is there any way in Lua (im sure there is) to break up the source code into logical, separate files?
Example: 1. Main.lua 2. Entity.lua 3. Settings.lua
Thanks!
Upvotes: 8
Views: 6794
Reputation: 5577
Here's a sample I wrote to demo what you're asking about: http://developer.anscamobile.com/code/object-oriented-sample-game-framework
EDIT: The forum post no longer seems to exist, so here's a link to download the sample code https://app.box.com/shared/uz5beg19h8
It divides things up into multiple files, and uses a sort of decorator pattern to add functionality like "level" or "floating character".
Upvotes: 10
Reputation: 558
You don't need to only work with main.lua file. You can create separate .lua file as you need it like -
1- If you are using many scenes/views/classes for this you can create your separate .lua file for different scenes/views/classes and call these separate .lua files by using storyboard.
2- You can also create separate .lua files for creating objects which you can access in your any class.
3- There are many .lua files like appirater.lua , ui.lua, json.lua provided.
Upvotes: 1
Reputation: 20944
objects.lua:
local M = {}
M.a = 3
return M
main.lua:
local objects = require('objects')
println(objects.a) --> 3
A very good discussion about this is available in the Lua users' wiki: http://lua-users.org/wiki/LuaModuleFunctionCritiqued. You should read it.
Upvotes: 15