• Advertisement
Sign in to follow this  

Same function name in 2 lua threads?

This topic is 4051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I'm pretty new to LUA and the way it works is making my head hurt, so go easy on me! I'm trying to integrate LUA scripting into my game engine, and it's going pretty well up to now. I have objects load their definitions from XML files, and a lua script name is given for object events (in this case, what happens when the object dies). I have two game objects, a ship and a rock. Each object has a seperate LUA file ascociated with it (ship_explode.lua and rock_explode.lua). Inside each file is a function called onGarbage() which tells each object how to die. The problem is that when I create seperate threads for these two scripts, the function definition in the file which was loaded last overwrites the one in the other thread and I end up with two object executing the same function call from 2 independent threads. How do you go about created 2 distinct threads that can call their own version of the function?

Share this post


Link to post
Share on other sites
Advertisement
Anyone any thoughts on this? I found some information regarding replacing the LUA_GLOBALSINDEX table with one for each thread. But my understanding of how this works is failing me for the time being!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look at the function setfenv, it can be used to change the global enviorment, i never used it so i know not much about it :).

If you plan to use lua i recommend this book
http://www.amazon.com/Programming-Lua-Second-Roberto-Ierusalimschy/dp/8590379825/ref=sr_11_1/104-7816202-3950323?ie=UTF8&qid=1174560329&sr=11-1

Share this post


Link to post
Share on other sites
I'm looking for something on the C side really. Something that can be set when my script manager loads a script and creates a new thread. Surely it must be possible for each thread to call its own version of a function?

Share this post


Link to post
Share on other sites
Why? The general definition of a thread (as opposed to a process) is that they share memory space. Your functions are in that memory space as part of the shared data, and therefore you can't call two functions the same thing. Basically, those functions should not be global, but should be part of (or contained within) individual objects, one for the ship, and one for the rock.

Share this post


Link to post
Share on other sites
Thanks. I'm a bit clearer on this now. I've redesigned it now so that each object can have a named function attached rather than a script file. So for instance when a rock dies it calls "large_rock_on_garbage()" instead of the global garbage() function.

This is part of my final year uni project and I've been working on it solidly for the past 3 weeks, so my brain is a bit frazzled to say the least!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement