How to interact with MMF objects from LUA?
Right so I am making an RTS game in MMf and things arn't going too bad. I have looked at the example file supplied with xlua showing how the active objects are being controlled via LUA but I just can't seem to get it to work.
Basically what I want to do is call the main LUA function every MMF frame which exports all objects into LUA. I then want to loop through each object and based on the object type and its alt values I want to be able to issue commands to it accordingly. Basically it will be a big script file dictating everything about the game.
RTS AI is very hard to program as it is and doing this within MMF events is extremely tedious and very slow. I am hoping that doing it in LUA will speed things up a lot more and will certainly be easier to edit/add things as I need.
Can someone please explain to me in further detail what I nee to do or give me another example MMF?
Re: How to interact with MMF objects from LUA?
you only need to export an object once, and it will exist in xlua until the object is destroyed or you remove it. Rather than exporting each frame, you should assign object ID's each time they are created, exporting them once.
Re: How to interact with MMF objects from LUA?
You may find it useful to export all objects by their fixed ID, so all your objects become self-tracking in MMF. As Pixelthief said, once you export an object, it is permanently bound to the Lua state until it is either destroyed, or you use the corresponding action of the XLua object to break that connection.
To make it easier to track your objects in Lua, you should define a function that you can call immediately after exporting an object, such as:
Code:
objects = {
enemies = {},
items = {},
projectiles = {},
}
function registerObject (objid, group)
table.insert(objects[group], objid)
end
Then in MMF you might have a few events:
+ Start of Frame
> Spread Value in AltA("enemytank")
> Start Loop "register_tanks" Count("enemytank") times
+ On Loop "register_tanks"
+ AltA("enemytank") = LoopIndex("register_tanks")
> XLUA: Export Object "enemytank" with ID fixedid("enemytank")
> XLUA: Call lua function registerObject(fixedid("enemytank"), "enemies")
At the end of that, all instances of your "enemytank" object are exported to XLua using their fixed ids as their export ids. Additionally, you have a table "objects" which contains several tables that group together export ids in a structure that might be useful to your program. Then in your program, you could for example loop through one of those tables to perform an operation on every enemytank object, like moving it or calling a function on it (passing its fixedid as a function parameter so it's easily identified in MMF). Every MMF interface function takes an export ID as its first parameter.
You should check the XLua documention for the functions mmf.ExportCallback and mmf.DestroyCallback, which help automate the above process (but not quite as flexible). There are several functions in the MMF interface just for returning information about an object, like its name or fixed id.
If I had to rewrite XLua today I'd actually do the object stuff a little different, but I didn't know anything about the MMF SDK OR the Lua C interface when I started, so I've learned some useful techniques only after the API has become well established. Ah well.
Re: How to interact with MMF objects from LUA?
Ah k cool. So I only register objects once? Thats great I thought it had to be done every loop. And I always reference everything with FixedValue.
So when I export an object I also need to do a register object aswell to set up that object in LUA? Or is that just a suggestion you made to make it easy to store and keep track of my objects?
Ok sweet. I understand what you mean now. I store all values in the table so I can reference them later and loop through them as I need to. Thanks for the advise.
The idea is that all objects are kept track of inside LUA so that each MMF loop I can call a LUA function to iterate through every object, read its current 'state' (which is stored in alt values/strings) and do events accordingly.
Also can LUA move and modify the objects direct or do I need to always do calls back into MMF with passed values to update the objects themselves?
Do you think you can make a simple example for me please Retriever2?
Re: How to interact with MMF objects from LUA?
Please read through the MMF Interface portion of the XLua help file. Even I keep the help file open when I'm programming Lua in MMF!
There are many things that you can modify directly such as position, visibility, animation set/frame, direction, angle, scale, alterable values and strings, etc. There are also functions for setting layer, setting/testing Z-order, and collision detection (but these particular functions are provided as-is .. I cannot 100% guarantee their safety).
There are other MMF interface functions available for things like getting key/mouse button state, frame position, etc.
Check the included example again for the MMF interface, you'll see the calls in use.
Re: How to interact with MMF objects from LUA?
OK will check it out when I get home. Cheers
Re: How to interact with MMF objects from LUA?
OK I was pulling my hair out on this for ages. I am not sure if its a bug or not but if I call a simple loop from lua such as
for i=1,10 do
DoCall("create")
end
In MMF I have an event OnFunction("create") it all works great and creates the object. Now if I try to do a lua call within this event it doesnt work, I get an error "return nil value" or something.
You can easily reproduce this by calling a lua function within the OnFunction event. To get around this I had to do a MMF fastloop and then call the function in that so I think its related to calling a lua function within another function??
Re: How to interact with MMF objects from LUA?
you should be able to call lua functions from within on function calls. In fact I have gone back and forth with these nested calls several levels deep.
Please send me an MFA demonstrating the problem and I'll take a look at it for you.
Re: How to interact with MMF objects from LUA?
Re: How to interact with MMF objects from LUA?
OK Well the problem mentioned above was due to my own error, as I was trying to call a function in lua before the parser had even reached it, so it basically didnt know it existed.
Anyway, now I need further information on the mmf.DestroyCallback feature. When and how do I call this? Do I simply put it at the end of the main loop and LUA will automatically delete any reference to the deleted object? Or do I need to loop through each object and pass its ID to see if its been flagged for deletion?
What if I needed to remove the object ID from a table in LUA, how do i pass the ID value? The help documentation doesn't make it very clear.