How do I make A sprite follow the player?

I wanted to make a sidekick sort of sprite that would follow you around. I don't know if this is even possible with Pulp but, if you have any suggestions about how to do this, that would be great!

thank you!

After some experimenting I’ve got a working implementation. The basic idea is to save the players last position and move the sidekick there in the next update.

One special case must be considered: When entering a new room, the sidekick isn’t there but has to be spawned first, after the player moved.

So here’s my implementation:

Into the game’s script, write this:

on enter do
	plX = event.px
	plY = event.py
	justEntered = "true"
end

Into the player‘s script, write this

on update do
	if justEntered=="false" then
		tell skX,skY to
			call "followAround"
		end
	else
		tell plX,plY to
			underSidekick = name plX,plY
			swap "sidekick"
		end
		skX = plX
		skY = plY
		justEntered = "false"
	end
	
	plX = event.px
	plY = event.py
end

And finally, write this into a sprite named „sidekick“:

on followAround do
	newUnder = name plX,plY
	tell plX,plY to
		swap "sidekick"
	end
	swap "{underSidekick}"
	underSidekick = newUnder
	
	skX = plX
	skY = plY
end

So basically, on update I first check, if the special case justEntered is active. This is set alongside with the new player position plX, plY in the game’s script upon entering into a new room.

If justEntered is true, there is no sidekick to move yet, so we have to spawn one. So, targeting where the player was standing before he moved, we save the current tile, swap in the sidekick, update the position of the sidekick (skX,skY) and set the justEntered-flag to false.

Now, if you didn’t enter a room just now, we tell the sidekick, which is located at skX, skY to call it’s function followAround.

Going into the followAround event, we first save the tile, where the player just stood and where the sidekick is about to move to. This is because we will swap it out, but want to swap it back in once the sidekick moved away.

We then move the sidekick by telling plX, plY to swap in our sidekick. At the old position (we exit the tell-function) we swap in the tile, that was under the sidekick and update the variable underSidekick to the tile the sidekick is now covering. Finally, we update the position of the sidekick.

When all of this is done, there’s one last and fundamental thing to do: update plX and plY in the update loop. We do this because in the next update, event.px and event.py will already be set to the players new position, which isn’t of any real value to us. We therefore store the players position now and when the update loop is called again (because the player moved) we know where the player WAS standing.

That’s it. Hope I could help :slight_smile:

7 Likes

hi there!
I just found this thread, I hope it still works.
I was wondering if you could help me. I'm trying to make a dog follow my character around the screen in my game. i used the code you wrote however, when playing, if I backtrack over the dog it disappears. It also disappears when I interact with a sprite by bumping into it.
I think I need to only tell the dog to move when the player sprite changes co ordinates but I'm not sure how to do this?
Any help would be greatly appreciated. This is my first time programming.
Many thanks.

i get the same result. it works but then it disappears!