Using Codea for the IPad

by TheSolderKing in Circuits > Apple

677 Views, 8 Favorites, 0 Comments

Using Codea for the IPad

Codea by Two Lives Left is one of the highest quality coding environments available for the iPad. Using Codea on the iPad is by far the best coding experience one could hope to have on an iPad. For those of you who are new to Codea, it is a code editor on the Apple App Store that is based off of Lua. Lua is a high level programming language that is fun to write and learn. Codea has many features, including syntax highlighting, a beautiful editor, autocompletion, search and replace, many useful libraries included, the ability to run the programs you write, graphics features, and all that you could hope for on an iPad code editor. Even if your native language is not English, Codea is translated into a myriad of other languages. This instructable will show you how to use Codea. By no means will this teach you Lua, however, as that is something that needs to be done on your own time. I do recommend having some past experience with programming before jumping right in to Codea, perhaps with ProcessingJS on Khan Academy (I only say that because that was my first coding experience). As a final note, do not give up. At times it may seem too hard or too much trouble to learn how to use Codea, but it is so worth it.
DISCLAIMER: This tutorial is in no way meant to teach programming to the reader. It is simply meant to help the reader understand how to use the great development platform that is Codea. Codea is in no way mine, and rights to Codea belong to Two Lives Left.

Downloading Codea

On the App Store, Codea is available for US $9.99. This may seem like a lot of money, but believe me, it is worth it. If you devote enough time and effort to Codea, you could even earn that money back because Codea has support for exporting as an xCode project. This means that your programs with Codea can be put onto the App Store. As an example, just look at Cargo Bot. Cargo bot is on the App Store and was programmed with Codea. Codea even comes with the source code for Cargo Bot and many other examples for you to look at pre installed. On the App Store, Codea is the most popular coding environment. Just take a look at the reviews! I also recommend downloading Sprite Something by Terrible Games for 5.99(tell me if this is still correct) for making pixel art and graphics for your games.

Open Up Codea

Upon opening up Codea, figure 1 shows what you will see. The examples that come with Codea are many and varied, and are to be found on the top bar of squares. The lower bar of squares in the photo is where your projects will be stored.This will be empty when you first open up Codea. You can scroll through all of the examples at the top and take a look at them. In the top left you will see a little button that when pressed will open up a sidebar. These features are pretty advanced, so we will ignore them for now. In the top right is a gear icon, which opens up the settings menu. Here you can simply change the look of Codea. Take a moment to just look around the opening menu, then move on to the next step.

Open Up the Examples

The examples are a great way to see what can be done with Codea. I highly suggest looking through all of them, and perhaps looking at the code quickly. One of the most complex and amazing is the 3D Roller Coaster. This is coded by Andrew Stacy and takes a look at the 3D features of Codea. It is highlighted in figures 1 and 2 above. Figure 1 shows the code and figure 2 shows the code running in the in-app runtime. This is also an opportune time to take a look at the editor and run window. This is one of the most complex examples, but it is a good one to start with because it is inspiring. Do not expect to be coding this in a few weeks, or perhaps a few years, unless you are already a complete coding Guru and already know simple Lua. The editor can either have a light or dark background that can be changed any time in settings, and the tabs at the top are called classes. If you are already a programmer, you know what these are. They are simply different sections of the program, a bit like functions(methods), that link up together. All the green lines with -- before them are comments, and these simply are for easier readability and have no effect on the functionality of the program. At the bottom you may see some buttons. These are the useful parts of the IDE. From left to right they are:
Indent

Search(doubles as find+replace)

Look(This is a complete syntax library for quick referral)

