Functions in Lua are an integral part of any Corona script, with one of the primary benefits being the ability to run an entire block of code just by simply calling the function. This usefulness really shines when the function needs to be called several different times throughout runtime.
But what happens if you need to use the same functions across multiple modules? For instance, if you have many different levels, should you really have to write (or copy/paste) the code for creating the main character, drawing the score display, pause buttons, etc. (things common to all levels) over and over again? Of course not.
Maintaining the same code across multiple modules/screens might not seem so bad with just one or two levels, but what about when your game grows to twenty levels and you need to change a simple option on the pause screen? My guess is that you’d rather modify one file, rather than all twenty.
There are several different ways you can modularize functions that are common to many modules/screens, but I’m going to walk you through what I believe is the easiest method to implement as well as understand.
But before you continue with this tutorial, I highly recommend that you take a moment to gain an understanding of how to use external modules in Corona first.
I’ll elaborate on the simple game scenario I described a moment ago. Let’s say we are creating a game with several levels, and the things common to each level are:
The main character (as well as the character’s associated functions).
Number that represent the game’s score.
Pause button and menu.
We’re not going to construct a complete game here, we’ll instead create a few sample functions to illustrate how you might apply the same logic to your actual project.
The first thing we’ll do is create a separate module called gamefunctions.lua, which we’ll use for functions that are common to all (or most) levels. Here’s what your gamefunctions.lua might look like:
As you can see from the code sample above, we have three separate functions for creating a character (and a jump method for the player), a score display (plus a method to change the score), and a pause button.
How, you can leverage the code above in as many levels as you want! Here’s the beginnings of an example level module:
You see, returning the player, score, and pause button from the respective functions stores an instance of the objects into the specified variables in level1.lua. You can also see how the methods and properties are accessed (lines 26 and 27).
Of course, these are very simplistic examples and don’t actually reflect a real game, but the point wasn’t to make an actual game right now, but to show you how you can write functions once, and leverage them across several different screens/modules.
Now, let’s say you want to add a “shoot” method to the player. Using this technique, you’ll only need to modify gamefunctions.lua and add code to the newPlayer function code block, and instantly any functions that call newPlayer() will have access to the new method! Simple as that.
Not Just for Games…
Although the example I provided had to do with creating game functions common to multiple levels, this concept can be applied to just about any app. Your “gamefunctions.lua” could be anything really, and serve to hold all the page creation/turning functions for your eBook app, or the functions for pulling data from a remote server in your business app—the possibilities are endless.
Just about every app can take advantage of this technique to not only access common functions across multiple screens/modules, but will also serve to help keep your code clean, organized, and easily maintained.