[Beginner] Organizing projects in smaller files

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

[Beginner] Organizing projects in smaller files

Post 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
User avatar
TheOdyssey
Citizen
Posts: 52
Joined: Mon Jul 13, 2015 4:29 pm
Location: Turn Around...

Re: [Beginner] Organizing projects in smaller files

Post 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
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: [Beginner] Organizing projects in smaller files

Post 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?
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: [Beginner] Organizing projects in smaller files

Post by MadByte »

Here is a small example:
RequireFiles.love
(1.3 KiB) Downloaded 153 times
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.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: [Beginner] Organizing projects in smaller files

Post 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.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: [Beginner] Organizing projects in smaller files

Post 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 :D). Its a basic template for platformers I made recently for someone:
Platformer Basics Demo.love
(7.77 KiB) Downloaded 184 times
It uses gamestates, a very basic entity handler and stuff like that. It should show some ways to avoid spaghetti code.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: [Beginner] Organizing projects in smaller files

Post by Rishavs »

thanks!
:awesome:
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: [Beginner] Organizing projects in smaller files

Post 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.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: [Beginner] Organizing projects in smaller files

Post by Rishavs »

Thanks. More code to read through is always welcome. :D
EulersIdentity
Prole
Posts: 3
Joined: Fri Aug 14, 2015 9:35 am

Re: [Beginner] Organizing projects in smaller files

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests