lua-users home
lua-l archive

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


The first implementation of Lubyk was doing things with C++ starting first and then Lua being called. This was easy to setup (all required C code was in memory before Lua started) but was hard to "mock" for testing.

In the new rewrite of Lubyk, I am doing everything the other way around. I use the plain "lua" binary to run the scripts and they load the C code and do whatever is needed (you can make OpenGL games with this). This setup is really nice for testing and to type "run" in a script editor. The only thing that has to be done to either

a. install all the shared objects and scripts in the usual Lua load path (as compiled, /usr/local/lua/5.1 in my case).
b. install a boot script in the usual place that alters the package paths (this is what I do).

A trivial example https://github.com/lubyk/lubyk#L11

The first line says "require 'rubyk' " (rubyk is the old name for lubyk and this line will be changed accordingly). What this line does is simply append the paths for lubyk stuff to the package C and Lua paths and transform the _G table to autoload (loads missing packages automagically).

For example, "app = mimas.Application()" loads core "mimas" code (Qt based GUI), the loads "mimas.Application" code, then creates the application. This is all that is needed to show a window:

require 'rubyk'
app = mimas.Application()
win = mimas.Window()
win:show()
app:exec()

Another advantage of a "pure Lua" approach is that parts of your code can be easily reused in other projects.

Gaspard

On Thu, Jan 20, 2011 at 10:39 AM, Philippe Lhoste <PhiLho@gmx.net> wrote:
On 20/01/2011 05:02, Steve Litt wrote:
In general, when game programmers use C and Lua, to they fire up a C program
that calls a Lua script, or do they fire up a Lua script that calls a C
program, or are both commonly done?

Actually, when you "fire up a Lua script", you run a C program first! It is the first and primary application using Lua, the Lua command line interface calling the Lua interpreter.


I'm glad you responded. In my research I've found no examples of how to start
in Lua and load C modules, nor any documentation of same. Do you know of a
trivial "hello world" application of calling C from Lua for Lua 5.1?

Two ways:
- just recompile the interpreter with your C module, exposing it as as some library (look at I/O module, for example);
- or make a dynamic library to be used with 'require'.

The first one is used by applications scripted with Lua (games, editors...); the second one is used when you want to use the Lua language (interpreter with command line handling) with some added facilities (filesystem, sockets, etc.).

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --