Create Your First Godot 4 Game

Lesson 4: Creating the player

Video Notes

Now things are really going to get interesting! In this lesson, you’ll start creating your player character. You’ll also start learning GDScript, Godot’s custom scripting language.

You can learn the GDScript basics on the Godot website.

Corrections

In Godot 4 Beta 16 or higher, the AnimatedSprite2D’s Playing checkbox has been removed from the Inspector. It has been replaced by the Autoplay button, which can be found in SpriteFrames panel next to the trashcan icon.

In newer versions of Godot, the loop toggle has been moved to a button with two circular arrows next to the FPS label.

Some versions of Godot 4 don’t have the “Listen for Input” option when setting up keybindings. In that case, just use the manual option.

Notes
  • Use the “Select/Clear All Frames” button as quick way to select all the frames in a sprite sheet
  • If your Player is not showing up in the World scene, make sure your Player scene is saved first
  • When getting a reference to the AnimatedSprite2D and the Camera2D, pay attention to the case of the names in the scene. Yours could be different than the ones shown in the video.

If you get stuck, you can compare your code to ours here: https://github.com/quiver-dev/raptor-run. If you’re still stuck, leave a comment here.


  • acesyde
    Aug. 13, 2023 at 8:29 p.m.

    In the last part of this chapter, you should use the compositing pattern by exposing the camera variable like this.

    @export var camera: Camera2D
    

    After that in the main.tscn you must assign the camera2D to the camera variable of the Player.

    The player component must be not know where the camera is.

    • amit
      Aug. 14, 2023 at 3:14 p.m.

      Agreed, I think that’s a better way to do in it hindsight. Thanks for pointing that out. This might be a good example to use in a future tutorial about cleaning up code and having a clean separation of concerns.


  • frostedcookies
    Sept. 4, 2023 at 7:29 a.m.

    I was under the impression that it was a bad idea to scale anything that had a collision shape in it. Is this no longer the case in GD4?

    • amit
      Sept. 5, 2023 at 9:25 p.m.

      That’s a good question. The documentation still recommends not scaling physics bodies, so the best solution is probably scaling the original assets and changing the extents of the collision shape manually. However, we didn’t notice any collision issues or physics instability during testing, so this solution might be good enough.

      Juan, the lead Godot dev, did mention that he wanted to support scaling collision shapes in 4.0, but I’m not sure if any work was done towards that in 4. We’ll have to do more research to see if this support has been added or is planned.


  • DevDad
    Dec. 2, 2023 at 9:36 p.m.

    In Godot 4.1, there is no “Loop” slide button to disable the looping animation. It is now the two arrows pointing to each other, just to the left of the number of frames per second.

    • amit
      Dec. 4, 2023 at 3:51 p.m.

      Nice find! I added a video overlay and a correction in the video description indicating this change in newer versions of Godot.


  • DevDad
    Dec. 2, 2023 at 9:57 p.m.

    Also, in order to get your raptor to run by default, you have to click the “A” inside of the arrow (next to the “Loop” icon) to set an animation as the default.

    • amit
      Dec. 4, 2023 at 3:53 p.m.

      Yes, that was a change made in the betas that was made after we recorded the video. We added a small text correction in the video and in the description for that. I hope that was clear.


  • DevDad
    Dec. 2, 2023 at 11 p.m.

    For the jumping code section, I recommend using the “was_jumping” variable to easily see if the current jump should be a double jump. Also, this allows you to directly code the pitch without requiring a variable.

    It should be noted that this approach of hard-coding the values assumes that the player will only have two jumps; or, if more jumps are available, then all but the first will be considered “double jumps”, no matter how many occur.

    	if was_jumping:
    		sprite.play("double jump")
    		jump_sound.set_pitch_scale(1.3)
    		jump_sound.play()
    	else:
    		sprite.play("jump")
    		jump_sound.set_pitch_scale(1.0)
    		jump_sound.play()
    			
    	jumps_remaining -= 1
    	was_jumping = true
    	velocity.y = jump_power
    
    • amit
      Dec. 4, 2023 at 3:58 p.m.

      I’m not sure I understand the improvement here. With Tom’s method, it’s easier to add more than double jumps and the pitch code will work automatically. I feel like hard-coding this gives you less flexibility, unless I’m missing something.


  • Charles
    Feb. 14, 2024 at 8:03 a.m.

    When you referred to the A button next to the trash can icon, on the left hand side of the sprite frames panel did you mean the auto play on load button?

    • amit
      Feb. 15, 2024 at 7:58 p.m.

      Yes, that’s correct!


  • hiraiv1
    April 3, 2024 at 6:39 p.m.

    Hi, I had issues with sound playing only on the second jump. I tried to look at the code in the Github but I couldn’t find the one for sound. I eventually had to add an extra line just to get it to play a sound twice but it wont increase in pitch. Could you please have a look for me and tell me where I went wrong? Thank you.

    			jump_sound.set_pitch_scale(jump_pitch)
    			jump_sound.play()
    			jump_pitch = 0.2
    

  • hiraiv1
    April 4, 2024 at 4:18 a.m.

    In the end I had to hard wire it to get it to work. Another student created the code if was_jumping: sprite.play(“double jump”) jump_sound.set_pitch_scale(1.3) jump_sound.play() else: sprite.play(“jump”) jump_sound.set_pitch_scale(1.0) jump_sound.play()

    this one worked. Not sure why when I enter the code in the video the jump sound only makes a sound when I hit the spacebar on the second jump. I tried everything from retyping each line of code 3 times and re running it after each line. It may be an issue with the newest version ?


  • kongming
    June 25, 2024 at 10:49 p.m.

    I am at about 7 minutes just after completing the player. The player doesn’t show up when you switch to the main world node. And there is a warning icon next to player saying it has no shape?

    Followed tutorial exact. Even redid a few times!

    • amit
      June 26, 2024 at 7:21 p.m.

      Can you send a screenshot or list of the nodes for the Player? Do you have a CollisionShape2D attached?

Want to get into the mix? Sign in or register to comment.

Next lesson