Hello Guest it is May 12, 2021, 08:39:18 PM

Author Topic: Using Modules  (Read 3836 times)

0 Members and 1 Guest are viewing this topic.

Using Modules
« on: May 29, 2016, 03:56:04 PM »
Can't seem to find any documentation covering the basics.  Lets say I have a module "Module1.lua" and it has a function in it called "theFunction()".  Heres what I have so far.  This is the module...

Code: [Select]
local Module1 = {}

function theFunction()
wx.wxMessageBox('Module Test')
end

return Module1

This is the code in a button event that I want to call "theFuntion()"

Code: [Select]
sw = require('Module1')
inst = mc.mcGetInstance()
sw.theFunction()

I suspect the reason it's not working is because the module is not in the right directory...or one or both files need a path specified...or the module needs to be loaded on screen load.  Maybe a combination of these.

Thanks
« Last Edit: May 29, 2016, 04:09:22 PM by rrc1962 »

Offline DazTheGas

*
  •  753 753
  • DazTheGas
    • View Profile
Re: Using Modules
« Reply #1 on: May 29, 2016, 04:24:43 PM »
You need to rename your function so it is tied to the Module1 table

Code: [Select]
local Module1 = {}

function Module1.theFunction()
wx.wxMessageBox('Module Test')
end

return Module1

As a norm the modules should be placed in the Mach4Hobby/Modules directory

DazTheGas
Theres always time for a choccy biccy
Re: Using Modules
« Reply #2 on: May 29, 2016, 06:08:50 PM »
When I run it in debug I get this...

Code: [Select]
[string "C:\Users\Richard\AppData\Local\Temp\le53EB.mc..."]:2: module 'Module1' not found:
no field package.preload['Module1']
no file 'C:\Mach4Hobby\lua\Module1.lua'
no file 'C:\Mach4Hobby\lua\Module1\init.lua'
no file 'C:\Mach4Hobby\Module1.lua'
no file 'C:\Mach4Hobby\Module1\init.lua'
no file '.\Module1.lua'
no file 'C:\Mach4Hobby\Module1.dll'
no file 'C:\Mach4Hobby\loadall.dll'
no file '.\Module1.dll'
stack traceback:
[C]: in function 'require'
[string "C:\Users\Richard\AppData\Local\Temp\le53EB.mc..."]:2: in main chunk

Do I need to be loading the module in the screen load script or providing a path to it somewhere?

If I put Module1.lua in C:\Mach4Hobby\, it opens a file dialog when I debug, then seems to run OK.  When I try to run it outside the editor, it doesn't do anything.

Offline dude1

*
  •  1,253 1,253
    • View Profile
Re: Using Modules
« Reply #3 on: May 29, 2016, 06:24:46 PM »
have a look back to the last page on the M4 post have a look at what popperbear did
Re: Using Modules
« Reply #4 on: May 29, 2016, 06:41:21 PM »
last page on the M4 post

Not sure what that means.

Offline DazTheGas

*
  •  753 753
  • DazTheGas
    • View Profile
Re: Using Modules
« Reply #5 on: May 29, 2016, 06:41:38 PM »
Out of curiosity insert this before you do sw = require('Module1') and have your module in the Mach4Hobby\Modules directory, the debug info means it cant find the file, this is already in the startup script but just incase whatever your doing cant see it??

Code: [Select]
   local profile = mc.mcProfileGetName(inst)
    local path = mc.mcCntlGetMachDir(inst)
    package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"

DazTheGas
Theres always time for a choccy biccy

Offline DazTheGas

*
  •  753 753
  • DazTheGas
    • View Profile
Re: Using Modules
« Reply #6 on: May 29, 2016, 06:43:11 PM »
Oh and the extension needs to be .lua
Theres always time for a choccy biccy

Offline dude1

*
  •  1,253 1,253
    • View Profile
« Last Edit: May 29, 2016, 06:57:18 PM by dude1 »
Re: Using Modules
« Reply #8 on: May 29, 2016, 07:47:51 PM »
So just to recap, here is the module...It is called Module1.lua

Code: [Select]
local Module1 = {}

function Module1.theFunction()
wx.wxMessageBox('Module Test')
end

return Module1

This is the button code calling the function in the module...I've tried this...

Code: [Select]
inst = mc.mcGetInstance()
M.theFunction()

And this...

Code: [Select]
inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)
package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"
local M = require "Module1"
M.theFunction()

Also tried adding this to the screen load script...

Code: [Select]
package.path = "./?.lua;c:/Mach4Hobby/Modules/?.mcc;"  
M = require "Module1"

As well as this....

Code: [Select]
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)
package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"
M = require "Module1"

Still nothing....I also tried putting a copy of Module1.lua in C:\Mach4Hobby, C:\Mach4Hobby\Modules and C:\Mach4Hobby\Profiles\Mach4Mill\Modules
« Last Edit: May 29, 2016, 07:50:25 PM by rrc1962 »
Re: Using Modules
« Reply #9 on: May 29, 2016, 08:15:53 PM »
Maybe a step in the right direction.  When I run the code below in the editor...

Code: [Select]
inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)
package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"
local M = require "Module1"
local rc = theFunction()

It opens a file dialog with Module1.lua already entered.  I click open and the module opens up allowing me to step through it.  When it gets to the bottom of the module, it returns to the calling script.  The next line in the script is the call to theFunction(), which generates this error.

Code: [Select]
mcLua ERROR: Lua: Error while running chunk
[string "C:\Users\Richard\AppData\Local\Temp\le4431.mc..."]:6: attempt to call global 'theFunction' (a nil value)