Sign in to follow this  

Lua Function Stack

This topic is 2662 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

Hello,

I am trying to figure out how this works a bit more.

Essentially I am running into a case where two scripts have the same functions (Start, Update, and let's call one Test). After loading both scripts, I can push the Start/Update function on the stack and call them from C++ and they run their respective functions. However, within the lua script, I call Test() from the Update function and both scripts end up using the Test() function from the last loaded script. So my question, what really is going on? I thought both threads/states have their own stack and their own functions. How is this different from me calling Update from C++?

Thanks
Jeff.

Share this post


Link to post
Share on other sites
Sorry, this may not be helpful since I don't really use Lua and don't quite understand exactly what you mean by "last loaded script", but, just in case..
I recall reading that Lua uses coroutines, so it suspends states and returns to them. If that is not at all helpful, please ignore..

Share this post


Link to post
Share on other sites
Lua doesn't have "scripts". When you use something like lua_dofile(), what's happening is that Lua loads in the file, wraps it up as a large, single function, and executes it. So if you have a file whose contents are "a = 3; function foo() print "bar" end;", that's a function which, when run, sets the global variable "a" to 3 and sets the global variable "foo" to a function which prints "bar".

So when you say that you have "two scripts which have the same functions", what you really mean is that you have two functions which write different function values into the same global variables.

Now, you mention having different "states". States which are created with lua_open() really will be completely separate from each other (different "global states") and global variables you set in one will not affect those you set in another. But if you create them via lua_newthread() or the lua-space equivalent, the resulting states will have their own stacks but not their own global tables.

That's just some scattershot info. If you find nothing in it helps, post code.

Share this post


Link to post
Share on other sites
They both load into the same global environment so the last script loaded if it overlaps with the previous scripts namespace will overwrite its functions. People imagine each script gets it's own environment but that's not the case, you'll have to manage that yourself using which ever paradigm is most suitable for your case.

Some people create a new global environment per script and switch between them, or they use OOP where a single script encapsulated a class which can be used to spawn instances of that scriptable object etc.. There's been tons of discussion on this topic before , you can do a search for it.

Good Luck!

-ddn

Share this post


Link to post
Share on other sites
Sneftel,

Excellent description. That's exactly my problem, I was loading a script and setting up a global function 'Update', then redefining it in another script. If then I execute Update in both threads (lua threads) I was expecting to call each function. However, since they share the same global table it's overwriting them. Is there anyway to keep a reference to each 'Update' function in the global table without having to create a separate 'global state' so I can still share data between them, but still use similar functions like 'Start', 'Update', etc.?

Thanks
Jeff.

Share this post


Link to post
Share on other sites
The way i've been doing it in my own software is to actually passing around tables instead of raw functions. Since you can define functions and data in tables, it provides a simple way to bundle up functions and data for a given entity.

Share this post


Link to post
Share on other sites

This topic is 2662 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this