Skip to content

Summary of Contents of Player.gd Script

James Russell edited this page Apr 9, 2018 · 4 revisions

This section will detail, in order from top of the script to the bottom, what each thing does.

Global variables, functions, unhandled input, “_ready()”, and the main loop will be explained in further detail in their own chapter for easier consumption.

Global Variables and Constants

The top of the file, before the functions, contains notes, global variables, and constants, some of which are settings. The ones that are not don’t need to be edited, as they are just keep variables needed for functioning, such as “SlideCount” inside the “SETTINGS > MOVEMENT” section, which just holds is result of “get_slide_count()”.

For more detail check the “(Global Variables In-Depth)[https://github.com/leiget/Godot_FPC_Base/wiki/Global-Variables-In-Depth]” chapter.

  • Explanation of script and reference to this manual and the FPC GitHub project page.
  • Notes on how to use the script correctly.
  • The “extends” that extends the current script’s functionality.
  • Settings for things like the player’s movement speed, mouse look toggling, etc.
  • Signals for debugging.
  • Variables that hold pointers to nodes.
  • Variables that hold states, such as if the player is on a floor and what not.
  • Global variables that don’t really have any other place to be.
  • Input bools and the strings that identify them by their action names set in the project’s settings.
  • Movement variables like the final walk velocity.
  • Rotation variables like the relative movement of the cursor.
  • Variables pertaining to the character’s falling, such as the bool which states if the player is falling or not.
  • Variables pertaining to jumping, such as the jump velocity.
  • Variables pertaining to the speed of the character when moving on a slope, such as the slopes normal before being normalized into a 2D vector.
  • Variables pertaining to stepping up platforms and steps, such as the distance the player must move up to get on the step.
  • Variables pertaining to the camera smooth movement when walking up stairs and steps, such as how much time has elapsed since the interpolation started.
  • Variables pertaining to ray casting, which is done several times through the script, such as the position that the ray cast should be shot from.
  • Variables pertaining to interaction via the “Use” button or touch, such as the array that holds all the touched objects that had a “touch” function in their attached script.

Functions

The custom functions for this script, to make things a little easier to understand and read. These will be explained further in-depth in the “(Functions Explained In-Depth)[https://github.com/leiget/Godot_FPC_Base/wiki/Functions-Explained-In-Depth]” section.

  • InterpolateCamera()
    • Interpolates the camera position when moving up a step or staircase for smooth camera movement going up them.
  • Step_Player_Up()
    • Checks to see if there is a step to be stepped upon, and moves the character up if there is.
  • Touch_CheckAndExecute()
    • Checks for any “touch” functions inside the currently colliding objects, and activates them if there are.
  • Slope_AffectSpeed()
    • Checks to see if the player is on a slope of some kind and affects speed accordingly.

Ready

Initializes several things. The general overview:

  • Starts the processes.
  • Prepares states.
  • Gets and sets the player’s position and the max height of the step, if too high.
  • Set’s some defaults.
  • Sets size of the “touched objects” array.

Unhandled Input

Takes care of camera and player rotation using the mouse. The general overview:

  • Checks if mouse look is enabled.
    • Check if input is a mouse motion.
      • Get the relative mouse motion in comparison to where it was the last frame.
      • Set limits to how high or low the player can look.
      • Apply the rotation.

Physics Process

Moves the character around; jumps, steps up, applies gravity, etc.

The code overview:

  • Check for input.
  • Set states according to input.
  • Check if the player is looking at something usable and react accordingly.
  • Calculate horizontal movement.
  • Calculate vertical movement according to if the player is:
    • On a floor.
    • Jumping.
    • Falling
    • On a slope.
  • Apply movement using “move_and_slide()”.
  • Get states once again.
  • Execute “Step_Player_Up()” function.
  • Check for camera interpolation and react accordingly.
  • Execute “Touch_CheckAndExecute()” function.
  • Set the debug label text.