Live coding with Moai

Several people asked me during the last few days why Moai live coding is not supported by ZeroBrane Studio and I decided to take another look at why it has not been working as expected. After couple of hours of poking around I realized that the damage was self-inflicted: one of the options I added specifically for Moai (as requested by first beta users) was interfering with scratchpad logic. After resolving the conflict, live coding was back to live.

Just a bit of a background for those interested. Live coding works by continuously re-evaluating the code being edited in the running process. In order to do that, it needs to:

  1. abort or suspend the running process;
  2. re-evaluate the new code fragment in the context of the (suspended or aborted) process.

If your initial setup and the code you want to modify are in the same file, you may want to hide your setup code behind a check to avoid running it on subsequent evaluations (as I have done with if not initialized check). Here is the code I've used:

if not initialized then
  MOAISim.openWindow ( "test", 320, 480 )
  local viewport = ()
  viewport:setSize ( 320, 480 )
  viewport:setScale ( 320, 480 )
  local gfxQuad = ()
  gfxQuad:setTexture ( "moai.png" )
  gfxQuad:setRect ( -64, -64, 64, 64 )

  prop = ()
  prop:setDeck ( gfxQuad )
  prop:setLoc ( 0, 80 )
  local font = ()
  font:loadFromTTF ( "arialbd.ttf",
    " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?!",
    12, 163)

  textbox = textbox or ()
  textbox:setFont ( font )
  textbox:setRect ( -160, -80, 160, 80 )
  textbox:setYFlip ( true )
  textbox:setAlignment ( MOAITextBox.CENTER_JUSTIFY )
  textbox:spool ()

  local layer = ()
  layer:setViewport ( viewport )
  layer:insertProp ( prop )
  layer:insertProp ( textbox )
  MOAISim.pushRenderPass ( layer )

  local thread = ()
  thread:run(function ()
      while true do
  initialized = true

function update()
  textbox:setString("Moai live!")
  textbox:setLoc ( 0, -100 )
  prop:setLoc(0, 60)

You can copy this code as main.lua in one of your Moai projects (and use moai.png and arialbd.ttf from samples/hello-moai/ project) and go to Project | Run as Scratchpad in ZeroBrane Studio. This will run your project in Moai; if you are new to this process, you may want to check my earlier post on configuring and debugging Moai projects.

When your script is running you can make changes in the update() method: for example, you can update the string in the setString call and should see the new value in the Moai window. You can also update numbers (manually or using sliders) and see the objects repositioned on the screen.

To use this functionality you need to get the most recent code from Github (or wait for ZeroBrane Studio v0.34).

You should get a copy of my slick ZeroBrane Studio IDE.


This is awesome! Thanks.

I'm trying to do live coding on a moai sample with the latest version of the code (directly from github), I can run the sample, debug the sample, but when trying to scrathpad the moai exec hangs.

Any reason for this?

@Alex, you didn't mention what sample you are trying with (from the blog post or from Moai distribution), but if you are trying with one of Moai samples, it's likely not to work without changes. This is mostly because there are some elements that will persist between refreshed unless I restart the entire app (which is possible, but not desirable as you are losing the current state). For example, if you are running a corouting using, then this coroutine will continue to exist even if you try to 'refresh' the function that is running in it.

This is why I split the code in two sections: one that is protected by "if not initialized" check (and is executed only once) and one that is executed on each refresh. This makes no difference when you Run or Debug this code, but does make a differnce when you Run it as Scratchpad.

@Paul, I actually tried the code you're providing, with the initialization section protected from being run again.

It's just not starting; the moai process appears in the task bar, but is unresponsive. Everything else works fine, it's just the scratchpad that hangs.

@Alex, what OS are you running on? What version of Moai? I'll try to reproduce the issue...

osX 10.8.2, Moai v1.3 build 160

Thanks Paul for investigating into this

Alex, do you have "moai entrypoints" set in your "cfg/user.lua" file by any chance?

I have the same problem as Alex on OSX 20.7.4, entrypoints are defined.

Removing the entrypoints fixes it :)

Alex, Claudia, this issue should be fixed in the github repository. You should be able to use Moai entrypoints with scratchpad. Please let me know if you run into any other issues and thank you for reporting this!

Hi Paul, this is not working for me. I'm using

  • Windows 7 32bit
  • ZeroBraneStudio 0.35
  • Moai 1.4p0

I opened the live-coding Moai sample, run it as scratchpad, tried changing some of the coordinate values. Nothing changed. The debugging works though.


@aaulia, are you trying to change the values inside the "update" function (at the bottom of the script)?

Yes I am. I use couple of ways. First by using the slider (mouse cursor changes icon), secondly by editing it directly. Nothing changes. Some behavior that might help.

The console suddenly clear up after I edit the value. Also after I closed the application, the console shows scratchpad error, cannot connect debugger, or something along those line. I assume it because I closed the application directly (using the close button) so I ignore it.

Also I tested with and without user.lua, didn't help. I also tried removing the "entry point" for moai. Also didn't help.

I'm sorry If I can't be more of an assistance. Is there any way I can gather logs of what happen to help you track this?

ZBS 0.526 moai-sdk-1.4p0 Win7 64bit

Works like a charm. Thank you very much.

Thank you for this, I have the demo working and live-updating using version 0.39 on Linux! I am wondering, however, just how can I use the livecoding in a larger application? Can I modify any file and have it reload? Does the code have to be from within a function called "update"? What are the limitations? I am unsure what I can/can't do with this tool.

@zbstester, yes, you should be able to modify any file, although you may need to be careful with structuring your code. You can add more than one tab to the scratchpad by right clicking in the editor and selecting "Add to Scratchpad" (it will be disabled if you are not running live). Note that only the code in the modified file will be re-evaluated, so for Moai you may have your "update" function in a separate file from everything else.

Leave a comment

what will you say?