War Battles Tutorial rockets lifetime became nil

Hi, I don’t understand what is happening, please help.

I follow the tutorial, the lifetime for the rockets were working fine, but when add the collision with the tanks and adding the function explode(self), I got a error that is:

ERROR:SCRIPT: main/rocket.script:9: attempt to index local ‘self’ (a nil value)
stack traceback:
main/rocket.script:9: in function explode
main/rocket.script:22: in function <main/rocket.script:15>

then, the rockets simply fly non-stop at infinitum.

My code:

go.property("dir", vmath.vector3())

function init(self)
	self.speed = 200
	self.life = 1
end

local function explode(self)
	self.life = 1000
	go.set_rotation(vmath.quat())
	self.speed = 0
	sprite.play_flipbook("#sprite", "explosion")
end

function update(self, dt)
	local pos = go.get_position()
	pos = pos + self.dir * self.speed * dt
	go.set_position(pos)

	self.life = self.life - dt
	if self.life < 0 then
		explode()
	end
end

function on_message(self, message_id, message, sender) 
	if message_id == hash("animation_done") then
		go.delete()
	elseif message_id == hash("collision_response") then
		explode(self)
		go.delete(message.other_id)
		msg.post("/gui#ui", "add_score", {score = 100})
	end
end
	if self.life < 0 then
		explode()
	end

You need to pass self into the explode function:

	if self.life < 0 then
		explode(self)
	end
3 Likes