File Structure and 'require'

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
Shanulu
Prole
Posts: 21
Joined: Mon Jun 24, 2013 11:34 pm

File Structure and 'require'

Post by Shanulu »

Hello, me again! Sorry to bug you.

Background:
In an effort to organize and clean up my Pong project I've searched and researched (see what I did there?) about handling game states and am attempting to write my own way of doing so. I've created a game.lua which is my base class as well, it containts load(), draw(), and update(), and now a title.lua which, as you may have guessed, will handle all my title screen business by using its own version of the three functions. All my game functions are working, as I can see my console notifications, but it doesn't seem to want to run my title code.

Problem:
I found this thread: http://love2d.org/forums/viewtopic.php? ... tle+screen and it is right up my alley. Yet the OP doesn't show me how the instuctions.lua would look like.

So in relation to mine, I have main.lua which requires game.lua. And game.lua which requires title.lua. But title doesn't work, obviously, because it doesn't know game exists. If I require both, I get a loop error, duh! And if I reverse them, title isn't being called anyway cause I suspect game doesn't know it is there...it seems I have a catch-22.

So my relatively easy question to you is, What kind of wizardy do I need to use to make, at least my file system let alone my code, work with each other. Or should I just bite the bullet and learn/utilize a library like Pölygamy?

Code: Select all

----------------------------------------------------------------------------
-- project:			Pong Revised
-- file: 			main.lua
-- author:			Michael Groll
-- version:			0.8.0
-- gitHub:			https://github.com/Shanulu/pongRevised
--
-- description:     Love2D's base file, contains most of the initializing,
--					drawing, and updating of the Pong components. Also
--					contains some of the keyboard inputs as well as my
--					customSort function.
----------------------------------------------------------------------------
require 'ball' --contains our ball class and collision deteciton
require 'paddle' --contains our paddle class
require 'interface' --contains our screen and buttons
require 'blocks' --contains our blocks
require 'game'

function love.load()
	Game:load()
	-- variables
	deleted = { order = math.huge } --a table used to for comparing later
	height = love.graphics.getHeight() --commonly used
	width = love.graphics.getWidth() 
	gameState = "title"		--title, pregame, options, live, pause, endgame
	difficulty = 100  --ai max speed movement
	preGame = 5 	--timer for pregame
	--we call Paddle:load() in interface.lua, so we can set difficulty
end

function love.draw()
	Game:draw()
end

function love.update(dt)
	Game:update(dt)
end

Code: Select all

----------------------------------------------------------------------------
-- project:			Pong Revised
-- file: 			game.lua
-- author:			Michael Groll
-- gitHub:			https://github.com/Shanulu/pongRevised
--
-- description:     
----------------------------------------------------------------------------
require 'title'
game = {}

Game = {}
Game.__index = Game

function Game:new()
	local game = setmetatable({}, Game)
	
	game.isActive = false
	game.beenLoaded = false
	
	return game
end

function Game:load()
	for _, v in ipairs(game) do
		if v.beenLoaded == false then
			v:load()
			v.beenLoaded = true
		end	
	end
	print("game load")
end

function Game:draw()
	for _, v in ipairs(game) do
		if v.isActive == true then
			v:draw()
		end
	end
	print("game draw")
end
	
function Game:update(dt)
	for _, v in ipairs(game) do
		if v.isActive == true then
			v:update()
		end
	end
	print("game update")
end

function Game:activate(i) --send this an index like "title" or "live"
	for _, v in ipairs(game) do
		if _ == i then
			v.isActive = true
		else
			v.isActive = false
		end
	end
end

Code: Select all

----------------------------------------------------------------------------
-- project:			Pong Revised
-- file: 			title.lua
-- author:			Michael Groll
-- gitHub:			https://github.com/Shanulu/pongRevised
--
-- description:    
----------------------------------------------------------------------------
require 'button'
--@LOVE FORUM GOERS: 
--not sure if theres a better way to do this (reference title to its corresponding table entry, or if it even works
--my code doesn't run yet so its hard to test.
game["title"] = Game:new()
game["title"].isActive = true
title = game["title"]

function Game:load()   --not sure if this should be Game:load() or title:load or even game["title"].load()
	--This is copied and works
	print("title loading")
	-- Graphics, Images, Font
	love.graphics.setBackgroundColor(0, 0, 0) --Black
	titleFont = love.graphics.newFont("Fonts/Digital_tech.otf", 56)--draw our title screen
	love.graphics.setFont(titleFont)
	love.graphics.setColorMode("modulate") --be sure to change it to replace when drawing buttons
	Button:load() --load the  buttons
	-- Sound and Music
	music = love.audio.newSource("Sounds/bgm.ogg")
	music:setLooping(true)
	BGM = true   --options setting
	sound = true --options setting
	love.audio.play(music)
	Button:load()
end

function Game:draw()
	love.graphics.print("Pong 2.0", width/2 - 50, 20)
	Button:draw()
end
Last edited by Shanulu on Wed Jul 24, 2013 7:58 pm, edited 5 times in total.
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: File Structure and 'require'

Post by baconhawka7x »

I just read this over in a few seconds so my answer may not even be relavent. But couldn't you simply require both "game" and "title" from main.lua?
User avatar
CaptainMaelstrom
Party member
Posts: 161
Joined: Sat Jan 05, 2013 10:38 pm

Re: File Structure and 'require'

Post by CaptainMaelstrom »

I might be able to help: I've developed a very simple but effective state machine for my current project.

It's hard to help you without seeing the code though. You said game.lua is your "base class." I'm not sure what that means. The jist of how my state machine/states work:

My state machine is just a global table "states" and three functions in main.lua: pushState(state), popState(state), switchState(state)

And then I have 5 or 6 State classes: Title, Game, Options, PromptSave, PromptClose... so I can make instances of and push them onto my state stack. Every frame, I have code in love.update and love.draw that updates and draws the appropriate states.
Shanulu
Prole
Posts: 21
Joined: Mon Jun 24, 2013 11:34 pm

Re: File Structure and 'require'

Post by Shanulu »

I am adding my current game.lua and title.lua. I'd like to note, its barebones because I am just creating a system that works, then I'll take my working code and paste it in, Ive added some comments for you readers.

Anyway, requiring all the files in main does work, although there is some bugs now.

Also when pasting that in it seems silly to pass update() to another update() which then passes it to yet another personalized update(). I might just code my states in main.lua, but for now I the goal here was to create a working state system. Just me thinking out loud...

I already have a working state system that just uses if "live" do this or if "title" do this but like I mentioned, it seems messy, and I wanted to try to clean it up a bit before I finish this project.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests