-
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.
-
Re: How to interact with MMF objects from LUA?
Anything with "Callback" in its name is not something you call directly. If you define those functions, MMF (or really, XLua) will call them at an appropriate time.
As the documentation said, this function will be called at the end of every event loop, after all other events have been called but before the frame is redrawn. At this point XLua will check for which exported objects have been destroyed, and will call "DestroyCallback" for each of these objects. This is to give you a chance to remove references to these objects from any of your internal data structures. XLua removes destroyed objects from its internal list regardless.
-
Re: How to interact with MMF objects from LUA?
Yes but how do I define it? Im sorry but its still not very clear.
EDIT: nevermind. For those of you who are insterested I had to type this into the start of my script:
#define mmf.DestroyCallback
I then had to have a function called mmf.DestroyCallback(ID) which searched through my objects array and removed the element which contained the ID of the deleted object. Seems to work. Hope this is the right way of doing it.
-
Re: How to interact with MMF objects from LUA?
function mmf.DestroyCallback (objid)
-- Do stuff
end
Sorry if that was not clear. I will probably add examples to the documentation at a later time.
-
Re: How to interact with MMF objects from LUA?
yeh I figured that part out alright, it was defining it that had me stumped. Seem to have it working now though. thanks
-
Re: How to interact with MMF objects from LUA?
#define mmf.DestroyCallback should not even be valid. That is a C preprocessor statement, not a Lua statement.
# is actually the length operator so #define would try to return the length of the 'define' table (which doesn't exist).
-
Re: How to interact with MMF objects from LUA?
Quote:
Originally Posted by Retriever2
#define mmf.DestroyCallback should not even be valid. That is a C preprocessor statement, not a Lua statement.
# is actually the length operator so #define would try to return the length of the 'define' table (which doesn't exist).
well if you want to get technical it'd throw an error since there's a couple of syntax errors in that statement
-
Re: How to interact with MMF objects from LUA?
Quote:
Originally Posted by xyzzy
Quote:
Originally Posted by Retriever2
#define mmf.DestroyCallback should not even be valid. That is a C preprocessor statement, not a Lua statement.
# is actually the length operator so #define would try to return the length of the 'define' table (which doesn't exist).
well if you want to get
technical it'd throw an error since there's a couple of syntax errors in that statement
I figured that was a given.
-
Re: How to interact with MMF objects from LUA?
Quote:
Originally Posted by Retriever2
Quote:
Originally Posted by xyzzy
Quote:
Originally Posted by Retriever2
#define mmf.DestroyCallback should not even be valid. That is a C preprocessor statement, not a Lua statement.
# is actually the length operator so #define would try to return the length of the 'define' table (which doesn't exist).
well if you want to get
technical it'd throw an error since there's a couple of syntax errors in that statement
I figured that was a given.
given the wackiness i've seen on lua-users i bet somebody has found a way to make that legal syntax
i mean something like: out "hi" "what" "ok" "cool" which is normally illegal can be done
-
Re: How to interact with MMF objects from LUA?
It doesnt throw me an error though? Whats the proper way of defining a function then? It seemed to work OK, I had a test program create and delte 500 objects each game loop and my memory didnt increase at all and it didnt throw any errors. Altough the program acted kinda funny after about 30mins
-
Re: How to interact with MMF objects from LUA?
There's two equivilent ways to define a function (or any other piece of data in Lua).
Code:
function bar ()
-- do stuff
end
and
Code:
bar = function ()
-- do stuff
end
There's no funny declaration statements or other syntax like needed in C. You can define or redefine functions at any time during execution, just like you can create or recreate tables.
-
Re: How to interact with MMF objects from LUA?
OK, so in order to define the function all I need to do is enter a function with that name "function mmf.DestroyCallback(ID)" somewhere in my script? Then that will be executed automatically everytime MMF destroys the object without the need for me to do anything else??
-
Re: How to interact with MMF objects from LUA?
Yes, that is all.
Though to be clear, not immediately when an object is destroyed, but at the end of the current even loop. The main purpose of this function is to help you clean up when object destruction is beyond your control (like if the destroy if too far from frame box is checked).
-
Re: How to interact with MMF objects from LUA?
Thanks, now its perfectly clear. I have the function set up which is why it worked before when I had #define in there. Its strange why that didnt throw an error message though. Everything seems to be working great now, cheers.