XLua: MMF Interface
|Add to this template|
Both Lua+ and XLua support an MMF interface, which are a special set of functions that can deal with objects directly; as calling functions within MMF is quite slow, interface functions bypass that step entirely. This tutorial will deal with XLua's interface as it is much more extensive than Lua+'s.
Do note that the interface is limited to extensions that support it, of which there aren't many. In fact, only two of them really do at this point: Active Objects and the OpenGL object. A few MMF properties and functions are supported as well. Everything else has to be done through slower callback functions; fortunately, you may not have to use them as often.
We need to set up XLua first; you can find out how to do this here. Afterward, we need to enable the MMF Interface, which can be done by checking the relevant box in XLua's properties. Or, you could do it through events:
Start of Frame --> XLua - Enable MMF Interface
If we want to use an object, we have to export it to XLua using its Export Object action; it is important to make sure you're only exporting one object at a time, as Object Selection problems apply here. The Export Object action wants two parameters: an object type to export and an ID number. As you can probably guess, the ID number needs to be unique for each object you export. Whether MMF or Lua keeps track of free IDs is up to your engine design; for this tutorial, we'll just let MMF handle it, because we're only going to export a few objects anyway.
Since you have XLua ready to run, we need an object to test and somewhere to store the script. Conveniently, XLua has a property to let you store a script within it, so we'll use that. First, just drop an Active Object somewhere (anywhere), then disable its "Create at start" option. Then add these events:
Start of Frame --> Start Loop: "newObjects"; 3 On Loop "newObjects" --> Create Object: <Active>; 0; 0 --> XLua - Export Object: <Active>; loopindex("newObjects") Always --> XLua - Call Lua Function: "main"
Then go into XLua's properties and make sure these are checked:
- Bind State (0)
- Use MMF Interface
- Run Script at Start
- Load Standard Libs
Then click the edit area of the Embedded Script line and click the '...' button. Enter the following script:
-- first object mmf.Object.SetX(0, 320) mmf.Object.SetY(0, 240) -- second object mmf.Object.SetX(1, 160) mmf.Object.SetY(1, 128) -- third object mmf.Object.SetX(2, 480) mmf.Object.SetY(2, 96) -- update objects function update() mmf.Object.Update(0) mmf.Object.Update(1) mmf.Object.Update(2) end -- main function function main() mmf.Object.SetX(0, mmf.Object.GetX(0) + 1) mmf.Object.SetAngle(1, mmf.Object.GetAngle(1) + 2) -- update() end
to be continued