4

I am trying to create a game with scripting in C#. I asked a similar question earlier but the example pointed to invovled hardcoding in the .cs file. Is there anyway to compile and run an Assembly containing C# code that contains functions, assignments, operators and types that can change variables in the hosting .cs file? For example:

my .cs file contains a string variable myName.
my script code contains a function myfunction.
Is there any way to access myName from my function and change its value in my .cs file just by calling the Invoke method on myfunction? Without hardcoding myfunction in my .cs file?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Anoop Alex
  • 179
  • 1
  • 9

1 Answers1

11

NLua can do pretty much what you want and it's extremely easy to implement it into your project.

Lua lua = new Lua();
lua.DoString("return 'Hello World!'");

You can also register DotNet functions for use in your script files.

lua.RegisterFunction("print", this, typeof(Program).Print("Print"));

After that you can use it in your .lua file file like that:

function Run()
    print("Hello World!")
end

And one more example specifically for what you're planning to do. So lets say you have Actor class:

class Actor
{
    public string name;
    public int age;
}

This is how we introduce our DotNet stuff to scripting:

Lua lua = new Lua();
lua["actor"] = new Actor();

And now in the script:

function Run()
    actor.name = "Thomas"   
    actor.age = 20
end

I used global variable in this example, so instance of Actor class will be accessible in all scripts. This is how we can pass it as a function argument:

Lua lua = new Lua();
lua.LoadFile("Script.lua");
LuaFunction function = lua["Run"] as LuaFunction;
function.Call(new Actor());
martynaspikunas
  • 485
  • 5
  • 15
  • You need to increase your reputation for that. Thanks for your good intentions. You can mark my post as an answer to your question. That'll do. :) – martynaspikunas Nov 17 '13 at 08:12
  • I dont know to do that short of saying it here. So, while I cant install nlua i can still install luaInterface and have to look more into but this I believe answered my question folks! – Anoop Alex Nov 17 '13 at 08:56
  • Nlua is basically a newer version of luaInterface, so my examples will work you. What problems do you have with Nlua? What's it saying? Maybe it's related with .dll referencing? I had one problem with that so maybe I can help you. – martynaspikunas Nov 17 '13 at 09:00
  • I get Unable to load lua52 DllNotFoundException when trying to create Lua object. – Anoop Alex Nov 17 '13 at 17:44
  • You need to put lua52.dll in the same folder where your project's .exe is. You don't need to reference it in your code. Maybe that's the problem? I got same exception when I forgot to do that. – martynaspikunas Nov 17 '13 at 17:56
  • 1
    You may want to update your last example to say "Run" to follow your theme of "A function named Run as the entire script" in the previous examples. – Guvante Feb 25 '14 at 17:59