Math symbols(tap on this for quick access to any of the math symbols you'll need)

Move cursor

Move selection bounds(used for quickly selecting words or lines)

Insert parentheses

Insert quotes
(Both insert quotes and insert parentheses encase the selection in quotes/parentheses when text is selected)

+=: Holding on this brings up a whole menu of +=, -=, /=, and x= operators.(Lua has no support for the whole ++, --, or += sort of thing, so you must type var=var+1 instead of var++, but Codea makes up for this by allowing you to type a variable, and then instead of typing "= var + _" after if, you can instead press this button and Codea will do it for you. This is simple but a bit hard to explain so you should try it out for yourself by typing a random word and then pressing this button).

=: This will insert an ordinary assignment equals sign if just tapped, but if the button is held, it will bring up a menu of other commonly used symbols used in if statements.

There are also some options that only present themselves alongside Apple's normal copy/paste bar. As shown in figure 4, there is also the option to re-indent when text is selected. Even though Codea isn't whitespace sensitive, it makes the code look prettier. The other option shown is "Lookup" which opens the selected text in the syntax reference, if it is defined there.
And that's it! The running window is simpler. To run the program, press the play button in the text editor. This will start the program and open up the running window. In the lower left there are five buttons. These are pretty self explanatory, but from left to right they are:
Back to editor
Pause/Play
Restart
Take a picture
Take a video
You will also see a button in the top left. This just brings up the console as a sidebar. The console can have input in the form of parameters, and output in the form of text in the output field. In this program, however, none of that is present as the console is mainly for debugging purposes. For more info on the console sidebar, look into the Parameters example. Figure 3 shows what that looks like.
I highly recommend looking at all of the other examples, but for now I am going on to the next step.

Your First Program

After what you just saw, your first program may be a little under-whelming. To create a new program, go to the title screen and click the + button. This will prompt you with a name for your project, as shown in figure 1. Enter a name, then tap Create. This will bring up the screen shown in figure 2. As you can see, the nice folks at Two Lives Left have done a little programming for you. This is the classic "Hello World" Program, where the words Hello World are written, A.K.A printed, to the output console as shown in figure 3. Of course, I am not here to teach you programming, but I will tell you that this is accomplished by the print() command. This is your first program. To modify it, you can try playing around with the print() command, making it say different things, or perhaps moving into the function draw. This, you'll notice, prints the words over and over again. Why is this? Because the setup function only runs once, at the beginning of the program, and is used primarily for the definition of variables(Which is easy in Lua, as it is so high level that variables don't need their type defined, and can change type in the middle of a program). The draw function, however, runs once every frame. This can be 60 FPS, which means everything in draw is running 60 times a second, but more likely(especially for complex programs with 3D graphics) it will be a bit less that that. This is the code editor.

Making More Complex Programs

To make more complex programs, you need to know how to, well...program. Even if you already know some hardcore language like C or Java(Ok, not really that hard core, but compared to Lua?...) you'll need to know Lua well. "But how do I learn Lua?" you might ask. "I'm glad you asked," I would subsequently reply. "Why's that?" you might ask. "Because Codea has one of the best community forums of any programming IDE EVER!" I would fervently reply(Ok I'll stop). Of course, that is just my opinion, but it really is awesome. Some of the most experienced coders hang around there and are happy to answer any question you might have. Just don't ask them to write code for you. That is one thing you NEVER do on any coding forum. They do share their own code, so if you want to take a look at some of the best, go to the "Code Sharing" section of the forums. To get to these forums, just click on the "Codea Forums" button on the main screen of Codea, or google Codea Forums. The forums alone, however, will not teach you Lua. It will take time, and hours of poring over the in-app documentation that I love so much(again, the little eye button in the code editor). There are also many other resources you can use, like the Codea Wiki. To find other resources, just look under the getting started section in the Codea examples. It will be the first one. The screen that pops up is shown in figure 1. I wish you good luck in your journey into Lua greatness. As a final note on the subject, there are many great books to be found on Lua. My favorite is "Learn Lua for iOS Game Development" by Jayant Varma. It even has a section on Codea! Again, good luck. I look forward to seeing what you come up with in the future!

Advanced Features

Here they are. The advanced features that you might use when you get to be a really good programmer. To tell you the truth, they really aren't that advanced, but they are pretty confusing when you first open up Codea. So now, take the time to open up that dreaded sidebar. Figure 1 shows what you'll find. From top to bottom they are:
Refernece(The same reference for Lua that is in the code editor. Nothing new here.) Figure 2.

Shader lab(The really nitty-gritty stuff that I don't understand. Here you can edit the shaders that you use with meshes. Take one look at the code and you'll see what I mean. BTW: NEVER TRY AND MESS WITH THE NUMBERS TOO MUCH ON THE FIBONACCI THING. I OVERHEATED MY IPAD) Figure 3.

Assets(Here you can manage the sprites and sounds and patterns and shaders available for your programs to use. Pretty simple actually) Figure 4.

Airplay(The best thing ever! This allows you to connect your computer to your iPad and code on your computer, while the code runs at the same time on the iPad! All you have to do is navigate your computers browser to the IP address shown.)

And thats all there is to it!

Final Note

Thank you for reading my ramblings about the best code editor of all time, Codea. Codea is not only great for development and making money, but it is also great for learning. At my school, there is a robotics club where the only thing we do is code on Codea, and all of us who attend have learned a lot about math and programming in general. For that, I thank you, TwoLivesLeft, and I wish all of you good luck in your journey into Lua mastery. If you need help, PM me on the Codea forums. I am TheSolderKing there as well. This is my first instructable and I would greatly appreciate it if you would comment, vote, and favorite. Above is one of my first Codea projects, and below is the code(If it gets saved. Instructables has been weird about that. I would appreciate some help with that.) Thank you for reading! If you liked the project, please consider voting for me in the Formlabs contest. If I win, I will use the 3d printer to rapidly prototype robotic parts for my robotics club at school. I will make sure to tell all of you what we come up with! Thanks again.
-TheSolderKing

Code