lua-users home
lua-l archive

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



When you say thread, do you mean OS thread or Lua co-routines (which are
type "thread")? If you mean co-routine, a Lua function is used to
establish the co-routine in which you can place any variables you like
and since Lua supports nested functions and lexical scoping any
declaration of "this" in the co-routine creation will be inherited by
the nested functions. You could also use setfenv() to set explicitly the
execution environment of each co-routine. You can basically customise
the scoping here using metamethods or by creating an environment table,
into which you place everything you want the co-routine to see.

Nick


-----Original Message-----
From: Joel Pritchett [mailto:jpritchett@sammystudios.com] 
Sent: Wednesday, October 08, 2003 7:47 PM
To: lua@bazar2.conectiva.com.br
Subject: Thread woes

I'm using lua in a game where we have a considerable amount (30+) of lua
controlled objects running around. In order to free up memory I'm trying
to get each object running off of a thread that was spawned off of a
master state. This allows me to load all of the standard libs and a fair
amount of my own into the state so that I can have a lot of objects
running around using the global copy of the libraries and not have to
load their own.

Now my problem is this: Since there is only one global table, how can I
add a unique 'this' pointer ( as well as some other game object specific
data ) to each thread as I was able to do when they each had their own
state so I can then pass this data to our functions? Is there anyway to
add them to the execution state so they look like any other variable
declared during normal execution? Ideally I'd like to be able to use
this data as if it were in the global table or had been declared in
script, but cannot find a way to do this. 

There are a lot of less than nice solutions to this problem that I'd
like to avoid.

Thanks!
Joel