/// <summary> /// Add all declared bindings to the globals table. /// </summary> public override void AddBindings(LuaEnvironment luaEnv) { if (!allEnvironments && (luaEnvironment != null && !luaEnvironment.Equals(luaEnv))) { // Don't add bindings to this environment return; } MoonSharp.Interpreter.Script interpreter = luaEnv.Interpreter; Table globals = interpreter.Globals; Table bindingsTable = null; if (tableName == "") { // Add directly to globals table bindingsTable = globals; } else { DynValue res = globals.Get(tableName); if (res.Type == DataType.Table) { // Add to an existing table bindingsTable = res.Table; } else { // Create a new table bindingsTable = new Table(globals.OwnerScript); globals[tableName] = bindingsTable; } } if (bindingsTable == null) { Debug.LogError("Bindings table must not be null"); } // Register types of bound objects with MoonSharp if (registerTypes) { foreach (string typeName in boundTypes) { LuaEnvironment.RegisterType(typeName); } } for (int i = 0; i < boundObjects.Count; ++i) { // Ignore empty keys string key = boundObjects[i].key; if (key == null || key == "") { continue; } // Check for keys used multiple times if (bindingsTable.Get(key).Type != DataType.Nil) { Debug.LogWarning("An item already exists with the same key as binding '" + key + "'. This binding will be ignored."); continue; } // Ignore bindings with no object set GameObject go = boundObjects[i].obj as GameObject; if (go != null) { // Register as gameobject / components Component component = boundObjects[i].component; if (component == null) { // Bind the key to the gameobject bindingsTable[key] = go; } else { // Bind the key to the component bindingsTable[key] = component; } } else { // Register as other UnityEngine.Object type bindingsTable[key] = boundObjects[i].obj; } } }