Lua question - loading multiple scripts

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Moosader
Game Developer
Game Developer
Posts: 1081
Joined: Wed May 07, 2008 12:29 am
Current Project: Find out at: http://www.youtube.com/coderrach
Favorite Gaming Platforms: PC, NES, SNES, PS2, PS1, DS, PSP, X360, WII
Programming Language of Choice: C++
Location: Kansas City
Contact:

Lua question - loading multiple scripts

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua question - loading multiple scripts

Post 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.
User avatar
Moosader
Game Developer
Game Developer
Posts: 1081
Joined: Wed May 07, 2008 12:29 am
Current Project: Find out at: http://www.youtube.com/coderrach
Favorite Gaming Platforms: PC, NES, SNES, PS2, PS1, DS, PSP, X360, WII
Programming Language of Choice: C++
Location: Kansas City
Contact:

Re: Lua question - loading multiple scripts

Post 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.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Lua question - loading multiple scripts

Post 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
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Moosader
Game Developer
Game Developer
Posts: 1081
Joined: Wed May 07, 2008 12:29 am
Current Project: Find out at: http://www.youtube.com/coderrach
Favorite Gaming Platforms: PC, NES, SNES, PS2, PS1, DS, PSP, X360, WII
Programming Language of Choice: C++
Location: Kansas City
Contact:

Re: Lua question - loading multiple scripts

Post 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;
andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: Lua question - loading multiple scripts

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua question - loading multiple scripts

Post 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.
User avatar
Moosader
Game Developer
Game Developer
Posts: 1081
Joined: Wed May 07, 2008 12:29 am
Current Project: Find out at: http://www.youtube.com/coderrach
Favorite Gaming Platforms: PC, NES, SNES, PS2, PS1, DS, PSP, X360, WII
Programming Language of Choice: C++
Location: Kansas City
Contact:

Re: Lua question - loading multiple scripts

Post 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.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Lua question - loading multiple scripts

Post 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)
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
Post Reply