Problem with resetting level (SOLVED)

Hello!

How do I make my snake tutorial game to reset?

I’ve made the grow food on the right places step, made a button and a reset function which is the same as the init function but the problem is that it gets the same as where it ended. I dunno what function/code to use here.

local function put_food(self)
	self.food = {x = math.random(2, 47), y  = math.random(2, 47)}
	local tile = tilemap.get_tile("#grid", "layer1", self.food.x, self.food.y)
	if tile == 2 or tile == 4 then
		put_food(self)
	else
		tilemap.set_tile("#grid", "layer1", self.food.x, self.food.y, 3)
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("/button#button", "disable")
	
	self.segments = {
		{x = 7, y = 24},
		{x = 8, y = 24},
		{x = 9, y = 24},
		{x = 10, y = 24} }
	self.dir = {x = 1, y = 0}
	self.dirqueue = {}
	self.speed = 7.0
	self.alive = true
	self.t = 0

	math.randomseed(socket.gettime())
	put_food(self)
end

function reset(self)
	msg.post("/button#button", "disable")

	self.segments = {
		{x = 7, y = 24},
		{x = 8, y = 24},
		{x = 9, y = 24},
		{x = 10, y = 24} }
		self.dir = {x = 1, y = 0}
		self.dirqueue = {}
		self.speed = 7.0
		self.alive = true
		self.t = 0

		math.randomseed(socket.gettime())
		put_food(self)
end

function update(self, dt)
	self.t = self.t + dt
	if self.t >= 1.0 / self.speed and self.alive then
		local newdir = table.remove(self.dirqueue, 1)
		if newdir then
			local opposite = newdir.x == -self.dir.x or newdir.y == -self.dir.y
			if not opposite then
				self.dir = newdir
			end
		end
		
		local head = self.segments[#self.segments]
		local newhead = {x = head.x + self.dir.x, y = head.y + self.dir.y}

		table.insert(self.segments, newhead)

		local tile = tilemap.get_tile("#grid", "layer1", newhead.x, newhead.y)

		if tile == 2 or tile == 4 then
			self.alive = false
			msg.post("/button#button", "enable")
		elseif tile == 3 then
			self.speed = self.speed + 1
			put_food(self)
			msg.post("/gui#ui", "add_score", {score = 10})
			
		else
			local tail = table.remove(self.segments, 1)
			tilemap.set_tile("#grid", "layer1", tail.x, tail.y, 0)
		end
		
		for i, s in ipairs(self.segments) do
			tilemap.set_tile("#grid", "layer1", s.x, s.y, 2)

		end

		self.t = 0
	end
end

function on_input(self, action_id, action)
	if action_id == hash("up") and action.pressed then
		table.insert(self.dirqueue, {x = 0, y = 1})
	elseif action_id == hash ("down") and action.pressed then
		table.insert(self.dirqueue, {x = 0, y = -1})
	elseif action_id == hash ("left") and action.pressed then
		table.insert(self.dirqueue, {x = -1, y = 0})
	elseif action_id == hash ("right") and action.pressed then
		table.insert(self.dirqueue, {x = 1, y = 0})
	end
end

function on_message(self, message_id, message, sender)
	if message_id == hash("reset") then
		
		reset(self)
	end
end

The problem is that you are modifying the tilemap of your game while it is being played (when the snake grows and when food is spawned). Your reset function doesn’t reset the tilemap.

I’d actually recommend that you reboot the engine:

msg.post("@system:", "reboot")

This essentially means that the entire game will restart. No state is saved and the init() function will be run again. Once you have a more complex game with a menu and so on it is likely that the game is loaded via a collection proxy and at that time it’s better to reload that proxy instead of the entire engine.

5 Likes

Been looking for that particular thing all day yesterday, thanks a lot!

Problem is, I can look around lua programming guide but I don’t know what should I be looking for since there are tons of groups and stuff that does stuff. But to be honest these defold tutorials are kinda easy to interpret and with a little dedication we can go step by step, I’ve been using code from each tutorial game to another to complete the extra steps.

1 Like