What table is "self" here?

I’m just starting with Defold, and have been going through the tutorials.

During the spaceship “movement tutorial” I was looking at the spaceship object printing text when you click or use the arrows. I decided to add what the object was that was writing those print statements, thought I could use “self.id” to return the id path (/spaceship). This doesn’t appear to work, however go.get_id does. I’m not super familiar with Lua, but I thought when a function has “self” in it’s arguments it must be sending a table to the function which would contain some information about the game object. This doesn’t appear to be true, so I am wondering what table is being refered to by “self” in the init, update and on_input functions?

function init(self)
 	msg.post(".", “acquire_input_focus”)
end

function update(self, dt)
end

function on_input(self, action_id, action)
 	if action_id == hash(“up”) then
 	 	local p = go.get_position()
 	 	p.y = p.y + 1
 	 	go.set_position§
 	 	print(“UP!”)
 	elseif action_id == hash(“down”) then
 	 	local p = go.get_position()
 	 	p.y = p.y - 1
 	 	go.set_position§
 	 	print(“DOWN!”)
 	elseif action_id == hash(“left”) then
 	 	local p = go.get_position()
 	 	p.x = p.x - 1
 	 	go.set_position§
 	 	print(“LEFT!”)
  elseif action_id == hash(“right”) then
 	 	local p = go.get_position()
 	 	p.x = p.x + 1
 	 	go.set_position§
 	 	print(“RIGHT!”)
  elseif action_id == hash(“click”) and action.pressed then
 	 	print(“CLICK!”)
 	end
end

self is the script component instance of game object. It doesn’t have id field, so you have to use go.get_id() or define id field inside the init() function.

function init(self)
  self.id = go.get_id()
end
4 Likes

Thank you, Sergey.lerg for your help.

You can read more about self and the lifecycle functions here: Writing game logic in scripts

1 Like