Embbeding LUA/multithread

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

Embbeding LUA/multithread

David Diaz
  Hi !!! I'm new on the list. My name's David. I'm trying to
  embbed LUA 4.0 on my game engine and I've got a problem:
  I need to run many scripts at the same time concurrently on
  the same lua_state. This is so 'cause I register many functions
  that would act as an API interface from LUA scripts to my engine.
  The problem is that Lua seems not to support concurrent access
  to states. Is this true ?  Is there any way to implement this ?

  Since now I'm creating a new state for each script. Then I
  register the API functions on EVERY state, and launch the
  script. As I create a new state for every script running, I
  have to register ALL the API functions (near 300) for each
  script run and I can't use global variables. Loosing global
  variables are not a problem, but I don't like to register
  the functions each time.

  Any Idea ? Thax 8)


Reply | Threaded
Open this post in threaded view
|

Re: Embbeding LUA/multithread

Sebby
Hi.

I have tried implementing the same thing for a game engine. There 
also has been many threads about this subject in the past. Basicaly 
there is 2 approaches. One is to use co-routines and seperate the 
global and "thread" specific components of a Lua state. The other is 
to make Lua non-recursive (which will probably be implemented in 
Lua's next version).

I have posted some code snippits from my engine a few months ago and 
there has been sevral discussions about the subject so i suggest 
searching a few months back in the mailing list archives (or use 
www.egroups.com to do a search).

If you have aditional questions, don't hesitate to ask me.

Sebastien St-Laurent
Software Engineer, Z-Axis ltd.
[hidden email]


> 
>   Hi !!! I'm new on the list. My name's David. I'm trying to
>   embbed LUA 4.0 on my game engine and I've got a problem:
>   I need to run many scripts at the same time concurrently on
>   the same lua_state. This is so 'cause I register many functions
>   that would act as an API interface from LUA scripts to my engine.
>   The problem is that Lua seems not to support concurrent access
>   to states. Is this true ?  Is there any way to implement this ?
> 
>   Since now I'm creating a new state for each script. Then I
>   register the API functions on EVERY state, and launch the
>   script. As I create a new state for every script running, I
>   have to register ALL the API functions (near 300) for each
>   script run and I can't use global variables. Loosing global
>   variables are not a problem, but I don't like to register
>   the functions each time.
> 
>   Any Idea ? Thax 8)


Reply | Threaded
Open this post in threaded view
|

Re: Embbeding LUA/multithread

Luiz Henrique de Figueiredo
In reply to this post by David Diaz
>  Since now I'm creating a new state for each script. Then I
>  register the API functions on EVERY state, and launch the
>  script. As I create a new state for every script running, I
>  have to register ALL the API functions (near 300) for each
>  script run and I can't use global variables. Loosing global
>  variables are not a problem, but I don't like to register
>  the functions each time.

I've posted one or more solutions for this. See the archives.
Here is a simple solution:

 Define a "getglobal" tag method that tries to match the name to one of
 your 300 API functions (if they all begin with the same prefix, this is
 easier). If there is a match, return the API function as a C function
 from the "getglobal" tag method.

--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Embbeding LUA/multithread

Mihai Palade
Hello,

I am new to lua. As a beginner I have the first problem. I need a very short
program to call a LUA procedure from C or Pascal(preferred). Let's say that
the lua procedure will print on screen "Hello Beginner".
Tank you !

Mihai


Reply | Threaded
Open this post in threaded view
|

Re: Embbeding LUA/multithread

Luiz Henrique de Figueiredo
In reply to this post by David Diaz
>I am new to lua. As a beginner I have the first problem. I need a very short
>program to call a LUA procedure from C or Pascal(preferred). Let's say that
>the lua procedure will print on screen "Hello Beginner".

How about this (for Lua 4.0):

Lua:
	function hello() print "Hello Beginner" end

C:
	lua_getglobal(L,"hello");
	lua_call(L,0,0);


--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Embbeding LUA/multithread

Erik Hougaard
----- Original Message ----- 
> How about this (for Lua 4.0):
> 
> Lua:
> function hello() print "Hello Beginner" end
> 
> C:
> lua_getglobal(L,"hello");
> lua_call(L,0,0);

Even shorter:

lua_dostring(L,"print 'Hello Beginner'");

/Erik


Reply | Threaded
Open this post in threaded view
|

Re: Embbeding LUA/multithread

Erik Hougaard
----- Original Message ----- 
> ----- Original Message ----- 
> > How about this (for Lua 4.0):
> > 
> > Lua:
> > function hello() print "Hello Beginner" end
> > 
> > C:
> > lua_getglobal(L,"hello");
> > lua_call(L,0,0);
> 
> Even shorter:
> 
> lua_dostring(L,"print 'Hello Beginner'");
> 
> /Erik

Ups... Try :
lua_dostring(L,"hello()"); 
...

/e