C calls Lua or Lua calls C?

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

C calls Lua or Lua calls C?

Steve Litt
Hi all,

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?

Thanks

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt


Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Marc Balmer
> 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?

I am not a game programmer, but I fire up a program written in C which
then creates a Lua state, looks in a certain directory for scripts, and
if found, executes them.  These scripts can then register various
callbacks with the main program.

Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Lee Hickey
They (we) fire up a C program that calls a Lua script. I usually have the game engine (C) call a bootstrap.lua file as the first thing it does. This then sets up the engine/game as appropriate.


On 19 January 2011 10:38, Marc Balmer <[hidden email]> 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?

I am not a game programmer, but I fire up a program written in C which
then creates a Lua state, looks in a certain directory for scripts, and
if found, executes them.  These scripts can then register various
callbacks with the main program.


Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Christian Tellefsen
In reply to this post by Marc Balmer
On 1/19/2011 11:38 AM, Marc Balmer 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?
> I am not a game programmer, but I fire up a program written in C which
> then creates a Lua state, looks in a certain directory for scripts, and
> if found, executes them.  These scripts can then register various
> callbacks with the main program.
>
>
I'm a game programmer :D , and I do this as well. Start the game, which
registers bindings and such, and then run a startup.lua file.

Cheers,
Christian Tellefsen.



Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Enrico Colombini
In reply to this post by Steve Litt
On 19/01/2011 11.35, 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?

In adventure games for a resource-constrained device, I used a simple
C++ scheduler (called from the frame loop) to launch or resume Lua
coroutines, which in turn called back the C++ game primitives through a
plain C interface. I kept game data on the Lua side.

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Rena
On Wed, Jan 19, 2011 at 04:34, Enrico Colombini <[hidden email]> wrote:

> On 19/01/2011 11.35, 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?
>
> In adventure games for a resource-constrained device, I used a simple C++
> scheduler (called from the frame loop) to launch or resume Lua coroutines,
> which in turn called back the C++ game primitives through a plain C
> interface. I kept game data on the Lua side.
>
> --
>  Enrico
>
>

I usually have a "bootloader" C program that creates a Lua state and
exposes some internals to it and lets it go nuts. A lot of my programs
however are a Lua script that loads a bunch of modules (some of which
are written in C); it's not infeasible that a game engine could be
done that way.

There is no best way, except whichever works best for you.

--
Sent from my toaster.

Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Steve Litt
On Wednesday 19 January 2011 20:56:18 HyperHacker wrote:

> I usually have a "bootloader" C program that creates a Lua state and
> exposes some internals to it and lets it go nuts.


> A lot of my programs
> however are a Lua script that loads a bunch of modules (some of which
> are written in C); it's not infeasible that a game engine could be
> done that way.

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?

Thanks

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt


Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

云帆江
if you are using debian based os, just use apt-get source to get the
source code of lua5
and in the package, there's example for calling c module from lua and
calling lua from c

On Thu, Jan 20, 2011 at 12:02 PM, Steve Litt <[hidden email]> wrote:

> On Wednesday 19 January 2011 20:56:18 HyperHacker wrote:
>
>> I usually have a "bootloader" C program that creates a Lua state and
>> exposes some internals to it and lets it go nuts.
>
>
>> A lot of my programs
>> however are a Lua script that loads a bunch of modules (some of which
>> are written in C); it's not infeasible that a game engine could be
>> done that way.
>
> 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?
>
> Thanks
>
> SteveT
>
> Steve Litt
> Recession Relief Package
> http://www.recession-relief.US
> Twitter: http://www.twitter.com/stevelitt
>
>
>



--
welcom to gtalk me
http://hi.baidu.com/jyf1987

Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Sean Conner
In reply to this post by Steve Litt
It was thus said that the Great Steve Litt once stated:
> On Wednesday 19 January 2011 20:56:18 HyperHacker wrote:
>
> > however are a Lua script that loads a bunch of modules (some of which
> > are written in C); it's not infeasible that a game engine could be
> > done that way.
>
> 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?

  Here's one for Unix (tested under Linux) I wrote.  Creates a table, fills
it with a few values, the current working directory, the current PID and the
load average (an array); the load average has a metatable controlling how
it's printed.

[spc]lucy:/tmp>gcc -g -shared -fPIC -o silly.so silly.c
[spc]lucy:/tmp>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> require "silly"
> x = silly.info()
> print(x.cwd,x.pid,x.loadave)
/tmp    22647   (0.06 0.04 0.01)
> os.exit()
[spc]lucy:/tmp>

  -spc (Code follows ... )

/****************************************************************************
*
* A silly example of extending lua via the shared object method.
* Here's how this is done under Linux:
*
* gcc -g -fPIC -shared -o silly.so silly.c
*
* You will then have a shared object you can load into the lua interpreter.
*
*****************************************************************************/

#define _BSD_SOURCE

#include <stddef.h>
#include <string.h>
#include <stdlib.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include <unistd.h>

/******************************************************************/

