1047
« on: May 15, 2014, 01:15:28 PM »
I think I have mentioned it before, but it is worth mentioning it again. There are two instances of LUA. One in the GUI and one in the mcLua plugin. The mcLUA plugin is there so that M codes will run regardless of what GUI is driving the core. It is NOT connected to the Mach4GUI in any way. However, communication between the two LUA instances is possible with Mach registers. But mcLua plugin based scripts and functions cannot see the Screen scripts as the screen scripts are imbedded into the screen set.
Mach 4 is written so that any developer can write their own GUI for the core. It can be written in C++, C#, VB, or any other language that supports loading of DLLs. We have two GUIs at the moment as an example of this. Mach4GUI and the wxMach "static" GUI. The static GUI is just that, static and not changeable. I do have a sample GUI written in C# that I played with. wxMach does not have a configurable screen and has no screen based scripting functions. It does, however, have a PLC script. So that is the reason for the separate LUA instances.
So on to global functions...
If you want a global function that can be called from any screen object script, put the functions in the screen load script. Then they will be available to all screen object scripts.
If you want a global function to be available to all M code "macro" scripts, put a file in the macro directory containing the global script functions. The file can be named anything as long as the extension is ".mcs". Say you named the file "myGlobalScripts.mcs". You can write this file using the mcLuaEditor. It is prudent to test the scripts (debug) and "compile" the file inside the mcLuaEditor to check for syntax errors. The act of compiling the *.msc file will generate a *.mcc binary file. Then when you press cycle start the first time, ALL of the *.mcc files in the macros directory will be combined into one LUA program called wxLua.mcc. This is the chunk that gets loaded.
What if you want the same global functions in the screen and macro scripts? You can duplicate the code in the screen load and the macros directory or you can "include" the code from the file system in each LUA instance. This "shared" code should be put in a global directory. Meaning not a profile's macro directory. Something more along the lines of a "Modules" directory in the Mach4 installation. That way all profiles or screens in you installation can access it regardless of if you happen to delete a profile or two. But I won't get into how to do all of this. The LUA manuals cover it.
Oh another thing... DO NOT expect any scripts that Brian or I post to just work. They are hand typed generalized examples of how to get something done. They are usually typed on-the-fly as we answer a post and they may not be perfect. They are close. But the only way to ensure they are perfect is to put them into the LUA editor, compile them, and tests them. That is not something we have time to do when answering a posts. It is an exercise for the user to make them work.
Steve