Page 1 of 2
Multi file programming
Posted: Sun Feb 21, 2016 6:09 pm
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:
Thanks a lot guys!
Re: Multi file programming
Posted: Sun Feb 21, 2016 8:24 pm
by Davidobot
Require it in the top of main.lua?
Re: Multi file programming
Posted: Sun Feb 21, 2016 9:48 pm
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
Re: Multi file programming
Posted: Mon Feb 22, 2016 3:20 am
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.
Re: Multi file programming
Posted: Mon Feb 22, 2016 4:15 am
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.
Re: Multi file programming
Posted: Mon Feb 22, 2016 4:16 am
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.
Re: Multi file programming
Posted: Mon Feb 22, 2016 4:29 am
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.
Re: Multi file programming
Posted: Mon Feb 22, 2016 7:19 am
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.
Re: Multi file programming
Posted: Mon Feb 22, 2016 1:43 pm
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.
Re: Multi file programming
Posted: Mon Feb 22, 2016 2:42 pm
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
Even if i did overcomplicate things, which wasn't my intention!