Donnerstag, 12. April 2012

Edit lua code in an external editor

Edit lua code in an external editor

This is the english version of the tutorial. It is also available in german
Dies ist die englische Version des Tutorials. Es ist ebenso auf deutsch verfügbar.

If you are familiar with programming wherigo cartridge in lua, you know the problem of too much code lines. Your program become confusing. The syntax highlighting of urwigo builder lua is a nice feature, but it is insufficient to handle code with a lot of lines.
During my programming of Cachopoly I have the problem to handle more than 1000 lines and so my main activity was to scroll to the line needed for debugging and testing.

After mailing with the developers of urwigo, they updates their editor. Know it is possible to link an external lua file which can be use in the urwigo project.
I decided to use the IntellijIDEA editor witch I extend with the lua plugin sylvanaar.
In Picture 1 you can see a simple "HelloWorld" Example, written with IDEA
Picture 1: Write lua code in IntellijIDEA

Top left you can see the storage location of the lua file. I decided to create a sub folder called lua which optimise the structure. But this decision is optional.
At the right side you can see the lua code. Beside syntax highlighting you can use features like parameter and function name competition and real-time syntax error and warning marking.
Bottom left we have the 'structure' element where I can see all the functions defined in my lua code. With the 'Autoscroll to source' feature you can jump to line 14 in code by clicking on the function 'helloWithName' in the structure frame. A nice feature if your code contains several hundred code lines.

It is not mandatory to use IntellijIDEA. You can edit the lua files with your favorite editor, but their are not many alternatives. If you found one, feel free to send me an email or make a comment for this post.
Pciture 2: all lua files in sub folder 'lua' are embedded
In which way can I embed the external files in my urwigo project? It seems to be tricky, but it will be very simple if you have see it once. All you have to do are two simple steps:
First you have to activate the checkbox "Inline Lua 'require'" and set the path to the lua files in the main screen of your urwigo project. The path 'lua\?.lua' contains all files in sub folder 'lua' (relative to your project folder) which ends with '.lua'.
The usage of a sub folder is optional. I think you will optimise your structure by dividing the lua files from your project files and picture.

Pay attention to the wildcard, that is not the asterisk '*' but the question mark '?'.
Picture 3: Embed the lua file with 'require'
The second step is very simple. All you have to do is write a require in your 'lua user directives' or 'lua user functions' of your project. The name of the embedded model must be the filename of the lua file
require "hello" must be equals to hello.lua.
Picture 4: Use the functions like you be used to
You can use the functions defined in the external file just as they were defined local in 'lua user directives' or 'lua user functions').

1 Kommentar: