0

I have some problems with C# luainterface library:

1. So I load a script and extract its functions like that:

LuaFunction function = lua.GetFunction("Update"); 

But what if I load two different scripts that contains functions with the same name. How to extract two different functions with the same name from script1 and script2?

2. If I load functions into memory, is it possible to dispose a specific one, not all functions?

3. When I use Lua.DoFile method I want to execute specific function from the file. Any ideas how to do that?

Edit

2. I found, that I can do something like this

string f = @"
        function hh()
          end";

        var result = lua.DoString(f)[0] as LuaFunction;

but for some reason I get null exception. Any ideas why?

John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

0

DoString will return what your script return.

lua.DoString ("return 10+10")[0]; // <-- will return Double. 20

If you want to get your Lua function as LuaFunction object you need to return your function, or even better just use the [] operator to get the global value of hh.

lua.DoString ("function hh() end");
var hh = lua["hh"] as LuaFunction;
hh.Call ();

Here is a example: https://github.com/codefoco/NLuaBox/blob/master/NLuaBox/AppDelegate.cs#L46 (but Using NLua instead LuaInterface)

And remember to release your LuaFunction calling Dispose when you don't need the function anymore.

Vinicius Jarina
  • 797
  • 5
  • 16
  • Thank You. That's exactly what I wanted. Just a few quick questions. Why do I need to use "[0] in "lua.DoString ("return 10+10")[0];"? So I managed to load those separate functions into memory. I loaded a lot of them into List (program takes about 54MB at that time), but when I dispose all LuaFunctions and clear the list, the program still takes about 34MB of RAM. So as I understand those functions are still in computer's memory even though I disposed them. Am I doing something wrong? – user2055675 Nov 16 '13 at 20:18
  • And is it possible to get all the global variables defined in .lua script? I know that I can use lua.Globals, but it only returns global variables defined inside .Net program, not the script. – user2055675 Nov 16 '13 at 20:47
  • One more question if that's not too much. Is it possible to get local variables that are defined in .lua script (outside function scope)? – user2055675 Nov 16 '13 at 22:24
  • You need to use [0] because a Lua function can return several values, like `return 10, "other", nil` and NLua/LuaInterface will return a array with all values. – Vinicius Jarina Nov 16 '13 at 23:44
  • You are doing nothing wrong, the GC probably didn't run (yet), you can try to force the gc to collect calling `collectgarbage('collect')` http://www.lua.org/manual/5.2/manual.html#pdf-collectgarbage – Vinicius Jarina Nov 16 '13 at 23:54
  • I've recommend you to use _G to inspect all global variables http://www.lua.org/manual/5.2/manual.html#2.2, and to get the local variables you need to use the debug library http://stackoverflow.com/questions/2834579/print-all-local-variables-accessible-to-the-current-scope-in-lua – Vinicius Jarina Nov 16 '13 at 23:55
  • Tried collectgarbage, but nothing changed. This is how I did: `List functions = new List(); for (int i = 0; i < 1000000; i++) { functions.Add(lua["hh"] as LuaFunction); } foreach (LuaFunction f in functions) { f.Dispose(); } lua.DoString("collectgarbage('collect')"); functions.Clear(); functions = null; GC.Collect();` It still shows same taken RAM amount and I have no ideas why. – user2055675 Nov 17 '13 at 00:45
  • The issue here you are asking a new global value without unref the old one. https://github.com/NLua/NLua/blob/7e264fca63b5153b990fe4b1d8e882fd5c86bed1/Core/NLua/ObjectTranslator.cs#L767 Creating several references to the same function, you could create a cache to avoid creation of unnecessary references: like this https://gist.github.com/viniciusjarina/7508169 – Vinicius Jarina Nov 17 '13 at 02:10
  • Which global value do I need to unref? – user2055675 Nov 17 '13 at 02:23
  • This might be a bug, but I am not sure if is safe to change the current behavior, I am not sure why when you call dispose and unref for each LuaFunction the counter doesn't goes down. But if you Dispose () the LuaFunction before ask another instance of "hh" you will se the memory keeps low. – Vinicius Jarina Nov 17 '13 at 02:35
  • Yeah, I think so too, because when I dispose and unref each LuaFunction (as You said) I can execute as much as I want and program takes only about 4 or 5 MB of RAM without even using GC. I still can't figure it out how to print local variables. I tried executing locals() function inside my file, but I got "bad argument #1 to 'getlocal' (level out of range)" exception, – user2055675 Nov 17 '13 at 02:43
  • Why would I want to get local variables... I want to make my .lua file like .net class where you can have local variables inside it. Before disposing my function, I need to save all the local variables, so I could load them another time. Maybe there's another, a better way to do that? – user2055675 Nov 17 '13 at 02:51
  • Looks like that I just found what I wanted. Thank You for your time. – user2055675 Nov 17 '13 at 03:22