Several Game files

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Ragusa
Prole
Posts: 2
Joined: Mon Dec 21, 2015 8:31 pm

Several Game files

Post 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
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Several Game files

Post 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
NigelNoscopes
Prole
Posts: 6
Joined: Sat Mar 28, 2015 8:22 pm

Re: Several Game files

Post 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.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Several Game files

Post 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))
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.
Ragusa
Prole
Posts: 2
Joined: Mon Dec 21, 2015 8:31 pm

Re: Several Game files

Post 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 :)
User avatar
Calandriel
Prole
Posts: 39
Joined: Wed Apr 22, 2015 9:00 am

Re: Several Game files

Post 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
If violence is not solving all your problems, You're simply not using enough of it.
Twitter: https://twitter.com/Calandriell Some news about wtf I'm doing.
Tumblr:https://www.tumblr.com/blog/calandriell I use to draw some random stuff.
User avatar
giantofbabil
Prole
Posts: 32
Joined: Tue Dec 15, 2015 6:07 pm

Re: Several Game files

Post 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?

Code: Select all

if enemy == lowerClassSaiyan and powerLevel > 9000 then
    love.graphics.print("What?! 9000?! There's no way that could be right!", 10, 200)
else
    love.graphics.print("You fool!", 10, 200)
end
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Several Game files

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
giantofbabil
Prole
Posts: 32
Joined: Tue Dec 15, 2015 6:07 pm

Re: Several Game files

Post by giantofbabil »

@S0lll0s

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

Code: Select all

if enemy == lowerClassSaiyan and powerLevel > 9000 then
    love.graphics.print("What?! 9000?! There's no way that could be right!", 10, 200)
else
    love.graphics.print("You fool!", 10, 200)
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 56 guests