platformer help
platformer help
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
Re: platformer help
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()
Re: platformer help
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:
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
Who is online
Users browsing this forum: Ahrefs [Bot] and 5 guests