Browse Source

Merge pull request #432 from FungusGames/lua-environment-initializer

Lua environment initializer
master
Chris Gregan 9 years ago
parent
commit
0f7aa07e64
  1. 21
      Assets/Fungus/Lua/Scripts/LuaEnvironment.cs
  2. 7
      Assets/Fungus/Lua/Scripts/LuaUtils.cs

21
Assets/Fungus/Lua/Scripts/LuaEnvironment.cs

@ -12,9 +12,19 @@ using MoonSharp.RemoteDebugger;
namespace Fungus
{
public class LuaEnvironment : MonoBehaviour
{
/// <summary>
/// Helper class used to extend the initialization behavior of LuaEnvironment.
/// </summary>
public abstract class Initializer : MonoBehaviour
{
/// <summary>
/// Called when the LuaEnvironment is initializing.
/// </summary>
public abstract void Initialize();
}
/// <summary>
/// Custom file loader for MoonSharp that loads in all Lua scripts in the project.
/// Scripts must be placed in a Resources/Lua directory.
@ -127,13 +137,14 @@ namespace Fungus
// You can restrict which core lua modules are available here if needed. See the MoonSharp documentation for details.
interpreter = new Script(CoreModules.Preset_Complete);
// Load all Lua scripts in the project
InitLuaScriptFiles();
// TODO: Use an interface here instead
LuaUtils luaUtils = GetComponent<LuaUtils>();
if (luaUtils != null)
// Initialize any attached initializer components (e.g. LuaUtils)
Initializer[] initializers = GetComponentsInChildren<Initializer>();
foreach (Initializer initializer in initializers)
{
luaUtils.Initialise();
initializer.Initialize();
}
if (remoteDebugger)

7
Assets/Fungus/Lua/Scripts/LuaUtils.cs

@ -13,7 +13,7 @@ using MoonSharp.RemoteDebugger;
namespace Fungus
{
public class LuaUtils : MonoBehaviour
public class LuaUtils : LuaEnvironment.Initializer
{
/// <summary>
/// Lua script file which defines the global string table used for localisation.
@ -57,7 +57,10 @@ namespace Fungus
/// </summary>
protected LuaEnvironment luaEnvironment;
public virtual void Initialise()
/// <summary>
/// Called by LuaEnvironment when initializing.
/// </summary>
public override void Initialize()
{
luaEnvironment = GetComponent<LuaEnvironment>();
if (luaEnvironment == null)

Loading…
Cancel
Save