Self variable not assigning first round through

I am trying to use the self variable self.CurrentHitboxPosition in order to clean up a tile once a entity moves. However I’ve ran into a weird issue I don’t know how to fix.

for some reason, self.CurrentHitboxPosition is only getting assigned on the second runthough of the function, instead of the first, leaving the tile it spawned in with not cleaned up. apart from that the code works perfectly.

I lighly abridged this function to remove the spawning, but that shouldn’t affect anything because I was only reading the value, not writing to it.

And this is where I define self.CurrentHitboxPosition but I tried it in Init, and as a go.property and still got the same issue

am I doing something wrong?

function EntityApi.SetCurrentHitboxPosition(self,Position)
	if self.CurrentHitboxPosition ~= nil then --not firing second round through
		tilemap.set_tile(
		GroundTiles.URL.COMBAT_SCREEN,
		GroundTiles.Layers.HITBOX_LAYER, 
		self.CurrentHitboxPosition.x,
		self.CurrentHitboxPosition.y, 
		GroundTiles.GROUND_TILE_TYPES.NULL.ID)
	end
	self.Xpos, self.Ypos = CustomMaths.WorldPositionToTilePosition(Position.x, Position.y,true)
	self.CurrentHitboxPosition = vmath.vector3(self.Xpos,self.Ypos,0)
end

Is it the same self both times? hard to say what’s wrong just from the snippet you shared.

It should be, I’ll just run though everything up to this point

The function is called from a script called UniversalEntityScript which Is applied for every single player/enemy/gimmick, to allow me to make universal changes to all entities

that runs the instruction

function init(self)
	EntityApi.SetCurrentHitboxPosition(self,go.get_world_position("."))
end

Which calls a Lua script with all my entity specific functions, which runs the code

function EntityApi.SetCurrentHitboxPosition(self,Position)
	if self.CurrentHitboxPosition ~= nil then --not firing second round through
		tilemap.set_tile(
		GroundTiles.URL.COMBAT_SCREEN,
		GroundTiles.Layers.HITBOX_LAYER, 
		self.CurrentHitboxPosition.x,
		self.CurrentHitboxPosition.y, 
		GroundTiles.GROUND_TILE_TYPES.NULL.ID)
	end
	self.Xpos, self.Ypos = CustomMaths.WorldPositionToTilePosition(Position.x, Position.y,true)
	self.CurrentHitboxPosition = vmath.vector3(self.Xpos,self.Ypos,0)
	local TileType = GroundTiles.GROUND_TILE_TYPES.HITBOX.ID
	if self.Id == 4 then
		TileType = GroundTiles.GROUND_TILE_TYPES.PLAYER_HITBOX.ID
	end
	tilemap.set_tile(
	GroundTiles.URL.COMBAT_SCREEN,
	GroundTiles.Layers.HITBOX_LAYER, 
	self.Xpos,
	self.Ypos, 
	TileType)
end

I tried to run this from the Player script init (Seen at top) and it worked, however even in an empty room with no other entities but the player; UniversalEntityScript ran into the same ‘not-loading-the-first time’ issue

I think I might be misunderstand how self works in scripts shared by multiple objects, or is there something I can do to fix this

Have you verified? What if you print(self)?

Wait is self script based and not object based?

How would I get around that?

it’s script instance based.