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
[Beginner] Organizing projects in smaller files
- TheOdyssey
- Citizen
- Posts: 52
- Joined: Mon Jul 13, 2015 4:29 pm
- Location: Turn Around...
Re: [Beginner] Organizing projects in smaller files
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
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?
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
Here is a small example:
for example like this:
then just call the fps:draw() function in the love.draw function.
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, 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?
for example like this:
Code: Select all
local fps = {}
function fps:draw()
love.graphics.print("FPS: "..love.timer.getFPS(), 10, 10)
end
return fps
Re: [Beginner] Organizing projects in smaller files
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.
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
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.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.
Re: [Beginner] Organizing projects in smaller files
You might be interested in this event module. You can set it up something like this:
This intercepts all Love callbacks and handlers and causes them to dispatch events. Now you can handle those events in other files:
The code is here.
Code: Select all
-- main.lua
local Event = require 'knife.event'
function love.load ()
Event.hook(love.handlers)
Event.hook(love, { 'update', 'draw', 'errhand' })
end
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)
Re: [Beginner] Organizing projects in smaller files
Thanks. More code to read through is always welcome.
-
- Prole
- Posts: 3
- Joined: Fri Aug 14, 2015 9:35 am
Re: [Beginner] Organizing projects in smaller files
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.MadByte wrote: ...
It uses gamestates, a very basic entity handler and stuff like that. It should show some ways to avoid spaghetti code.
Who is online
Users browsing this forum: No registered users and 1 guest