[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Callbacks.
- From: Jamie Webb <j@...>
- Date: Wed, 27 Aug 2003 08:54:43 +0100
On Wednesday 27 August 2003 08:19, Reyn Vlietstra wrote:
> Hi.
>
> I'm new to lua. I'd like to do the following:
> From lua, call a C function, with a lua function as one of the
> parameters, eg.
>
> RegisterLuaFunction(ptr,object.func,10)
> [ptr is pointer to a C++ object' instance, 10 is a future time in ms]
>
> That C function will then put the function on a std::priority_queue<> to
> be called later.
> I'v read that the registry would help out here, the problem is what
> should I use
> as a unique key for that ?(because the function can be on the queue more
> than once,
> I cant simply use the lua_topointer of the table)
Create a new table and put it in the the registry with a key of your choosing.
Then put your functions in that table, using e.g. a sequence of integers as
keys.
> What I would like to do is: Instead of placing the funcion on the
> registry, use
> (void*) var = lua_topointer(L,place where function is) and store that
> somewhere.
> The problem is, there isnt a lua_tofunction, so I cant put that back on
> the stack
> and call the function. This would have been nice because then I wouldnt
> need to
> access another table(registry) to find my function.[lots of
> misconceptions in that paragraph?]
Basically, the trouble is that in order to deal with a Lua object, you need to
make sure that it is always stored in a well-known place within the Lua
environment (such as the registry). Otherwise, you could take a pointer to
the object, then in the meantime Lua could garbage-collect it, then you would
try to use your pointer and probably crash your program.
> The other alternative is to make a priority queue in lua and do all my
> stuff there, would
> you recommend doing this ? (Its just looping through and calling all the
> functions in it,
> up to the current time, tho C performance would be nice)
You'll have to decide what makes most sense for you. If you are able to write
the code described above, then the C++ solution may be better as it will be
faster, and you already have the priority queue code. OTOH, the Lua priority
queue code may be easier to read, write, and maintain than the interface
code, so if you can live with the speed reduction and having to rewrite code
you already have, then you might end up with a more manageable program.
-- Jamie Webb
It's easy to say you want more money, and I suppose that's what I like
about it: sitting here, rocking back and forth, wanting that money...