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!
Multi file programming
Re: Multi file programming
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
personal page and a raycaster
Re: Multi file programming
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:
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
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.
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.
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Multi file programming
Passing a table should have no overhead. It's just a reference, equivalent to a pointer in c++.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.
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.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?
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Multi file programming
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 True 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.
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Multi file programming
Well if you don't use globals then the player can't just access it.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.
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Multi file programming
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.bobbyjones wrote:Well if you don't use globals then the player can't just access it.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.
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 True 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.
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: Multi file programming
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.
- zorg
- Party member
- Posts: 3470
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Multi file programming
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!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.
Me and my stuff True 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.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests