Gdscript - Tumblr Posts
These first few devlogs will be numbered with roman numerals as I have not yet started work on my actual game
Context and warning
This project is my graduation project for high school, I do have a partner for doing this, but they’re behind me on learning the engine. Imay publish the result or even keep working on it after my senior year, saying this, to people who would push for me to release my project earlier than my senior year, no. This is a school project and I will not be pushed, the school comes first.
Mario Clone
To learn Godot, my engine of choice, I have been working on multiple small projects, such as a Tarot reader, a mobile game on the Godot docs, and more recently, a Super Mario Bros. clone.
Today I will be going over:
The movement script
The bounding box and signals in Godot
My plans for this project
Movement Script
In the movement script, I may have been caught in some feature creep. The movement script has:
Side to Side movement
@export var speed = 300.0 @export var friction = 10 ... func _physic_process(delta): ... if Input.is_action_pressed("ui_right"): velocity.x += speed if Input.is_action_pressed("ui_left"): velocity.x -= speed else: velocity.x = move_toward(velocity.x, 0, friction)
The "ui_right" is set to right arrow key and D, the "ui_left" is set to left arrow key and A. When "ui_right" or "ui_left" are pressed the velocity vector (Vector2(x, y)) is incremented by speed, and when neither are pressed the move_toward function is used. The move_toward function takes the arguments “what value does it start at, what is the end value, what value is it decremented/incremented by”, in my case, the starting value is the current x velocity, the end value is 0, or standing still, and it is decremented by friction, which equals 10.
Jumping with Jump Buffering and Coyote Time
@export var jump = -400.0 ... var coyoteTimer var jumpBuffer = 0 var isJumping = false ... func _ready(): coyoteTimer = Timer.new() coyoteTimer.one_shot coyoteTimer.wait_time = 1.0 add_child(coyoteTimer) ... func _physics_process(delta): if jumpBuffer > 0: if is_on_floor(): velocity.y = jump isJumping = true jumpBuffer = 0 else: jumpBuffer -= 1 elif Input.is_action_just_pressed("ui_accept"): if is_on_floor(): velocity.y = jump isJumping = true elif not coyoteTimer.is_stopped(): velocity.y = jump isJumping = true else: jumpBuffer = 5 elif Input.is_action_just_released("ui_accept"): velocity.y = max(velocity.y, jump)
Let’s look through this line by line, starting with the variable jump, why is it negative if you want to go up, well in Godot, the vector graph is mirrored over the x axis so -y is up and +y is down. The other variables apply to jump buffering and coyote time. I haven’t told you about jump buffering and coyote time yet have I? well, they’re both fairly simple features that make the game feel smoother and more responsive. Jump buffering makes it so that if you hit the jump button ("ui_accept") and you're not quite on the ground, you'll jump when you land. Coyote time is named after Wile E. Coyote and makes it so that if you jump just after leaving a ledge, when it seems like you'd still be able to jump, you can. The _ready() function is the first thing to run, as it runs on startup, this one sets up the coyote timer The _physics_process() function runs every physics frame , yes, there are even more kinds of framerate! This first if statement checks to see if the jump buffering frames are still active. If they are, and the player is on the floor, then the player jumps, sets isJumping to true and sets the buffer frames to 0. if is_on_floor() is false, then the code decrements jumpBuffer by 1. If there aren't any buffer frames, it checks if "ui_accept" was pressed, if so, it checks if is_on_floor() is true, if so the player jumps, else if the coyoteTimer is active, and if so, the player jumps, else, it activates the jump buffer frames. I'm not entirely sure what that last elif statement does , it probably just jumps.
Bounding Box and Signals in Godot
Signal are an incredibly powerful tool when used correctly, "Signal up, call down" is a saying in Godot that helps to make organized code . Signals can connect nodes to trigger things, but they can be ignored, which helps for reusablity.
func _on_bound_body_entered(): get_tree().reload_current_scene()
The _on_bound_body_entered() function checks if a phyics body has entered the bounding walls. the get_tree().reload_current_scene() reloads the level.
Future Plans
I hope to finish world one by the end of this week, but we'll see when we get there, and hopefully by around the end of january, me and my partner can start working on the actual game.
TCHÜSS