Camera component programmed perfectly, still doesn't follow the player

I seem to have stumbled across a really strange error. I have copied the example camera module from the examples page to track the player, and have followed the instructions perfectly, and checked for typos, but I cant seem to get it to follow the player.

function init(self)
	msg.post("#camera", "acquire_camera_focus")
end

function on_message(self, message_id, message, sender)
	if message_id == hash("follow") then
		go.set_parent(".", sender)
		go.set_position(vmath.vector3(-360, -360, 0))
	elseif message_id == hash("unfollow") then
		go.set_parent("camera", nil, true)
	end
end
function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("camera", "follow")
	self.follow = true
	
	self.moving = false
	self.firing = false
	self.input = vmath.vector3()
	self.dir = vmath.vector3(0, 1, 0)
	self.speed = 150
	self.damage = 1
end

function final(self)
	msg.post(".", "release_input_focus")
end

function update(self, dt)
	if self.follow == true then
		msg.post("camera", "follow")
	end

image

this is a really weird error, tbh it could be some logic program elsewhere in my code, but to save debugging time I wanted to post it here in case I missed something.

This tells me that you have a camera component with id “camera” on the game object this script belongs to.

But here you post a message to a game object with id “camera”, not specifically to the camera component above.

Does the game object with the camera have id “camera”?

image

yes

image

double checked, definitely right

Are you sure the message is sent?

Add a print before sending the message:

print("sending follow message")` 
msg.post("camera", "follow")

Also add a print in on_message() of the camera script:

function on_message(self, message_id, message, sender)
    print("Received message", message_id)
	if message_id == hash("follow") then

Do you see the message?

I think you could solve this much faster yourself by adding prints such as I suggested or by adding a breakpoint or two in your code and use the integrated debugger in the editor:

:slight_smile:

thanks, i’ll try the debugging later on, tbh i kind of forgot about debug scripts somehow