Basic Concepts - Woospringbreak1/Luamod_Docs GitHub Wiki

LuaBehaviour

The LuaBehaviour class is the base for the Lua Modding Framework. Every LuaBehaviour runs a single script file, referenced as a file path and/or a .lua.txt TextAsset. Each script runs in an isolated global namespace.

If a LuaBehaviour has both a file path and TextAsset reference, the file path will be loaded first and the TextAsset only loaded if this fails. This allows the use of drop-in files to override scripts, and greatly accelerates development by allowing reloading scripts without restarting Bonelab or recompiling pallet files.

image

Note: due to the way the Crate system caches objects, the map must be reloaded to see code changes even if you spawn a crate using the spawn gun etc.

Unity Magic Functions

LuaBehaviour scripts automatically implement all of the unity Magic Methods. Just declare a function with the correct name (including parameters and capitalization) and the Lua Modding Framework will call it when the parent unity method is called.

function Awake()
function Start()
function LateStart()
function OnEnable()
function OnDisable()
function OnDestroy()
function Update()
function LateUpdate()
function FixedUpdate()
function SlowUpdate()
function OnTriggerEnter(other)
function OnTriggerExit(other)
function OnTriggerStay(other)
function OnCollisionEnter(collision)
function OnCollisionExit(collision)
function OnCollisionStay(collision)
function OnJointBreak(breakForce)
function OnBecameVisible()
function OnBecameInvisible()
function OnParticleSystemStopped()
function OnParticleCollision(other)
function OnParticleTrigger()
function OnParticleUpdateJobScheduled()
function OnTransformChildrenChanged()
function OnTransformParentChanged()

C# Interoperability

With exceptions caused by bugs and interoperability issues, all accessible C# methods can be called and properties accessed as with C#. A series of helper API classes, named API_x implement methods that bypass interoperability issues and add helpful functions.

Script-Script communication

Script-Script communication and control can be performed by calling the following c# function on a LuaBehaviour Object

public bool CallFunction(string functionname, params DynValue[] args)
public DynValue GetScriptVariable(string name)
public void SetScriptVariable(string name, DynValue DyVar)

Note that script functions can not be called directly as they not exposed to C#

Scripts can also communicate using the event system defined in API_Events, which allows subscribing to and calling global events

Script Tags

the LuaBehaviour Script Tags is a List<string> that contains user-defined tags. These are can be set at compile time in the unity editor and be modified at runtime via Lua Code. These can be used to declare types, features and states.

Access the script tags via the LuaBehaviour.ScriptTags property.

image

require()

Lua scripts can be included using require(script) where script is either a string file path or a TextAsset

TextAssets to be used as modules should be referenced in a LuaResources instance

local luaResources = API_GameObject.BL_GetComponent(BL_Host,"LuaResources")
require(luaResources.GetObject("RequireExample.lua","TextAsset"))

LuaResources

The LuaResources class makes up for the inability to assign references and variables to LuaBehaviours in the unity editor. LuaResources functions as a dictionary that can be assigned to in the editor and stores the following types with string keys:

string
float
bool
UnityEngine.Object

image

LuaGun

LuaGun is a subclass of LuaBehaviour. It functions the same except it detects a SLZ class Gun and binds the following extra functions

function OnFire() - return true to fire a bullet as normal
function TriggerPulled() - fires every frame the trigger is pulled.

The property AttachedGun allows access to the attached Gun instance

⚠️ **GitHub.com Fallback** ⚠️