Search code examples
c#lualuainterface

Instantiating C# Classes in Lua


I am writing a scripting engine for my game using the LuaInterface library. I am getting an error when attempting to instantiate the class in Lua. The error is:

"./Scripts/sv_worldgen.lua:2: attempt to call global 'Campfire' (a string value)"

Where sv_worldgen.lua is (in entirety):

function GenerateChunk(worldChunk, chunkGridPosition)
    tf = Campfire()
    tf:SetPosition(chunkGridPosition)
end

Campfire is a class in C#, and appears to be exposed to lua as per the CLRPackage example and of course the LuaInterface Reference. I cannot seem to get around this error, and I have done due diligence of searching. The only other behavior of the script I can manage throws a similar error, but where it is "(a table value)". What am I doing wrong? Thank you in advance!

I tried explicitly doing Campfire._ctor(), but _ctor() is a string value.


Solution

  • This was resolved by using CLRPackage and using it to first load the assembly.

    //Lua
    JASG = CLRPackage("JASG", "JASG")
    

    Then and only then can you link the classname to the actual C# class using (this must be done before trying to access it in Lua):

    //Lua
    Campfire=JASG.Campfire;
    

    and then normal instantiation can occur by

    //Lua
    cf = Campfire()