X Tutup
Skip to content

Managing Scenes

ATS edited this page Mar 20, 2022 · 9 revisions

Hi! Today I'll be teaching you how to create a scene management system =).

This system is responsible for handling scenes such as the Menu, Options and even the Gameplay itself.

Time to work!

I will open my TIC-80 and create a new file

-- name: Scene Manager
-- author: (you)
-- desc:   short description
-- script: lua

t = 0

function TIC()
	t = t + 1
end

Our objective here is to create a function capable of showing us the scene we want, in a motorized way.

So I'll start by creating a function called sceneManager it will have the same schema as the common entity management functions, see:

function sceneManager()
	local s = {}

	return s
end

This function will return a table, the reason will be explained later

Time to add some essential variables

function sceneManager()
	local s = {}
	s.scenes = {}
	s.current_scenes = ""

	return s
end

Three variables of the table were created, s.scenes will be responsible for storing our scenes, s.current_scenes is responsible for telling us which index the table is in, that is, which current scene is being performed.

Now we can with the declared variables we can proceed, now I will create some functions:

-- in sceneManager function
function s:add(scene, name)
	s.scenes[name] = scene
end

function s:active(name)
	s.current_scene = name
	s.scenes[s.current_scene]:onActive() -- optional
end

function s:update()
	s.scenes[s.current_scene]:update()
end

function s:draw()
	s.scenes[s.current_scene]:draw()
end

I know, it's too much function! But don't be scared, I'll explain one by one, it's easy, I swear.

Let's start with the function s:add(scene, name), as the name says it will add the scenes, its parameters are simple scene is for us to put a function (function? In a parameter? What are you talking about? Calm down little grasshopper, I'll get there soon), name is for us to choose the name of the scene, very important because it will be with this name that we will be able to access it in the table scenes.

function s:add(scene, name)
	s.scenes[name] = scene
end

It's the turn of the s:active(name) function, it "activates" the scene, its parameter tells us which scene it will activate (using the name we decided in the second parameter of the function s:add )

(this is optional) as soon as this function is called it also calls a function, this function is not in sceneManager, but later on you will understand.

function s:active(name)
	s.current_scene = name
	s.scenes[s.current_scene]:onActive() -- optional
end

And finally, s:update() and s:draw(), there is no secret, they are just "drawing" and "updating" the scenes present in the table.

function s:update()
	s.scenes[s.current_scene]:update()
end

function s:draw()
	s.scenes[s.current_scene]:draw()
end

I know this tutorial is long, but please continue because it will be worth it, what seems difficult is actually quite easy, you just need to try and practice =).

Remember, if you're learning, you're not making mistakes. ;)

Okay, we finished our function, now it's time to test and also teach how to use the function.

I declared a variable called mgr (Manager) that receives the same table present in sceneManager. Then we must create the scene, create a function, and do the same as we did in sceneManager but adding only these functions:

mgr = sceneManager()

function Hello()
	local s = {}
	function s:onActive() -- optional

	end

	function s:update()

	end

	function s:draw()
		cls()
		print("Hello World!", 0, 0, 12)
	end
	return s
end

After that, let's add this scene: :

mgr:add(Hello(), "hello")

then we activate it. And for TIC to see and update our scenes, let's call two more functions that came with us in the sceneManager function

mgr:active("hello")

function TIC()
	mgr:update()
	mgr:draw()
	t = t + 1
end

And look:

screen2

But WAIT! Still want to know what that optional function does? It is only called when the scene is activated, very useful if you want to create a cutscene system. Look:

If I add something to this function...

-- in Hello function
function s:onActive()
	trace("Thankyou for read =)", 9)
end

then:

video2

As soon as the scene was activated (basically when I ran the game) it called us this function, as a consequence printing it in our terminal.

All code:

-- name: Scene Manager
-- author: (you)
-- desc:   short description
-- script: lua

t = 0

function sceneManager()
	local s = {}
	s.scenes = {}
	s.current_scenes = ""
	s.overlayer = {}

	function s:add(scene, name)
		s.scenes[name] = scene
	end

	function s:active(name)
		s.current_scene = name
		s.scenes[s.current_scene]:onActive()
	end

	function s:update()
		s.scenes[s.current_scene]:update()
	end

	function s:draw()
		s.scenes[s.current_scene]:draw()
	end

	return s
end

mgr = sceneManager()

function Hello()
	local s = {}
	function s:onActive()
		trace("Thankyou for read =)", 9)
	end

	function s:update()

	end

	function s:draw()
		cls()
		print("Hello World!", 0, 0, 12)
	end
	return s
end

mgr:add(Hello(), "hello")
mgr:active("hello")

function TIC()
	mgr:update()
	mgr:draw()
	t = t + 1
end

Thanks for reading this tutorial, I hope I understood and managed to implement this system in your game =).

X Tutup