lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


(Apologies if this ends up as something of a double post.)

> in general, you have to design some API to expose the functionality of your core to Lua.  there are several styles for this, and you can create your own.
>
> specifically, for events and timers, two main ways are:
>
> 1) installing callbacks
>
> 2) using coroutines

I whipped up a little example, using callbacks, on top of another demo
I did a short while ago. I've got some coroutine functionality that
would be worthwhile too, but it might take some time to write a useful
example (the Textbox update logic may give some hint of it, though; see
below). Still, one of said callbacks uses a coroutine.

I've always gotten really good results by having a few event streams,
executed at key points, into which you can add events at will. All the
events in these streams get executed, in order, and can choose to stick
around afterward (in this case, by returning true) for next time.

In the linked demo, and in the game code from which it was adapted,
the bulk of the game flow occurs in Lua, which it enters at a few
well-defined points. These entry point functions in turn send messages
to the current section ("section" is, more or less, a poorly chosen name
for a window and its callback). Certain important messages are sandwiched
between event stream executions, e.g.

 -- The "enter_update" stream is executed
 -- The current section processes its update logic
 -- The "leave_update" stream is executed

I hope that made at least some sense; this is a bit difficult to
explain concisely. :P
It might be more evident in the code, which you can find in the
"Scripts" folder.
In particular see these:

Base/CoroutineOps.lua

-- Some useful control flow operations.

Class/Control/Stream.lua

-- The stream object itself.

Class/UI/Widget/Textbox.lua

-- Possibly interesting (relevant?) use of coroutine.

Game/Game.lua

-- At the top are the F1, F2, F3 events.
-- The entry points, which could be exposed to C++ and lua_pcall()'d
into. (Never mind WithBoundGroups. It just wraps some context around
the function for split-screen awareness.)

Section/Main.lua

-- An example section, which also throws together the GUI.

Support/Section.lua

-- The stock section implementation, with the event streams.

Support/Tasks.lua

-- These are a bunch of pre-built tasks or task primitives, including
some of those I use here.

Support/Transitions.lua

-- Some more task stuff, mostly for GUIs, though unfortunately I
haven't added an example.

Support/Windows.lua

-- Some of the code behind the section and GUI stuff.

Anyhow, you can find the demo here:

http://blog.xibalbastudios.com/?p=50

and run it with this program:

http://love2d.org/download

The .love is just a renamed zip file, so switch it back to .zip to inspect
the code. If you're on Windows, the batch file in the zip can generate
some documentation (see post), though almost all relevant material is
in the half I have yet to write. :(

In the demo itself, you can press F1, F2, or F3 to set a one-time
timer; set a periodic, permanent timer; or run an asynchonous event,
respectively. A bunch of stuff will print out on the left side.