Code Error: Building and Using Lua Libraries

This section is for general discussion about Gadgets
Post Reply
User avatar
jimandi5000
Vectric Wizard
Posts: 506
Joined: Wed Mar 11, 2015 6:50 pm
Model of CNC Machine: Home Made 60 x 120
Location: North Houston Tx.

Code Error: Building and Using Lua Libraries

Post by jimandi5000 »

Hi All,

I need some help...

I am trying to build/use code library. I am trying to reduce the number of lines I have to scroll through to fix and issue by being a stand alone code. As of now, I am getting an error.

Attached, is the code I have been testing and a screen shot of the error.

Your help would be GREAT!!!!

Test.lua

Code: Select all

-- VECTRIC LUA SCRIPT
-- require("mobdebug").start()
--  =*********************************************= 
function main(script_path)
  local tl = require(script_path)
    local job = VectricJob()
    if not job.Exists then
         DisplayMessageBox("Error: No job loaded")
         return false ; 
    end
    DisplayMessageBox(tl.Jim)
  return true 
end
--  =============== End ===============================
Lib.lua

Code: Select all

-- VECTRIC LUA SCRIPT

function tool
-- =================================
tool.james = "{1,4,6,2,3,5}"
-- =================================
tool.bob = function(a,b); local c = 0; c=a+b; return c;  end
-- =================================
tool.jim = "Jim Anderson"
-- =================================
return tool
end
Attachments
Error.jpg
Libaray.zip
(594 Bytes) Downloaded 41 times
Thanks,
Jim

User avatar
Adrian
Vectric Archimage
Posts: 10733
Joined: Thu Nov 23, 2006 2:19 pm
Model of CNC Machine: ShopBot PRS Alpha 96x48
Location: Surrey, UK

Re: Code Error: Building and Using Lua Libraries

Post by Adrian »

Busy working on my car today so haven't got time to look at the code in detail but two things strike me. First is where have the put the lib.lua file? If it's not on the package path then you need to define the path to where it's stored as part of the require call.

Secondly your lib file doesn't look to be structured as a module. You need to declare it as a module and then prefix the functions with the module so that the calling routine knows what it's calling. With the way it's structured I don't think Lua would see it as a valid module even if it could find it (if it's on the path).

User avatar
jimandi5000
Vectric Wizard
Posts: 506
Joined: Wed Mar 11, 2015 6:50 pm
Model of CNC Machine: Home Made 60 x 120
Location: North Houston Tx.

Re: Code Error: Building and Using Lua Libraries

Post by jimandi5000 »

Hi Adrian,

Thanks for replying to my email.

I've made some modifications to the code (see below) and I'm still getting an error. Both files reside in the same directory location and I've tried both providing the full path with filename (test 1) and just the file name (test 2), resulting in the same error.

Lib file

Code: Select all

-- VECTRIC LUA SCRIPT
local M = {}
  -- =================================
  function M.james()
    print("{3,4,6,2,3,5}")
  end
  -- =================================
  function M.bob(a,b)
    local c = 0
    c=a+b; 
    return c;  
    end
  -- =================================
  function M.jim()
    print("Jim Anderson")
  end
-- =================================
return M
Test File

Code: Select all

-- VECTRIC LUA SCRIPT
-- require("mobdebug").start()
-- local tl = require(script_path .. "\\" .. "Lib.lua") -- test 1
local tl = require("Lib.lua")                                  -- test 2
--  =*********************************************= 
function main(script_path)
    local job = VectricJob()
    if not job.Exists then
         DisplayMessageBox("Error: No job loaded")
         return false ; 
    end
    DisplayMessageBox(tl.Jim)
    
  return true 
end
--  =============== End ====================
As always, I truly appreciate the help.
Attachments
Error1.jpg
Error1.jpg (5.24 KiB) Viewed 249 times
Thanks,
Jim

User avatar
jimandi5000
Vectric Wizard
Posts: 506
Joined: Wed Mar 11, 2015 6:50 pm
Model of CNC Machine: Home Made 60 x 120
Location: North Houston Tx.

Re: Code Error: Building and Using Lua Libraries

Post by jimandi5000 »

Hello everyone,

I hope you're all having a great evening/morning/day wherever you're at the world. :)

I continue to have issues with making the lua module work correctly. I have installed Zerobrains and debugging the module works with the code below without error. (I do have to change the DisplayMessageBox to print for it to work correctly in Zerobrains)

Unfortunately, it does not work correctly when try I loaded into VCarve Pro version 10.018. I keep getting an error saying I'm trying to call a nul function.

There's got to be something I'm just plain out missing.

Any thoughts?

Test.lua

Code: Select all

-- VECTRIC LUA SCRIPT
require("mobdebug").start()
local xx = require("Lib") 
--  =*********************************************= 
function main()
    DisplayMessageBox("Jim")
    DisplayMessageBox(xx.jim)
    return true 
end
Lib.lua

Code: Select all

-- VECTRIC LUA SCRIPT
local M = {}
  -- =================================
  function M.bob(a,b)
    return a+b
    end
  -- =================================
  function M.jim()
    return "Jim Anderson" 
  end
-- =================================
return M
Thanks,
Jim

Post Reply