-1

I'm writing a simple program in c# using .net framework 4.8, winforms, and nlua. I want to change the location of my main form using a lua script. Here is what I tried:

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
}
using NLua;

lua_context = new Lua();
lua_context.LoadCLRPackage();
lua_context.DoString("import 'System.Drawing'");
lua_context.DoString("import 'System.Windows.Forms'");

MainForm main_form = new MainForm();
lua_context["main_form"] = main_form;
lua_context.DoString("main_form.Location = Point(5, 5)");

I get this error when calling the last line:

Exception thrown: 'NLua.Exceptions.LuaScriptException' in NLua.dll An unhandled exception of type 'NLua.Exceptions.LuaScriptException' occurred in NLua.dll [string "chunk"]:1: attempt to call a nil value (global 'Point')

I have also tried:

lua_context.DoString("main_form.Location = System.Drawing.Point(5, 5)");

and I get this error:

Exception thrown: 'NLua.Exceptions.LuaScriptException' in NLua.dll An unhandled exception of type 'NLua.Exceptions.LuaScriptException' occurred in NLua.dll [string "chunk"]:1: attempt to index a nil value (global 'System')

sloopy
  • 3
  • 3
  • It should be `main_form.Location = new Point(5, 5)`. But I don't know how NLua works, hence if you should pass an already constructed Point object or if declaring a `new Point()` in the string itself is enough, or even if you need to fully qualify the assembly, as in `... new System.Drawing.Point(5, 5)`. – Jimi Jul 18 '22 at 04:52
  • Just change x and y explicitly `main_form.Location.X = 5` and `main_form.Location.Y = 5` – Nifim Jul 18 '22 at 13:59
  • @Jimi, There is no new keyword in lua. The way to create a new object (as far as I understand) is to do it like I'm doing already. – sloopy Jul 18 '22 at 21:21
  • @Nifim, I tried that as well. I don't get an error, but for some reason it doesn't work. Besides that this problem I have with Point also happens with a few other things, so I really need to find the root issue. – sloopy Jul 18 '22 at 21:21
  • Well, you have this: `MainForm main_form = new MainForm();`. Can you do the same thing with a Point? I.e., just assign the Point value, as you're *assigning* the Form instance (`lua_context["main_form"] = main_form;`)? – Jimi Jul 18 '22 at 21:50
  • No. That is not a solution. – sloopy Jul 19 '22 at 00:47

2 Answers2

0

There is no global Point in Lua. Maybe you can use System.Drawing.Point(5, 5).

Alternatively adding lua_context.DoString("import 'System.Drawing'"); should fix the problem.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Sorry, I forgot to add that I tried ```lua_context.DoString("main_form.Location = System.Drawing.Point(5, 5)");``` and I get the same error: "NLua.Exceptions.LuaScriptException: '[string "chunk"]:1: attempt to index a nil value (global 'System')'". I have already imported system.drawing in the lua context. – sloopy Jul 18 '22 at 21:17
0

Try to add

import("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

The form.lua used to work

Vinicius Jarina
  • 797
  • 5
  • 16
  • I see. So I need both `import("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")` and `import("System.Drawing")`. This works! Do you know why the more verbose import is necessary? – sloopy Jul 19 '22 at 16:40