Hello Guest it is March 19, 2024, 03:05:52 AM

Author Topic: Can't seem to get load_modules.mcs working  (Read 1665 times)

0 Members and 1 Guest are viewing this topic.

Can't seem to get load_modules.mcs working
« on: June 17, 2018, 11:42:37 AM »
RHTuttle posted in January about launching functions as coroutines from his scripts using load_modules.mcs to load his custom module, a user module, and a button script
 http://www.machsupport.com/forum/index.php/topic,36474.0.html

I had no success trying to duplicate his efforts. I reduced my code to a minor test case, and it seems that I'm doing something wrong regarding load_modules.mcs.

The file 'load_modules.mcs' in my profile's '\Macros' subdirectory
Code: [Select]
---------------------------------------------------------------
-- Load modules that you want to be able to use from Mcodes
---------------------------------------------------------------
inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)

package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"
--package.path = path .. "\\Modules\\?.lua;" .. path .. "\\Profiles\\" ..  profile .. "\\Modules\\?.lua;"

--ErrorCheck module
package.loaded.mcErrorCheck = nil
ec = require "mcErrorCheck"

ph = require "phModule"     -- resides: C:\Mach4Hobby\Profiles\MyCNC\Modules

'phModule.lua' in my profile's '\Modules' subdirectory.
Code: [Select]
local phModule = {}

   function phModule.bigMac()
      mc.mcCntlSetLastError(inst, "Special Sauce?")
   end

return phModule


Button code
Code: [Select]
ph.bigMac()

When I activate the screen button, I get the following error message:
C:\Mach4Hobby\ScreenScript.lua:1029: attempt to index global 'ph' (a nil value)
stack traceback:
   C:\Mach4Hobby\ScreenScript.lua:1029: in function <C:\Mach4Hobby\ScreenScript.lua:1028>
   
However, if I add the load_modules code to the Screen Load Script, my button functions as expected.
Code: [Select]
--My Stuff (Paul)
ph = require "phModule"

Yes, I got the code working, but I'm perplexed as to what I was doing wrong initially. Any and all help much appreciated.
« Last Edit: June 17, 2018, 11:45:48 AM by lesspaul »
Re: Can't seem to get load_modules.mcs working
« Reply #1 on: June 17, 2018, 03:45:42 PM »
I figured it out. Dumb mistake.

The correct way is to put it in _both_ the screen load and the load_modules. Not just one or the other.
Re: Can't seem to get load_modules.mcs working
« Reply #2 on: June 17, 2018, 06:33:09 PM »
You are calling the module from the screen, so the require statement needs to be within the screen script, as you have discovered. Placing it in the Macros directory in LoadModules makes the module available to all macros, as these share a single file when compiled.

Allan
Re: Can't seem to get load_modules.mcs working
« Reply #3 on: June 17, 2018, 08:31:19 PM »
Allan,

Thank you for the clarification. The initial curve of learning the Mach4 basics, Lua, and the ins/outs of scripting for Mach4 can be daunting at first.

Paul

Offline smurph

*
  • *
  •  1,544 1,544
  • "That there... that's an RV."
    • View Profile
Re: Can't seem to get load_modules.mcs working
« Reply #4 on: June 21, 2018, 06:42:01 PM »
This is the way to share code between LUA states.  But it will not let you share global variables between the states!  Just throwing that out to clarify.  If you want to share data between the states, use Mach registers. 

Steve