Each script in MicroMacro 2 has three functions to control the flow of execution:
macro.init()
This is the function called at the start of the script. It accepts any command-line arguments passed to it and should not return any results. This is where you want to do your setup.
macro.main()
This is your main loop. This will be called ~1,000 times per second. It accepts one parameter
(delta time) and should return
macro.event()
All events will be passed off to this function. The first parameter is the event type as a
Splitting your project into multiple files
If you were to put everything into one file, you would quickly run into a gigantic mess of unreadable code that is hard to work with. For this reason, programmers have split up their work into many smaller and more concise files that work together. Lua can already allow you to do this via the dofile() or require() functions, but MicroMacro improves upon this with include().
include() differs from dofile() in two ways. First, it will attempt to pull in the script relative to the script calling include() (while dofile() is relative to the CWD that you originally started the script from, and require() searched package directories). This makes things a bit easier to work with and more portable; a small quality of life improvement. Secondly, it (by default) it prevents re-loading a script multiple time (like require()) unless you request it to re-run the script.
It is suggested that you start your project with a new folder in MicroMacro's script directory (micromacro/scripts/ by default), then placing a new file named main.lua here. In this file you should place your macro.init(), macro.main(), and macro.event() functions, but try to keep them clean and compact. Place any classes, functions, or other code into files/folders relative to your project directory, then include() them at the top of your main.lua file.
For example, your project might need to handle many different events. Instead of a nasty mess of if/else statements clogging up your macro.event() function, we can clean up the code by creating an event handler class that then delegates different event types into functions.
MicroMacro comes with several libraries to serve common use cases. These libraries are available for you to use in any of your project much like the standard MicroMacro modules, however they are entirely optional and will not be pulled-in to your project unless you specifically ask for it. You may also write your own libraries to share common code across multiple projects, or to share with others to use.
Whether you use the included MicroMacro libraries, use your own, or want to use a Lua modules, the process is the same. While it is recommended that you pull in libraries from your main.lua file, you may do so in any file in your project so long as it is called before any code that relies on the library. Use require to pull in a library/module.
Some libraries/modules may not be written purely in Lua and may have dependencies (such as multiple .dll files). In this case, the main file (the file you will require) should be placed into the micromacro/lib directory, and any dependencies should be placed into the root MicroMacro directory (micromacro/). If you get an error about a missing file/dll when attempting to load a library, you've probably got your files placed in the wrong directory.
Page last updated at 2018-09-25 20:47:10