Victor,
A good way to learn coding, is checking out some of the many good books out there, and mostly reviewing sample code over and over and over. If you want to learn without going to an official school, you need to spend a lot of time looking at code, then retyping code, changing code and experimenting, and then you can start to get a understanding on what is going on. There are many good developers that have posted very helpful tutorials on lua and corona. Search the internet and corona forums and you will find a lot of them. Lua is one of the easiest programming languages to learn. And Corona is a great tool for making good 2d mobile apps. You might be best starting with some of the sample code included int he corona sdk.
Q1 & Q2. Yes, create a separate file and paste that code sample include everything below
– ==========================
– GAME MODULE
– ===========================
into that file.
You can name that file anything you want. Just be careful to avoid naming it something that might be a reserved lua-corona word, like ‘timer’ or such. This can cause issues and can also get confusing.
I used ‘Game.lua’ my example, but ‘game.lua’ works as well. Actually, I typed that example fairly quickly, and the normal style for me is to name all my lua files with lower case. So I normally would have named that file game.lua, not Game.lua … but either is fine.
In my opinion it is important to try and have some consistent style with how you name things … it helps keep things clean and neat and much much easier to debug when there are issues.
So in main, I could of, and normally do it this way:
local Game = require(“game”)
– that is the reverse of what I showed in the example. But again I was typing it out quickly and made a typo error.
– you can upper or lower case any of it. Just be consistent on how you do it.
– I like to upper case first letter of the ‘handle’ to a required file
local Game = require(“game”)
‘Game’ is the handle to that file I am requiring in to main, in this example. I do this, so when I see that ‘uppercase’ word later in the code I know it is a handle to a file I required in.
So, if you decide to name the ‘handle’ ‘Game’ instead of ‘game’ as I initially had it, you will need to uppercase the ‘g’ of game, everywhere in the example where I referenced it as such:
game.data.gameState would need to be Game.data.gameState
Q3. You can put all that sample code ‘above’ the
– ==========================
– GAME MODULE
– ===========================
into home.lua, but you can also split it up like so…
In main.lua
- require the game.lua file
local Game = require(“game”)
- load the data in
Game:loadData()
- print it just so you can see it is working… take out the print when you get ready to deploy your app to the market
print(Game.data.gameState)
Then in home.lua
- require the game.lua file
local Game = require(“game”) – do this above all the composer functions
- I am not that verse with Composer, but I would probably in the create or show function put the rest of the code example
print(Game.data.gameState)
– change the game state
Game.data.gameState = “READY” – or whatever names you give the different game states.
You can do it as I have it, and paste that code into main.lua… because that is still the first code that will execute in your app. Then in your home.lua (at the top of that file) you just need to require the game.lua file just as you did in main.
local Game = require(“game”)
This way, you loaded your data files in right when main is called, and you are accessing the game date, in home.lua where apparently the game action occurs.
Regarding your ‘quick note’. It appears confusing, but the reason you see it print/show ‘NEW LEVEL’ and ‘2’ is because I (just as an example) set those values in the sample code, then saved that info.
game.data.gameState = “NEW LEVEL”
game.data.level = 2
game:saveData()
This was just to show you how to update that data table. In your actual code, you would set those values when you decide they need to be set… clearly after a level or wave of your game is done, you would set those values to the data table to be saved.
You will also, notice, if you run the app again those values are still there. If you want to start back at the default values, that are set in the game.lua file… you need to get to the sandbox, documents/boxes directory and delete that ggData file ‘gameData.box’.
When you start the app again, it will re-create that file with the default data, and you are starting fresh.
Good luck!!
Bob