Page 1 of 1
Lua question - loading multiple scripts
Posted: Sun Nov 22, 2009 11:14 pm
by Moosader
I'm playing around with Lua (in C++) a little bit finally, and writing a basic text adventure with it. Anyways, I want to separate different things like the map table and functions from the item table and functions, but I can't really find anything on how to have multiple scripts open.
Or am I overcomplicating it? I just want to call different functions from different .lua files.
Re: Lua question - loading multiple scripts
Posted: Mon Nov 23, 2009 12:11 am
by Falco Girgis
I don't think that I really understand what you want (because there are lots of ways to do it).
You can either include the lua scripts with the desired functions inside of the lua script that you have open. Or you can create another instance of the Lua interpreter, and load the script there. OR, you can break the existing VM instance into multiple threads/coroutines, each with their own Lua script loaded.
Re: Lua question - loading multiple scripts
Posted: Mon Nov 23, 2009 1:02 am
by Moosader
I mainly just want to have multiple .lua files for different types of data types and functions (like how we separate classes into separate .h files).
Basically, so the lua script doesn't turn out like Shattered Wrath's long-ass file:
http://www.moosader.com/misc/combatsystem.lua
I thought I had recalled someone telling me that there was a way to store all that in separate files.
Re: Lua question - loading multiple scripts
Posted: Mon Nov 23, 2009 5:04 am
by short
I know this isn't helpful, at all, but
"TEXT::DON’T COME ANY CLOSER!, he says, brandishing an office chair.",
pretty funny to read through your dialogue haha
Re: Lua question - loading multiple scripts
Posted: Mon Nov 23, 2009 11:44 am
by Moosader
short wrote:I know this isn't helpful, at all, but
"TEXT::DON’T COME ANY CLOSER!, he says, brandishing an office chair.",
pretty funny to read through your dialogue haha
Oh, yeah. That script file is from a group project I worked on last Spring. Someone else did the scripting and story, so I was like "wtf" when I first read that. XD;
Re: Lua question - loading multiple scripts
Posted: Mon Nov 23, 2009 12:52 pm
by andrew
Moosader wrote:I'm playing around with Lua (in C++) a little bit finally, and writing a basic text adventure with it. Anyways, I want to separate different things like the map table and functions from the item table and functions, but I can't really find anything on how to have multiple scripts open.
Or am I overcomplicating it? I just want to call different functions from different .lua files.
If you haven't already figured it out, you can use:
dofile("file.lua") to include another script.
Here's a reference.
Re: Lua question - loading multiple scripts
Posted: Mon Nov 23, 2009 2:23 pm
by Falco Girgis
I guess what you want is probably just the equivalent of #include, which is dofile() as andrew said. But I must say, Shattered Wrath looks ridiculously long as a result of poor design, rather than just not being able to separate things into files.
That's got to be at least three to five times longer than the longest script in ES. Does your engine handle anything other than just rendering/collision? You probably would have been better off just writing the entire thing in Lua and saying screw C++, if Lua is going to be doing everything.
Re: Lua question - loading multiple scripts
Posted: Mon Nov 23, 2009 2:36 pm
by Moosader
Hmm, I could do everything in Lua, but I kinda wanted to get familiar using it with C++. It's just going to be a basic text adventure, tho, with moving, battles, items, and such.
Re: Lua question - loading multiple scripts
Posted: Tue Dec 01, 2009 5:12 am
by M_D_K
use require() it's pretty much #include
Programming in lua book wrote:
Lua offers a higher-level function to load and run libraries, called require. Roughly, require does the same job as dofile, but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work. Because of these features, require is the preferred function in Lua for loading libraries.
somescript.lua
Code: Select all
--over simplified but whatever
GlobalVar = 20 --OH NOEZ
function SomeFunction(Str)
print("Got string =>", Str)
end
main.lua
Code: Select all
require("somescipt.lua")
SomeFunction("This is a String")
print(GlobalVar)