I said I got very confused about how Lua worked with C++. I started thinking things like "well, if a chunk returns a function, then I should be defining functions. So I'd write a lua script like this:
function greeter()
log("greeter script begin")
message("Welcome to Clonemaster")
end... and then wonder why it didn't work. Of course, it doesn't work for the same reason as it wouldn't work in a Lua script: the function is never called. When I run the Lua chunk I define the function, but need to also call it to make it run. If I'd just added one line to it, it would have been fine:
function greeter()
log("greeter script begin")
message("Welcome to Clonemaster")
endgreeter()
That seems so obvious in retrospect, but at the time, it cause me major headaches.
The other thing was the fact that Whoremaster needed the script exit so the SDL event loop could run. This meant that some things didn't happen when you asked them to happen. Message noxes just queued up, and that was fine. But image changes didn't happen until the script returned. So if you had three different images to illustrate those queued text boxes, they all be shown at once, at the end, and only the last one would be seen. Likewise, menus never got drawn until the script returned.
Eventually, I figured this out, and decided that what I needed was to have a separate namespace for each script, and for each one to have "prepare" and "run" methods.
Example = Example or {}
local stage = 0
Example.prepare = function()
// set up the class variables ready for the script to run
stage = 0
end
Example.run = function()
// set up the class variables ready for the script to run
stage = stage + 1
if stage == 1 then
// do stuff up to the first change of scenerey
elseif stage == 2 then
// do stuff up to the second...
// ...
end
endThe prepare idea was partly because necno's scripts in WM had a prepare method, and partly because I needed something to reset events when they were triggered.
Actually, that's probably still a pretty good event template for Clonemaster. It's just that I'm using scripts in a lot of other ways too, so I think I need to be a bit more flexible in how I call them.
One possibility which occured to me some time ago: if I can factor the SDL event loop into a C++ function, I could call it from inside the script. That would simplify a lot of the Lua architecture. It was too big a change for WM; for Clonemaster ... I'm worried that I'd need multiple invocations until it was ready to return to the script, and that in that time, the user would find a way of triggering another script, resulting in multiple embedded event loops, and some very hard to find errors. I still am, and I'm not going to do it for that reason...
Need to think about this a bit more..
No comments:
Post a Comment