🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

More Lua...

Started by
6 comments, last by Illumini 20 years, 11 months ago
Since I made my previous post I''ve been trying to implemented a scripting system which is capable of running multiple scripts, with access to global variables. My aattempts have been somewhat of a failure though. I''ve tried numerous ideas, read the documentation, the wiki tutorials, etc. I just can''t get lua to do what I want it to do. Here is what I want. - I want to be able to run multiple scripts. Each script will have Main(), OnHit(), OnUse(), etc. These need to be local to the script some how - I want global variables shared between scripts (but not those functions I noted) - I want to be able to pause a script for a given length of time - I want to be able to call function from one script to another, using a C function as a go between I would assume. The basic I have been trying to do works like this. Create a lua thread for each script, create a wait command that yields a scripts exectution. A main loop constantly checks each script and calls resume if it hasn''t terminated or is still in Waiting state. This works well enough, for running multiple scripts. But when it comes to the functions with the same names... the second script loaded overwrites the first scripts functions in global space? ie script1 function Main() print("In script1 Main()") Wait(100) Test() end Test() print("In script1 Test()") end With script2 being the same minus altered output (saying script2 instead of 1). If I do the following in C LoadScript("script1"); LoadScript("script2"); DoScriptsLoop(); I get the following output In script1 Main() In script2 Main() In script2 Test() In script2 Test() Obviously each thread shares a refrence to the main state global table and the functions override the old when second script is loaded. This is not good though, since I want to have 10-20 cscripts running all with Main''s and OnWhatevers. I believe the answeer to this involves messing with tables, but anyone with a clean idea on how to fix this while keeping global variables I would be more than happy to hear from you. I''m not that expierienced with lua and I dont want to right some horribly hackish code to do something that could be done more elegantly. Thanks in advance.
Advertisement
You could encapsulate each set of functions in a table, a la:

obj = {
Main = function ()
end,
OnHit = function ()
end,
}

Making sure to name each one differently.

-or-

Name them all the same ("obj") and then at load time, copy "obj" into a unique name, and get rid of "obj".

I do the latter and it seems to work...my next step will be to sandbox the load-time object, so nothing else gets loaded along with it.....

-scott
I understand you logic, but I''m just missing on how to implement it a litt.e


Right now my load script code looks something like this.

Thread = lua_newthread(Lua);lua_dofile(Thread, filename);	lua_getglobal(Thread, "Main");lua_resume(Thread, 0);lua_pop(Thread, 1);



I believe as soon as I call dofile the functions overwrite whatever is already in the global table. So After that I need to take that "obj" table that is in the global table and rename it? Could you give a little example on this could be done?
There''s a bit of a tendency on the part of newer Lua users to use the term "script" as though it was the fundamental unit of execution. In fact, a script is just a function, which may or may not happen to leave stuff behind in the global table by the time it''s done.

Here''s sort of what a "script" is, in my engine:

room["bedroom"] = {    desc = "Bob''s bedroom",    enter = function()        -- do stuff    end,    leave = function()        -- do stuff    end}


In reality, there''s a little more syntactic sugar, but the basic idea is that what a "script" does is register stuff in a particular place. Actually executing stuff happens later on.

How appropriate. You fight like a cow.
I understand that Sneftel, what I don''t understand is how I''d design a scripting system I want LOL. I mean I can see many uses for Lua, but the particular way I want to use it seems difficult. I''m fine with putting my functions in a table, but I need some guidance on how to relocate that table and such.

First thing tho... How do I call a Lua function from C that is in a table?

I know this is in the doc, but I just can''t read that thing properly or something.

Thanks in advance.
I''ve been thinking it through and it would look something like this?

Call tablename.function()

lua_pushstring(Lua,"tablename"); // supply table name
lua_gettable(Lua, LUA_GLOBALSINDEX); // gets the tables index from global array
lua_pushstring(Lua, "function"); // function to get
lua_gettable(Lua, -2); // table to get from
// now function is on top of stack, so call it
lua_call(Lua, 0,0);

If any of you could check me on this I''d really apprieciate it.
Well I got that to work... Using what both of you suggested and my own table/function call, however I''ve run into a snag

obj = {

Main = function()
print("In Main()")
Test()
end,

Test = function()
print("In Test()")
end
}


Test doesn''t get run I assume I need to do obj.Blah(), but seems annoying at best, considering I can already seperate objects into different files (in fact thats what makes sense to me).

Is there a way around this or no?
Yeah, you are putting Test into the obj table, so the function is obj.Test. I''m not sure if this would work completely for what you are doing, but you could try changing the environment for the function you are calling to be the table that it is in. Normally the environment for a function is the globals table, but you can change it to be any other table (so when the function tries to use a global, instead of getting it from the globals table it comes from the table you set as the environment). Of course this is getting kind of confusing, so there might be a cleaner way to do this.

This topic is closed to new replies.

Advertisement