0
votes

I am in the process of creating my first ever game for the mobile device and using Corona to do it in the Lua scripting language.

I've heard when creating each level inside a different file it is more memory-smart to put everything that stays the same into one logic file and load it inside each level file. What type of things should be inside this file, functions, sprites etc?

Also, how do I load this in the different files if all my variables are local? I have tried require "logic" - my file name containing all my functions, however I'm not sure how I get the functions written inside to 'activate' because currently they don't.

If you need me to post any code I'm happy to do so. Thanks for reading!


Update

Thanks for giving the idea of tables, I've stored all my functions inside one now, it looks like this:

-- Logic File 
local functionTable = {}
functionTable[1] = onTouch,
functionTable[2] = physicsStart,
functionTable[3] = onComplete,
functionTable[4] = winCondition

However I'm struggling to find a way to call these functions in my level files, here is what I have tried which isn't working, do you know of a way to improve this?

-- Level File
local logic = require "logic"
logic.functionTable[1]
logic.functionTable[2]
logic.functionTable[3]
logic.functionTable[4]

When you say store image names or paths, say I have an image called red_apple located in my 'graphics' folder in the root of my project; would the following be correct?

local imagePath = {}
imagePath[1] = graphics/red_apple

Also, my game is 2D so I'm hoping the learning curve isn't too steep although I understand there will be a lot of things I may have never come across and don't understand however I think that is the best way to learn - and a little bit of naivety never hurt as well :P

1

1 Answers

2
votes

First - if it's your first ever game, start small. If you try to start with everything (levels, etc) you will get overrun by complexity.

Now, regarding your question - it depends. Most of the time, "levels" are just regular lua files.

You can make them return a table. Inside that table you can add any kind of Lua object - strings, functions, other tables, etc.

For example, on this .lua file I'm returning a table with two fields: 'difficulty' (an integer) and 'map' (a multiline string). Those could be used to generate the level on the "level loader" function.

-- level1.lua
local level = {}
level.difficulty = 1
level.map = [[
xxxxxx
x    x
x    x
xxxxxx  
]]
return level

Regarding images - I don't think you can store them directly in the level. But you could (for example) store image names or paths. And make the "map loader" load those new images "on the fly", while reading the map.

But as I said before, that is a bit too complex. Start with something smaller, without levels.