v3.5.0 Documentation
Fungus Lua Module

The Fungus Lua module provides handy functions for working with Lua, Unity and Fungus. It is located in FungusLua/Resources/Lua/Fungus.txt and is loaded automatically when you use the Lua Script component.

In this page we cover some of the more generic functionality in the module, other major features are described elsewhere in the documentation.

Inspecting Lua objects

You can use Lua's built in print() function to get a basic description of any object printed to the console. When you want to get a more detailed description of an object, use inspect().

1 -- Prints a short description of object v
2 print(v)
3 
4 -- Prints a summary of object v in a human readable format.
5 inspect(v)

Running Unity coroutines

When you bind to a C# component using Lua Bindings, you can access any public method in the class. If a method returns IEnumerator then that method can be executed as a Unity coroutine, which is a powerful way to run asynchronous code.

The runwait() function allows you to call a C# coroutine method from Lua which may take multiple frames to finish its work, and then carry on with the rest of the Lua code once that C# method has finished executing. This is how the say() function works for example.

This is the list of available functions for waiting and working with coroutines.

1 -- Waits for a number of seconds, then continue execution of Lua script
2 wait(duration)
3 
4 -- Waits until the Lua function provided returns true, or the timeout expires.
5 -- Returns true if the function succeeded, or false if the timeout expired
6 waitfor(fn, timeoutduration)
7 
8 -- Run a C# coroutine and continue execution of Lua script
9 run(co)
10 
11 -- Run a C# coroutine, wait until it completes, then continue execution of Lua script
12 runwait(co)

Globals vs Table mode

The Fungus module can be used in three modes, controlled by the Fungus Module option in the LuaUtils component.

  1. Use Global Variables: all module functions are mapped to global functions. This allows for convenient access, but it runs the risk that you might accidentally declare a variable with the same name as a Fungus module function.
  2. Use Fungus Variable: all module functions are accessed through a global table called 'fungus'. This gives a degree of namespace safety at the cost of more typing.
  3. No Fungus Module: the Fungus module will not be registered. Used if you don't want to use the Fungus module.

Options 1 and 2 are functionaly equivalent, it's just a matter of personal preference which you want to use.

1 -- sub is a function in the %Fungus module, mapped to a global variable
2 
3 -- Use Global Variables
4 sub('a string')
5 
6 -- Use Fungus Variable
7 fungus.sub('a string')