This post has been de-listed (Author was flagged for spam)
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
1
nlua "LuaScriptException: invalid arguments to method"
Post Body
im trying to make a lua scriping system for my game and when i try to create a gameobject i get the error "invalid arguments to method: GameObject.Find"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using NLua;
using System.Reflection;
using System;
namespace PlanetCluck.LuaScripting
{
[System.Serializable]
public class LuaScript
{
Lua LuaVM;
public string Path;
public LuaScript(string Path)
{
this.Path = Path;
string Code = File.ReadAllText(Path);
LuaVM = new Lua();
LuaVM.LoadCLRPackage();
LuaVM.DoString
(@"
import('Assembly-CSharp','PlanetCluck')
import('UnityEngine')
import('System.Collections')
");
try
{
LuaVM.DoString(Code);
}
catch(NLua.Exceptions.LuaException e)
{
Debug.LogError(e);
}
}
public System.Object[] Call(string function, params System.Object[] args)
{
System.Object[] result = new System.Object[0];
if (LuaVM == null) return result;
LuaFunction lf = LuaVM.GetFunction(function);
if (lf == null) return result;
try
{
// Note: calling a function that does not
// exist does not throw an exception.
result = lf.Call();
}
catch (NLua.Exceptions.LuaException e)
{
Debug.LogError(FormatException(e) $":{Path}");
throw e;
}
return result;
}
public void Start()
{
Call("Start");
}
public void Update()
{
Call("Update");
}
public void LateUpdate()
{
Call("LateUpdate");
}
public void FixedUpdate()
{
Call("FixedUpdate");
}
public void OnGUI()
{
Call("OnGUI");
}
public System.Object[] Call(string function)
{
return Call(function, null);
}
public static string FormatException(NLua.Exceptions.LuaException e)
{
string source = (string.IsNullOrEmpty(e.Source)) ? "<no source>" : e.Source.Substring(0, e.Source.Length - 2);
return string.Format("{0}\nLua (at {2})", e.Message, string.Empty, source);
}
}
public class RuntimeLua : MonoBehaviour
{
public static RuntimeLua Instance;
public List<LuaScript> Scripts = new List<LuaScript>();
[RuntimeInitializeOnLoadMethod]
public static void Init()
{
GameObject Go = new GameObject("Runtime Lua");
Instance = Go.AddComponent<RuntimeLua>();
foreach (string File in Directory.GetFiles(Application.streamingAssetsPath "/LuaScripts"))
{
if (File.Contains(".lua"))
{
AddScript(new LuaScript(File));
}
}
}
public static void AddScript(LuaScript Script)
{
Script.Start();
Instance.Scripts.Add(Script);
}
void Start()
{
foreach(LuaScript Script in Scripts)
{
Script.Start();
}
}
void Update()
{
foreach(LuaScript Script in Scripts)
{
Script.Update();
}
}
void LateUpdate()
{
foreach (LuaScript Script in Scripts)
{
Script.LateUpdate();
}
}
void FixedUpdate()
{
foreach (LuaScript Script in Scripts)
{
Script.FixedUpdate();
}
}
void OnGUI()
{
foreach (LuaScript Script in Scripts)
{
Script.OnGUI();
}
}
}
}
Author
Account Strength
0%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
34,548
Link Karma
9,973
Comment Karma
23,263
Profile updated: 7 months ago
Posts updated: 8 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/Unity3D/com...