Multi file programming

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
zugende
Prole
Posts: 12
Joined: Sun Feb 21, 2016 5:47 pm

Multi file programming

Post by zugende »

Hi guys,

first post, so hello everybody :)

I am still in the very beginning of game programming so I wondered if there is something like a best practice when working with mutiple files.

I have these files:

world.lua
player.lua
hud.lua
enemies.lua
map.lua // this is a table with the map grid.

What is the best way, so that all the files can access the map.lua file?
For better understanding, this is the view I am currently working on:

Image

Thanks a lot guys!
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Multi file programming

Post by Davidobot »

Require it in the top of main.lua?

Code: Select all

map = require("map.lua")
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
zugende
Prole
Posts: 12
Joined: Sun Feb 21, 2016 5:47 pm

Re: Multi file programming

Post by zugende »

So when I include the map.lua in the main.lua via require - is it then best practice when I pass the map table every frame to the player.lua file, so that i also have an updated table on that side?

Simple example:

Code: Select all

local player = require("player")
local map = require("map")

function love.load(arg)
  player.set(100,100)
end

function love.update(dt)
end

function love.draw()
  
  player.draw(map)
  
end

User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Multi file programming

Post by Beelz »

I would advise against it. Having to pass a large table every frame creates a bit of overhead... You could serialize it into a 1d array and pass that through.

Also, for the most part, you're 'player.lua' should basically have no knowledge of 'map.lua'(and vise-versa). It is good practice to try to keep everything on its own task, so to speak.

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Multi file programming

Post by bobbyjones »

Beelz wrote:I would advise against it. Having to pass a large table every frame creates a bit of overhead... You could serialize it into a 1d array and pass that through.

Also, for the most part, you're 'player.lua' should basically have no knowledge of 'map.lua'(and vise-versa). It is good practice to try to keep everything on its own task, so to speak.
Passing a table should have no overhead. It's just a reference, equivalent to a pointer in c++.
zugende wrote:So when I include the map.lua in the main.lua via require - is it then best practice when I pass the map table every frame to the player.lua file, so that i also have an updated table on that side?
You could send the table over yes. That's what I do kinda in my game, but instead of map I pass the collision engine. In terms of performance there is no issue doing it that way. The only thing I can think of that would be an issue is the amount of code you would have to change if you change the data structure of your map. But it's fine for now.
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Multi file programming

Post by zorg »

When you load it, you can modify its contents directly; you should only save the file when you exit the game or the player wants to save the map modifications or in similar scenarios. Also, why would you pass it to player.lua if you already included it in main? Include player in main, and let it access your map.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Multi file programming

Post by bobbyjones »

zorg wrote:When you load it, you can modify its contents directly; you should only save the file when you exit the game or the player wants to save the map modifications or in similar scenarios. Also, why would you pass it to player.lua if you already included it in main? Include player in main, and let it access your map.
Well if you don't use globals then the player can't just access it.
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Multi file programming

Post by zorg »

bobbyjones wrote:
zorg wrote:When you load it, you can modify its contents directly; you should only save the file when you exit the game or the player wants to save the map modifications or in similar scenarios. Also, why would you pass it to player.lua if you already included it in main? Include player in main, and let it access your map.
Well if you don't use globals then the player can't just access it.
False, you can either use locals in main, require the files there, and pass each module stuff that they need, or alternatively, when requiring, pass in more parameters; from the second onward will fill the ... varargs that's accessible at the top of the loaded files... at least, if i remember correctly. If not, this might only work with love.filesystem.load.
The only mandatory globals you need are love.update and love.run; optionally love.load and any other callbacks. You needn't even them, if you define love.run in main, since then, only that needs to be global, and everything else can be local.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Multi file programming

Post by bobbyjones »

zorg that is what I am saying he should pass the map to the player. You were suggesting that the player could access it with no passing.
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Multi file programming

Post by zorg »

bobbyjones wrote:zorg that is what I am saying he should pass the map to the player. You were suggesting that the player could access it with no passing.
Schemantic difference between passing one module to another via require/load and passing through some function exported from player; in any case, it's possible however we want to call it :3 Even if i did overcomplicate things, which wasn't my intention!
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest