Page 1 of 2
[Beginner] Organizing projects in smaller files
Posted: Tue Sep 29, 2015 1:05 pm
by Rishavs
Hi guys
New to lua/love.
I am currently learning from example files over the net and all of them work only with the main.lua file
I was wondering how do we break the code into smaller modules (mainly i want to understand how the modular code works with the callbacks).
Does anyone have any sample (simple) code that i can read through and understand?
regards
Rishav
Re: [Beginner] Organizing projects in smaller files
Posted: Tue Sep 29, 2015 1:14 pm
by TheOdyssey
I'll try to find some example code later but for now, I suggest reading the
Lua Users Modules tutorial. It should give you a basic idea on how to use modules
Re: [Beginner] Organizing projects in smaller files
Posted: Tue Sep 29, 2015 2:22 pm
by Rishavs
Thanks!
I am quite confused with the callbacks. Should i always call my different file modules in the callbacks?
Or can i create the same callbacks in the modules as well without them impacting anything?
For example, if i have a file dedicated to showing the current fps. can i use love.update and love.draw in the fps.lua file as well (as in main.lua) or are the callbacks supposed to be used only in main.lua and have to call the fps module from inside main.lua?
Re: [Beginner] Organizing projects in smaller files
Posted: Tue Sep 29, 2015 2:31 pm
by MadByte
Here is a small example:
For example, if i have a file dedicated to showing the current fps. can i use love.update and love.draw in the fps.lua file as well (as in main.lua) or are the callbacks supposed to be used only in main.lua and have to call the fps module from inside main.lua?
You could use love callbacks in other files but generally this doesn't make sense because the main.lua is the first file which get processed by the interpreter. Also you CANT use i.e love.update multiply times, because the old calls would get overwritten and the program doesnt work anymore. For your "fps module" you could use the technic shown in the file above (the player table).
for example like this:
Code: Select all
local fps = {}
function fps:draw()
love.graphics.print("FPS: "..love.timer.getFPS(), 10, 10)
end
return fps
then just call the fps:draw() function in the love.draw function.
Re: [Beginner] Organizing projects in smaller files
Posted: Tue Sep 29, 2015 2:50 pm
by Rishavs
followup question:
wouldnt that mean that my update function will end up being monstrously huge?
Are there any design patterns used to tackle this?
I personally like having small bits of code spread over multiple files.
Sorry, if these are very obvious questions.
Re: [Beginner] Organizing projects in smaller files
Posted: Tue Sep 29, 2015 3:12 pm
by MadByte
Rishavs wrote:followup question:
wouldnt that mean that my update function will end up being monstrously huge?
Are there any design patterns used to tackle this?
I personally like having small bits of code spread over multiple files.
Sorry, if these are very obvious questions.
It's totally up to you how much you like to put into other files and what kind of "systems" you'd like to use to reduce the code for each function. Personally I like to use things like gamestates, object oriented programming and some sort of entity handlers. Here is another small example (I love examples yumy
). Its a basic template for platformers I made recently for someone:
It uses gamestates, a very basic entity handler and stuff like that. It should show some ways to avoid spaghetti code.
Re: [Beginner] Organizing projects in smaller files
Posted: Tue Sep 29, 2015 7:28 pm
by Rishavs
thanks!
Re: [Beginner] Organizing projects in smaller files
Posted: Tue Sep 29, 2015 7:49 pm
by airstruck
You might be interested in this
event module. You can set it up something like this:
Code: Select all
-- main.lua
local Event = require 'knife.event'
function love.load ()
Event.hook(love.handlers)
Event.hook(love, { 'update', 'draw', 'errhand' })
end
This intercepts all Love callbacks and handlers and causes them to dispatch events. Now you can handle those events in other files:
Code: Select all
-- somefile.lua
local timeRemaining = 30
Event.on('update', function (dt)
timeRemaining = timeRemaining - dt
end)
-- anotherfile.lua
local timeElapsed = 0
Event.on('update', function (dt)
timeElapsed = timeElapsed + dt
end)
The code is
here.
Re: [Beginner] Organizing projects in smaller files
Posted: Wed Sep 30, 2015 7:41 am
by Rishavs
Thanks. More code to read through is always welcome.
Re: [Beginner] Organizing projects in smaller files
Posted: Tue Oct 06, 2015 2:23 pm
by EulersIdentity
MadByte wrote:
...
It uses gamestates, a very basic entity handler and stuff like that. It should show some ways to avoid spaghetti code.
This is a great example. Does anyone have links to other examples or game sources that demonstrate good organization? This is an area that I really struggle with.