Looking for help with using multiple lua files

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
natev
Prole
Posts: 14
Joined: Thu Nov 20, 2014 3:55 pm

Looking for help with using multiple lua files

Post by natev »

Hello! Just discovered Love and, of course, love it. I've loved Lua since discovering it while working on mods for ToME, and finally got around to looking for something that had the tools I needed. My first project is trying to recreate the tactical minigame from Endless Legend so that I can play with programming better AI :)

My main.lua is quickly becoming unmanageable, and, beyond that, I would love to enable selection of different AI, worldgen, etc models, each with its own lua. I would prefer these to be in my main folder rather than a user/appdata/etc folder. But I'm not sure how to read different lua files. dofile doesn't see my files (and I understand module is to be avoided?)

Is anyone willing to help explain the best way to do this? Failing that, can anyone recommend some public code that does this, hopefully as simply as possible?

Thanks in advance for your time.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Looking for help with using multiple lua files

Post by Positive07 »

In lua you can require a file which basically means that it will read the file and execute it as a function. To do this you can do

Code: Select all

require "otherluafile"
If your file is in the folder "src" you would do

Code: Select all

require "src.otherluafile"
You can also use it as a module, doing this in the otherluafile.lua

Code: Select all

local mytable = {
    func = function (t)
        print("Hello "..t.."! this is my great module")
    end,
    a = "HELLO WORLD"
}
return mytable
then in your main.lua you would do

Code: Select all

print(mytable) --nil
local mytable = require "src.otherluafile"
mytable.func("natev") --Hello natev! this is my great module
print(mytable.a) --HELLO WORLD
This is prefered, since locals dont spread across everywhere as globals do... but dont bother about this right now


NOTE: All the files should be next to your main.lua file, they could be in folders but those folders should be next to the main.lua, in the example above the directories inside the .love file should look like this

Code: Select all

>main.lua
>src
>>otherluafile.lua
And all that should be inside the .love file.
If you want to run files from other directory, outside of the .love file then I shall tell you that it's trickier and not safe (it's possible though), but this should be enough to help you with what you asked for
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
natev
Prole
Posts: 14
Joined: Thu Nov 20, 2014 3:55 pm

Re: Looking for help with using multiple lua files

Post by natev »

Thank you so much! That works perfectly!

If I can ask one more question:

I would like to get the contents of a subdirectory of my Love main folder so that I can use it to populate menus, something like

Code: Select all

menu = UI:newMenu("Worldgen")
for _, filename in ipairs(getDir("generators")) do
     menuItem = menu:newItem(filename)
     menuItem.onSelect = function(self) return (require ("generators."..filename)) end
end
I found OS dependent getDir code via io.popen on StackOverflow, but it's in absolute terms ("C:/whatever") and I'd really rather have it relative to my main.lua.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Looking for help with using multiple lua files

Post by Azhukar »

natev wrote:I would like to get the contents of a subdirectory of my Love main folder
http://www.love2d.org/wiki/love.filesys ... ctoryItems

To run the .lua files afterwards, use http://www.love2d.org/wiki/love.filesystem.load
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Looking for help with using multiple lua files

Post by Positive07 »

Azhukar wrote:To run the .lua files afterwards, use http://www.love2d.org/wiki/love.filesystem.load
Well you could still use require, [wiki]love.filesystem.load[/wiki] is mostly used with files in the save directory, but I think that is trickier, cause you need to sandbox it and such... also I prefer:

Code: Select all

loadstring(love.filesystem.read("file.lua"))()
Because if I wanted I could change the content of the file as a string, before loading it as lua code... just saying, natev doesnt need this, so to answer the question:

Use [wiki]love.filesystem.getDirectoryItems[/wiki] and [wiki]require[/wiki] but take into account that require uses dots as folder separator and the extension is not included while love.filesystem uses "/" and needs the file extension.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Looking for help with using multiple lua files

Post by Azhukar »

Positive07 wrote:you need to sandbox it
You don't.
natev
Prole
Posts: 14
Joined: Thu Nov 20, 2014 3:55 pm

Re: Looking for help with using multiple lua files

Post by natev »

Thank you! Again, working perfectly.

I appreciate the help greatly.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Looking for help with using multiple lua files

Post by Positive07 »

Azhukar wrote: You don't.
Well, you do...
You dont want users running untrusted code in your game... they could break it and make it unusable. For example this code:

Code: Select all

love.update = nil
love.draw = nil
love.errhand = love.event.quit
love.graphics = nil
error()
If you do love.filesystem.load() with a file like that and run it then your game will brake. If a user knows about this vulnerability they could use it to hack it too, for example modifying the score, or the names in the highscore table, getting items that are rare and such. So if you want to prevent this you NEED to sandbox your LOADED lua files. Require can only require files inside of the .love file so the user shouldnt be able to modify them (they can but they shouldnt).

Also as kikito told you before, you should explain your point of view instead off being aggressive. If karma existed I would probably rate you wrong because of that last answer. Also moderator may not like that attitude.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Looking for help with using multiple lua files

Post by Azhukar »

Positive07 wrote:You dont want users running untrusted code in your game
Then you don't code in lua or for an opensource framework.
Positive07 wrote:If karma existed I would probably rate you wrong because of that last answer.
You funny man. :crazy:
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Looking for help with using multiple lua files

Post by Positive07 »

I code in lua, in an open source framework, for users! And there are good users and bad users, I want good users to enjoy what I develop and I want bad user to stay away of doing bad things.
In order to do this I provide a nice playground with many things to modify and play with, and where nothing can be broken (if you are playing with something and it breaks or it breaks something else then you wouldn't have fun so I take that into account).
Also bad users want to do many things with my game, I allow them to do it too, but to a limit. If they wanted to play more, or develop my game more or in different ways, they could still open the .love file or download the source code from github and make whatever they wanted with it (cheating and attacking my server in multiplayer games included) but there are little to no person that would do this, while there are lots of person that are going to play with the modding system I described above.
Even then, this is how I think and how I develop, if you have other point of view then that is fine too.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 62 guests