Lua and JContainers in Skyrim

Under construction

Overview

JContainers comes with a bundled Lua interpreter.
If you don't what the hell that means, I'll tell you it means good news.

You can use Lua to program the meat of your mod while using Papyrus more like a middle man, but that power comes with some price and this page is just about that.

I'll use my mod Sandow Plus Plus for all these examples. You can download the source code here.

Unrelated note, it's a damn shame no one has ever noticed I named my mod after the wonderful Sandow Plus page (the world really lost so much irreplaceable info when that page went down and never quite recovered).

Why Lua?

Workflow

To use Lua in Skyrim this is what you should be generally doing:

Lua-Skyrim workflow
  1. Create a tree structure/array/whatever in Papyrus, using JContainers API.
  2. Pass that structure to Lua.
  3. Process it in Lua.
  4. Get back the structure in Papyrus.
  5. Apply new values to Skyrim objects using Papyrus.

As you can see, in this workflow the only thing Papyrus does it's getting and setting variables while all real calculations are made by Lua.

Example

In my case, my mod makes you gain weight (muscles) after some calculations are done, so this is what my mod looks like:

  1. Papyrus gets player bodyweight among other things and creates a JMap with all that data.
  2. Sends the newly created JMap to Lua.
  3. Lua uses all that data to calculate how much bodyweight the player will gain or lose.
  4. Lua returns all newly processed data using return.
  5. Papyrus applies new weight to the player.

Summary: Papyrus is only doing what Lua can't; things like directly accessing Skyrim objects and events.