Getting started tutorial - lives and game over

Been trying to complete the getting started tutorial for a couple of days now. I’ve been able to customize graphics and get everything else to work, but now i’m stuck. For some reason i’m not able to get the lives system and game over text to work at all.

I copied hud.gui_script from the youtube-video, but the mentor didn’t explain nothing but the point system. Is there anyone who could and has time to help me with this? Console doesn’t give any errors while testing the game.

The GUI in Defold is some of the best I’ve seen inplemented, here’s the maual page

but basically you need to get the gui node before you can do anything with the gui

so you would do;

local score_node = gui.get_node("insert your path to gui here, but probably just id of node")
-- then to access the score and update it
gui.set_text(score_node, score)

the update value is best sent via a message like

msg.post("url to gui script goes here", "update_score", {value = score})
1 Like

Thanks! I read that, but i’m still having issues.

My hud.gui_script is:

local text_nodes = {"score", "score_text", "life1", "life2", "life3", "life4", "game_over", }

function init(self)
	reset_lives(self)
end

function on_message(self, message_id, message, sender)
	if message_id == hash("set_score") then
		local s = gui.get_node("score")
		gui.set_text(s, message.score)
	
	elseif message_id == hash("show") then
		msg.post(".", "enable")
		show(self)
		
	elseif message_id == hash("hide") then
		hide(self, true)
		
	elseif message_id == hash("show_game_over") then
		show_game_over(self)
	
	elseif message_id == hash("set_lives") then
		set_lives(self, message.lives)
	end

end

function show(self)
	reset_lives(self)
	node = gui.get_node("game_over")
	gui.set_enabled(node, false)
	for i, id in pairs(text_nodes) do
		node = gui.get_node(id)
		local c = gui.get_color(node)
		gui.set_color(node, vmath.vector4(c.x, c.y, c.z, 0))
		c.w = 1.0
		gui.animate(node, gui.PROP_COLOR, c, gui.EASING_INOUTSINE, 0.75, 0.25, function() end)
	end
end

function hide(self, disable)
	for i, id in pairs(text_nodes) do
		node = gui.get_node(id)
		local c = gui.get_color(node)
		c.w = 0.0
		gui.animate(node, gui.PROP_COLOR, c, gui.EASING_INOUTSINE, 0.75, 0.25,
			function()
				if disable == true then
					msg.post(".", "disable")
				end
			end
		)
	end
end

function show_game_over(self)
	hide(false)
	node = gui.get_node("game_over")
	gui.set_color(node, vmath.vector4(1, 1, 1, 0))
	gui.set_enabled(node, true)
	gui.animate(node, gui.PROP_COLOR, vmath.vector4(1,1,1,1), gui.EASING_INOUTQUINT, 7.0, 0.0,
		function()
			gui.set_enabled(node, false)
		end
		,gui.PLAYBACK_ONCE_PINGPONG
	)
end
	
function set_lives(self, lives)
	node = gui.get_node("life" .. lives)
	gui.set_color(node, vmath.vector4(1, 1, 1, 1))
	gui.animate(node, gui.PROP_COLOR, vmath.vector4(1,1,1,0), gui.EASING_OUTCIRC, 1.0)
	gui.animate(node, gui.PROP_SCALE, vmath.vector3(2,2,2), gui.EASING_OUTCIRC, 1.0)
end

function reset_lives(self)
	for i = 1,4 do
		node = gui.get_node("life" .. i)
		gui.set_color(node, vmath.vector4(1, 1, 1, 1))
		gui.set_scale(node, vmath.vector3(1,1,1))
	end
end	

Do I need to do something else to make the hearts diseappear while colliding with spikes?

In the tutorial under Animation and Death, step 10 lists the code where you need to place the

msg.post("url to gui script here", "set_lives", {lives = lives_total}), --lives_total variable to keep track of number of lives

it should go after the part

elseif message_id == hash("contact_point_response") then
        -- check if we received a contact point message
        if message.group == hash("danger") then
            -- Die and restart

You’ll need to figure out how to keep track of lives_total but thats easy.