Skip to main content

Intro to Scripting with Lua

To select a world's scripting language, open SDK control panel, select world tab, select scripting and select Lua option:

It's important to talk about the tread of execution within Massive Loop. It will help us understand how and where the Lua scripts will be executed. The Lua scripts will only get executed when they are attached to a Unity GameObject or called using Import function.

Lua scripts that are attached to a Game Object executed by User Callbacks. Most of these callbacks are happens by the Unity Engine. You can check out the Unity execution call back here.

  1. First pass: This is the first time that a Lua script gets executed. This phase happens for all Lua scripts in the world at the very first frame of the game. If there are any Syntax, Compilation, or Interface errors in your code, those will surface in this phase. Depending on the structure of the code, you might have others errors surface at this phase too. This is also where all the code which are outside of the user callback functions will be executed.
  2. Start: In this phase, the start function in your code will be called. This happens on beginning of the next frame after initial pass.
  3. FixedUpdate: Fixed update may called multiple time per frame. This sub loop often called physics loop, which makes Fixed Update useful for physics calculation.
  4. OnTriggerXXX: Trigger events.
  5. OnCollisionXXX: Collision events.
  6. Update: In this phase, the update function gets called on every frame.
  7. Coroutine.Yield: The suspended coroutines resume here.
  8. LateUpdate: Called latest on the frame.

We will discuss more about the start and update function in Script Structure.

Check out All User Callback to learn more about callback functions.

In Game thread of execution

Massive Loop like most of the games uses a single thread to execute the game logic. This ensures a simpler design and keeps the integrity of data and events. The Lua scripts will also be executed in this thread. It means that on every frame the update function of all the Lua scripts gets executed.

Creating a LUA script in Unity

To add a Lua script to a GameObject, first, you need to attach a LuaScript component to it.

To do so, select the desired GameObject from the scene Hierarchy, in the Inspector, click on .

Then Select MassiveLoop > LuaScript.

Lua Script

You can attach your Lua scripts to the LuaScript component. As you notice, there are few options on the LuaScript Component.

  • : if checked, this script only get executed on the master client. To learn more about master client and general network structure in Massive Loop click here.

  • You can drag and drop the Lua Script assets to this field. The scripts must have a .lua extension.

The easiest way to create a new script is to press Create New Lua Script. This will brings up a new window and allows us to name the new script. Then automatically adds that to the field.

Editing Lua Scripts

LUA scripts can be edited with many IDEs and any text editor. We recommend using the VS Code. Right click on the LUA script and select “always open with this program” to bind LUA scripts.

Massive Loop SDK can create an lua scripting workspace in your project folder, which enable autocomplete functionality using sumneko.lua extension.

To allow SDK setup the environment, please make sure you have the latest installation of the VSCode, then open SDK control panel, switch to Settings tab, select Lua Workspace and select vscode option from from the list.