Different solution to jump pad launching player

Hi, first of all, I love those courses and thank you for your work.

But I wanted to ask about one thing: at the end, you create a new function for the player scene that is responsible for character jumping. I always try to be a little ahead of the course to always think about what I am learning and I did it a little different: in the jump pad script, I just decrease the body.velocity.y by a variable force. It still can be easily exported and I wondered, if any of those two solutions is any better in terms of optimization or anything else. Of course, both solutions works perfectly fine.

For clarification, I paste the code below. There is also an additional fragment to set the player on the set hight to make sure, that he jumps always to the same height, because from my tests there was sometimes a difference in the player height after jumping on jump pads.

func _on_body_entered(body):
	if body is Player:
		animated_sprite.play("jump")
		body.global_position.y = global_position.y - 10
		body.velocity.y = -jump_force
1 Like

That’s the idea!

This will work just fine at this stage and there’s not much of a difference from a performance standpoint because you’re doing the same thing; however, I do actually prefer Kaan’s solution in this case. The reason for that is because with your current implementation, if you add anything else into the jump() function later on, it would have to be duplicated here - “WET” (We Enjoy Typing) code. You’re more or less already doing that as it is, it’s just that it doesn’t matter in this case because it’s a one-line function.

As a project becomes more complex, there is also the possibility that as you change and update functionality like this, that you overlook one of the lines where it’s used. Because there are multiple situations in which a player can “jump” (with Jump input and with the jump pad), it’s better future-proofing to use a function to handle whatever will be common between those game events.

Don’t be discouraged by this though; there will be many more opportunities for innovation, and if you’re already thinking this way, that’s a great sign =)

Thank you so much for the response ^^

I am never discouraged by making something imperfect, that’s a part of learning, and I am greatful for explanation, why method used in the course is better long term than my solution.

Have a nice day!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms