Several Game files
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Several Game files
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
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
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
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
-
- Prole
- Posts: 6
- Joined: Sat Mar 28, 2015 8:22 pm
Re: Several Game files
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.
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.
- zorg
- Party member
- Posts: 3465
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Several Game files
Except dumping everything into the global scopeNigelNoscopes 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.
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 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.
Re: Several Game files
Yes, that's exactly what I was thinking.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.
Thanks a lot
- Calandriel
- Prole
- Posts: 39
- Joined: Wed Apr 22, 2015 9:00 am
Re: Several Game files
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.
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.
- giantofbabil
- Prole
- Posts: 32
- Joined: Tue Dec 15, 2015 6:07 pm
Re: Several Game files
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?
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
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
Re: Several Game files
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: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?
Code: Select all
if levelSelector == 1 then
...
else if levelSelector == 2 then
...
else if levelSelector == 3 then
...
else if levelSelector == 4 then
...
end
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
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
Code: Select all
local level = {}
function level.update(dt)
...
end
function level.draw()
...
end
- giantofbabil
- Prole
- Posts: 32
- Joined: Tue Dec 15, 2015 6:07 pm
Re: Several Game files
@S0lll0s
Not sure if I'll do that but I can definitely see the reasoning behind it, thank you!
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
Who is online
Users browsing this forum: No registered users and 5 guests