A Compact Scene Manager for TIC-80

September 3, 2024

alt text

In this post, I’ll show you how to write a compact scene manager for your TIC-80 games. Feel free to check out the code and let me know if you have any suggestions for further optimization!

You can play the TIC-80 cart online here: Play TIC-80 Cart

Code in Lua

-- scene manager
function sceneManager()
	return {
		scenes = {},
		curr = nil,
		register = function(self, s, n) self.scenes[n] = s end,
		switch = function(self, n)
			self.curr = n
			self.scenes[n]:init()
		end,
		init = function(self) self.scenes[self.curr]:init() end,
		update = function(self) self.scenes[self.curr]:update() end,
		draw = function(self) self.scenes[self.curr]:draw() end
	}
end

sMgr = sceneManager()

-- menu scene
function Menu()
	local s = {}

	function s:init()
		-- empty
	end

	function s:update()
		if btnp(4) then sMgr:switch("Game") end
	end

	function s:draw()
		cls(0)
		print("> Menu Scene ...", 0, 50, 4)
	end

	return s
end

-- game scene
function Game()
	local s = {}

	function s:init()
		-- empty
	end

	function s:update()
		if btnp(4) then sMgr:switch("Menu") end
	end

	function s:draw()
		cls(0)
		print("> Game Scene ...", 0, 50, 3)
	end

	return s
end

-- init
function init()
	sMgr:register(Menu(), "Menu")
	sMgr:register(Game(), "Game")
	sMgr:switch("Menu")
end

-- main
init()
function TIC()
	sMgr:update() -- update scene
	sMgr:draw() -- draw scene
end
Coding Techniques Game Design Game Development Game Engines Game Programming Indie Games Lua Retro Gaming Scene Manager TIC-80