Page 1 of 1

Several Game files

Posted: Mon Dec 21, 2015 8:43 pm
by Ragusa
Hello!
I have been using love2d a bit now and I have taught it to myself by googling stuff.
But there is one thing I cannot find out how to do after several google sessions:

I want to load another lua file from the main.lua, so I can have a Menu file, a game file and such. How do I go about doing so?

Thanks a lot in advance

Re: Several Game files

Posted: Mon Dec 21, 2015 9:33 pm
by pgimeno
If I understand you correctly, what you want is gamestates. If you have difficulty implementing them, look at some library that deals with states. I can only recall Hump at this moment:

HUMP wiki page

Edit: there seem to be more; in particular this one is Lua module-oriented which seems to be what you're after:

Stateswitcher wiki page

There are at least a couple more listed here, LövelyMoon and Pölygamy:

https://love2d.org/wiki/Category:Libraries

Re: Several Game files

Posted: Tue Dec 22, 2015 12:08 am
by NigelNoscopes
I think OP was talking about require().

To organize your game files into different modules, you can just put a require('yourModuleName') at the top of your main.lua and it will carry over all your global variables/functions to main.lua as if it were all one file. When you require() a lua file, don't put the .lua extension at the end.

Re: Several Game files

Posted: Tue Dec 22, 2015 1:13 am
by zorg
NigelNoscopes wrote:I think OP was talking about require().

To organize your game files into different modules, you can just put a require('yourModuleName') at the top of your main.lua and it will carry over all your global variables/functions to main.lua as if it were all one file. When you require() a lua file, don't put the .lua extension at the end.
Except dumping everything into the global scope thatany way makes code less compartmentalized and manageable than requiring into a local variable, like how HUMP does it (and probably how the other ones do it as well)

Code: Select all

-- other.lua
local m = {}
m.a = 1
m.b = function(x) return x end
return m

-- main.lua
local h = require "other"
print(h.b(h.a))

Re: Several Game files

Posted: Tue Dec 22, 2015 1:04 pm
by Ragusa
NigelNoscopes wrote:I think OP was talking about require().

To organize your game files into different modules, you can just put a require('yourModuleName') at the top of your main.lua and it will carry over all your global variables/functions to main.lua as if it were all one file. When you require() a lua file, don't put the .lua extension at the end.
Yes, that's exactly what I was thinking.

Thanks a lot :)

Re: Several Game files

Posted: Sat Dec 26, 2015 4:39 am
by Calandriel
That's how I do this things:

Code: Select all

function love.load()
	State={}--This is my main state. It holds my lua states
	require('menu')
	State=menu--this time, when I start my aplication, it will load my menu
end
function love.update(dt)
	dt=love.timer.getDelta()
	checkState=State:isDead()--true if its Dead. If a state is "dead" then I'll get back to the menu
	pass=State:isPassed()--true if its Passed. If a state is "passed" then I'll be redirected to the next state
	name=State:getID()--true if... no! that's a String. it'll help me know inside which state I am and to what I'll go
	if checkState then
		if name~="menu" then
			require('menu')
			State=menu
			State:init()
		end--returning to menu if the state "die"
		if name=="menu" then
			require('stage1')
			State=stage1
			State:init()
		end--going to first state
	end
	if name=="stage1" then
		if pass then
			require('stage2')
			State=stage2
			State:init()
		end
	end--going to next state
end
	State:update(dt)--updating the current state
	collectgarbage()
end

function love.draw()
	State:draw()drawing whatever you need to draw
end

Re: Several Game files

Posted: Sat Dec 26, 2015 11:33 pm
by giantofbabil
I'm making a game right now where I am eventually going to have a metric #%$& ton of levels, but right now I just have a menu and tutorial. I am doing my gamestates currently by just using a variable where 0 = title screen, and 1 = tutorial right now and then it will move on to 2 = level 1 and so on. Is there anything wrong with this? Here's what my main.lua looks like:

Code: Select all

--set up for levels
local title   = require 'assets.levels.title'
local level01 = require 'assets.levels.level01'

--variable for adjusting physics values for movement speed and jumping
physicsMultiplier = 1000

--table for all player related stats
playerStats = {
                health       = 3,
                score        = 0,
                invul        = 0,
                weaponEquip  = 0}

--variable for level selection
levelSelector = 0

function love.load()
  if levelSelector == 1 then loadLevel_01() end
end



function love.update(dt)
  
  if levelSelector == 1 then
    updateLevel_01(dt)
  else
    updateTitle(dt)
  end

end

function love.draw(dt)
  
  if levelSelector == 1 then
  drawLevel_01()
  else
  drawTitle()
end

end
Right now I don't see an issue with doing it this way but maybe there's something I'm missing?

Re: Several Game files

Posted: Sun Dec 27, 2015 12:00 am
by s-ol
giantofbabil wrote:I'm making a game right now where I am eventually going to have a metric #%$& ton of levels, but right now I just have a menu and tutorial. I am doing my gamestates currently by just using a variable where 0 = title screen, and 1 = tutorial right now and then it will move on to 2 = level 1 and so on. Is there anything wrong with this? Here's what my main.lua looks like:


Right now I don't see an issue with doing it this way but maybe there's something I'm missing?
There's nothing really wrong or problematic with it, there's just ways that make it a lot easier and less prone to errors. As you write more levels, it get's longer and harder to maintain:

Code: Select all

if levelSelector == 1 then
...
else if levelSelector == 2 then
...
else if levelSelector == 3 then
...
else if levelSelector == 4 then
...
end
now imagine you remove level 2. You have to edit level3 and level 4 etc again, and you have to look in every place that handles all gamestates (love.draw, love.update, possibly love.keypressed etc.)

Instead you could simply do this:

Code: Select all

level01 = {}
function level01.draw() -- instead of level01_draw
...
end
function level01.update(dt)
...
end

level02 = {}
function level02.draw()
end
function level02.update(dt)
  if levelExit:touchesPlayer() then -- for example
    currentlevel = level03
  end
end


function love.load()
  currentlevel = level01
  currentlevel.load()
end

function love.draw()
  currentlevel.draw()
end

function love.update(dt)
  currentlevel.update(dt)
end
you can even cleanly seperate the files now, and you don't have to ever touch the "main" love callbacks again when working on levels:

main.lua

Code: Select all

level01 = require "level1"
level02 = require "level2"
level03 = require "level3"

local current_level = level01

function love.draw()
  current_level.draw()
end

function love.update(dt)
  current_level.update(dt)
end
level1.lua

Code: Select all

local level = {}
function level.update(dt)
 ...
end
function level.draw()
...
end

Re: Several Game files

Posted: Sun Dec 27, 2015 12:24 am
by giantofbabil
@S0lll0s

Not sure if I'll do that but I can definitely see the reasoning behind it, thank you!