platformer help

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
bigboy97
Prole
Posts: 1
Joined: Mon May 22, 2023 3:42 pm

platformer help

Post by bigboy97 »

I have been writing my game and its only one file right now. so far its only a player controller and a camera. I want to put my player in a separate file from main.lua and have the files communicate everything they need to. the problem is i don't know how to do that. :(
Attachments
main.lua
(1.87 KiB) Downloaded 82 times
duaner
Prole
Posts: 41
Joined: Thu May 07, 2020 6:43 pm
Contact:

Re: platformer help

Post by duaner »

One way to do it would be to make one file a library and require it from the other file.

Code: Select all

-- file_one

local _M = {}

function _M:do_something()
    do_it()
end

return _M

Code: Select all

-- file_two

local file_one = require('file_one')

file_one:do_something()
User avatar
Azzla
Prole
Posts: 43
Joined: Sun Mar 29, 2020 2:23 am

Re: platformer help

Post by Azzla »

It's definitely good idea to keep components/libraries separated as duaner demonstrated above. You're thinking in the right direction by modularizing your codebase.

I would also humbly suggest some form of state-management, such as kikito's gamestate library. You will save yourself a lot of headache down the road by doing so. Every gamestate can be its own file via the same process, and only include the necessary components for that state. Brief example:

Code: Select all

--main.lua
Gamestate = require('libraries/hump/gamestate')
Camera = require('libraries/hump/camera')

game = require('states/game')

function love.load()
	Gamestate.registerEvents()
	Gamestate.switch(game)
end

--LOVE's callbacks are handled by your gamestate files
function love.update(dt) end
function love.draw() end

Code: Select all

--game.lua gamestate example
local Game = {}
local PlayerClass = require('classes/player')

function Game:init()
	self.player = PlayerClass(sprite, x, y, speed, health)
	self.cam = Camera(self.player.x, self.player.y)
end

function Game:update(dt)
	self.cam:update(dt)
	self.player:update(dt)
end

function Game:draw()
	self.cam:attach()
	
	--draw stuff
	self.player:draw()
	
	self.cam:detach()
end

return Game
libraries: Stalkpile
electronic music stuff: https://soundcloud.com/azzlamusic
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests