Looking for help with using multiple lua files
Looking for help with using multiple lua files
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.
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.
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Looking for help with using multiple lua files
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
If your file is in the folder "src" you would do
You can also use it as a module, doing this in the otherluafile.lua
then in your main.lua you would do
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
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
Code: Select all
require "otherluafile"
Code: Select all
require "src.otherluafile"
Code: Select all
local mytable = {
func = function (t)
print("Hello "..t.."! this is my great module")
end,
a = "HELLO WORLD"
}
return mytable
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
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
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Looking for help with using multiple lua files
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
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.
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
Re: Looking for help with using multiple lua files
http://www.love2d.org/wiki/love.filesys ... ctoryItemsnatev wrote:I would like to get the contents of a subdirectory of my Love main folder
To run the .lua files afterwards, use http://www.love2d.org/wiki/love.filesystem.load
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Looking for help with using multiple lua files
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:Azhukar wrote:To run the .lua files afterwards, use http://www.love2d.org/wiki/love.filesystem.load
Code: Select all
loadstring(love.filesystem.read("file.lua"))()
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Looking for help with using multiple lua files
You don't.Positive07 wrote:you need to sandbox it
Re: Looking for help with using multiple lua files
Thank you! Again, working perfectly.
I appreciate the help greatly.
I appreciate the help greatly.
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Looking for help with using multiple lua files
Well, you do...Azhukar wrote: You don't.
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()
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Looking for help with using multiple lua files
Then you don't code in lua or for an opensource framework.Positive07 wrote:You dont want users running untrusted code in your game
You funny man.Positive07 wrote:If karma existed I would probably rate you wrong because of that last answer.
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Looking for help with using multiple lua files
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.
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Who is online
Users browsing this forum: No registered users and 3 guests