static int silly_loadave__toprint(lua_State *L)
{
  char       buffer[BUFSIZ];
  lua_Number load[3];
  int        i;
 
  if (lua_type(L,1) != LUA_TTABLE)
    return luaL_error(L,"Expecting table, but got %s",luaL_typename(L,1));
 
  for (i = 0 ; i < 3 ; i++)
  {
    lua_pushinteger(L,i + 1);
    lua_gettable(L,1);
    load[i] = luaL_checknumber(L,-1);
  }
 
  sprintf(buffer,"(%.2f %.2f %.2f)",load[0],load[1],load[2]);
  lua_pushstring(L,buffer);
  return 1;
}

/*******************************************************************/

static int silly_loadave__newindex(lua_State *L)
{
  return luaL_error(L,"Attempting to upate a read-only table");
}

/******************************************************************/

static int info(lua_State *L)
{
  lua_newtable(L);
 
  lua_pushliteral(L,"pid");
  lua_pushnumber(L,getpid());
  lua_settable(L,-3);
 
  char buffer[FILENAME_MAX];
 
  getcwd(buffer,FILENAME_MAX);
  lua_pushliteral(L,"cwd");
  lua_pushlstring(L,buffer,strlen(buffer));
  lua_settable(L,-3);

  double load[3];
 
  if (getloadavg(load,3) != -1)
  {
    /*----------------------------------------------
    ; equiv of
    ; loadave = {}
    ; loadave[1] = x;
    ; loadave[2] = y;
    ; loadave[3] = z;
    ;----------------------------------------------*/
   
    lua_pushliteral(L,"loadave");
    lua_createtable(L,3,0);
   
    lua_pushnumber(L,1);
    lua_pushnumber(L,load[0]);
    lua_settable(L,-3);
   
    lua_pushnumber(L,2);
    lua_pushnumber(L,load[1]);
    lua_settable(L,-3);
   
    lua_pushnumber(L,3);
    lua_pushnumber(L,load[2]);
    lua_settable(L,-3);
   
    /*---------------------------------------------
    ; mt = {}
    ; mt.__tostring = silly_loadave__toprint
    ; mt.__newindex = silly_loadave__newindex
    ; setmetable(loadave,mt);
    ;---------------------------------------------*/
   
    lua_createtable(L,0,2);
    lua_pushliteral(L,"__tostring");
    lua_pushcfunction(L,silly_loadave__toprint);
    lua_settable(L,-3);
   
    lua_pushliteral(L,"__newindex");
    lua_pushcfunction(L,silly_loadave__newindex);
    lua_settable(L,-3);
   
    lua_setmetatable(L,-2);
   
    lua_settable(L,-3);
  }

  return 1;
}

/************************************************************************/  

static const struct luaL_reg reg_silly[] =
{
  { "info" , info },
  { NULL   , NULL }
};

int luaopen_silly(lua_State *L)
{
  luaL_register(L,"silly", reg_silly);  
  return 1;
}

/************************************************************************/


Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Enrico Colombini
In reply to this post by Steve Litt
On 20/01/2011 5.02, Steve Litt wrote:
> 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?

Suggested reading:
http://www.inf.puc-rio.br/~roberto/pil2/

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Henning Diedrich
In reply to this post by Steve Litt
Hi Steve!

On 1/20/11 5:02 AM, Steve Litt wrote:
I'm glad you responded. In my research I've found no examples of how to start in Lua and load C modules
...
trivial "hello world" application of calling C from Lua for Lua 5.1?

Here is a static Lua-C Hello World Sample / Tutorial.

This links a C extension statically into Lua. It's the other option you have, beside dynamic linking, that ties your extension in as closely as the Lua standard libs that need no require() call to be available, and thus no pathes. The modification of linit.c does the loading.

At the end there's how you integrate it into the official Lua Makefile. That's the one in the src folder, not the one in ../src.

Best,
Henning
File ../src/hello.lua:
----------------
hellolib.hello()


File src/hello.c:
-----------------
#include <stdio.h>
#include "lauxlib.h"

static int hello(lua_State *L) {
    printf("Hello World!\n");
    return 1;
}

static const struct luaL_Reg hellolib [] = {
    { "hello", hello }, /* registering the function */
    { NULL, NULL }
};

LUALIB_API int luaopen_hellolib (lua_State *L) {
    luaL_register(L, "hellolib", hellolib); /* registering the module */
    return 1;
}


File src/hello.h:
-----------------
#include "lua.h"

#define HELLO_LIBNAME    "hellolib"
LUALIB_API int (luaopen_hellolib) (lua_State *L);


Extend file src/linit.c:
------------------------
...
#include "lauxlib.h"
#include "hello.h" /* add */
...
static const luaL_Reg lualibs[] = {
...
  {"hellolib", luaopen_hellolib}, /* add */
  {NULL, NULL}
...


Extend src/Makefile:
--------------------
...
LIB_O=    lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
    lstrlib.o loadlib.o linit.o hello.o # add
...
hello.o: hello.c # add

# (end of Makefile)


Build & Run
-----------
Build Lua from ../src/ folder, e.g. sudo make

Run src/lua hello.lua

Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Philippe Lhoste
In reply to this post by Steve Litt
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
--  --  --  --  --  --  --  --  --  --  --  --  --  --


Reply | Threaded
Open this post in threaded view
|

Re: C calls Lua or Lua calls C?

Gaspard Bucher
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).


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 <[hidden email]> 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
--  --  --  --  --  --  --  --  --  --  --  --  --  --