Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.
These challenges are really cool!HardRockI really enjoy how this course is structured as a whole. I mainly mean the fact that it gradually moved toward us doing more and more, with only pointers given to us. I have to admit, when I got to this point and read that we'll be re-implementing previous functionality ourselves I thought things would be easy, but I realized a couple of things very quickly:
1) I didn't remember nearly as many concepts and methods as I thought I did. Mentioning and showing the Godot documentation during the course turned out to be extremely helpful, again. :)
2) Re-implementing stuff ourselves allows us to write some of the previous code differently, and experiment with concepts we learned. For example I try to use ternary if expressions when possible, just to see how this feels in practice and how it affects code readability. I also started experimenting with how I name variables and adhering more to the DRY principle, to see what downsides it has when it is done prematurely.
3) It is very easy to make trivial mistakes when we are writing all of the code ourselves. I just spent 10 minutes debugging a texture flipping bug in my code, that was caused by a silly typo. :)11Jan. 02, 2025
My solution is slightly different but it works for me.PurpleSunriseHello,
I just wanted to confirm that I am doing it right. My code is a bit different from the reference (I am pushing myself to not spend too much time thinking but just coding in the fastest way so that I can to solve the problem at hand since it's the first phase of implementation).
Here's my code:
```gdscript
extends CharacterBody2D
const GODOT_BOTTOM = preload("res://player/godot_bottom.png")
const GODOT_BOTTOM_RIGHT = preload("res://player/godot_bottom_right.png")
const GODOT_RIGHT = preload("res://player/godot_right.png")
const GODOT_UP = preload("res://player/godot_up.png")
const GODOT_UP_RIGHT = preload("res://player/godot_up_right.png")
@onready var robot_sprite: Sprite2D = %RobotSprite
@export var max_speed : float = 100.0
@export var acceleration : float = 1200.0
@export var deceleration : float = 800.0
var direction := Vector2.ZERO
func _physics_process(delta: float) -> void:
direction = Input.get_vector("left", "right","up","down")
var has_input_direction : float = direction.length() > 0.0
var desired_velocity = direction * max_speed
if has_input_direction:
velocity = velocity.move_toward(desired_velocity, acceleration * delta)
else:
velocity = velocity.move_toward(Vector2.ZERO, deceleration * delta)
move_and_slide()
var direction_discrete = direction.sign()
match direction_discrete:
Vector2.DOWN:
robot_sprite.texture = GODOT_BOTTOM
Vector2.DOWN + Vector2.RIGHT, Vector2.DOWN + Vector2.LEFT:
robot_sprite.texture = GODOT_BOTTOM_RIGHT
Vector2.RIGHT, Vector2.LEFT:
robot_sprite.texture = GODOT_RIGHT
Vector2.RIGHT + Vector2.UP, Vector2.LEFT + Vector2.UP:
robot_sprite.texture = GODOT_UP_RIGHT
Vector2.UP:
robot_sprite.texture = GODOT_UP
if direction_discrete.length() > 0.0:
robot_sprite.flip_h = direction.x < 0.0
```
So as you can see I didn't spend time renaming the texture references and I used the `move_toward()` method instead of the steering factor from M4. The result looks the same as the video reference, but wanted to be sure if my mindset is right or if I am thinking in a way I shouldn't for some reasons.
Also the `has_input_direction` var is something I used for module 9 but if it was for me I would go:
if direction.length() > 0.0:
do something
else:
do something else
is it right or that var helps in any way?
Thank you so much and sorry for if the questions are kinda dumb.10Jan. 20, 2025
adding the steering and dragSootySo I attempted this lesson, I initially used acceleration and deceleration as we did in the previous module. After completing the lesson and looking at the solution code I saw you used the same code we used in the earlier lesson with the steering factor/vector. In that lesson we used it to rotate. After testing the code , it feels like its doing the same thing as accelerating and decelerating, but is that what is is actually doing?
my code was
var has_input_direction := direction.length() > 0.0
if has_input_direction:
var desired_velocity = direction * max_speed
velocity = velocity.move_toward(desired_velocity, acceleration * delta)
else:
velocity = velocity.move_toward(Vector2.ZERO, deceleration * delta)
move_and_slide()10Jan. 18, 2025
Should I rename all the child nodes?DMKIn my player scene I have the sprite child, named simply 'Sprite2D'. The problem arises when I attach a script to it, because it auto-generates the name 'sprite_2d.gd', which would be a problem when I add a different sprite child to the enemy. So then I would rename the script to 'player_sprite_2d.gd', but then it doesn't match with the node name. When there are many child nodes, it might be hard to find out which script corresponds to which node.
So my question is:
1 - Is it okay to have different names for the node and the script attached to it?
2 - Is it okay to use the default node name for childs?10Jan. 14, 2025
When the gamepad is connected, the player's movement changes slightly.ram876Hello! I just noticed one bug and I don't know how to deal with it. If a gamepad is connected to the computer, and I control the keyboard, then when the character moves to the right or left, the character does not move in a straight line to the right or left, but when moving, a small upward movement is added and the player's spray is turned up. Also, if I move the character's keyboard up or down, then a little movement is added to the movement and to the right, and sometimes when reconnecting, on the contrary, to the left. I tried to increase the dead zone in the settings, but it didn't help, even with the parameter set to 1. After disconnecting the gamepad, everything is fine.60Jan. 03, 2025
Using class names for better auto-completion suggestionsHardRockThis is probably obvious for many, especially people who are more familiar with object-oriented programming, but I wanted to share my experience. Using class names has an additional benefit not directly mentioned in the troubleshooting section, although it is related to type checking. With class names Godot can provide more accurate auto-completion suggestions as well.
For example, when working on these challenges, I used a second script on the Sprite2D node to handle the changing of the sprite texture and I didn't want to call `Input.get_vector()` a second time here (needless and premature optimization for sure, but I learned something as a result). I made the player direction a property of the root CharacterBody2D node instead and tried to use it in the Sprite2D script, but Godot didn't show the property among the auto-complete suggestions, even though I could definitely access the property. I quickly realized that the problem was that I was saving the reference to the root node with a CharacterBody2D type hint, and that class doesn't have a direction property of course. After adding `class_name Player` to the root node script and using this as the type hint for Godot when adding a reference to the node in the sprite script Godot could provide auto-completion hints for any properties I added.20Jan. 02, 2025
Error while parsing file room.tscnimmense-swallowWhat does that mean?50Dec. 03, 2024
Using a controller iguessfiveI'm trying to add controller for input when moving the sprite but I am finding it difficult to solve the following bug. When moving to any cardinal directions the sprite will face a corresponding ordinal direction. I think the issue is that the inputs near zero are not registering as zero but - or + 1.
I tried implementing is_zero_approx() like this but did not work.
```gdscript
direction.x = 0 if is_zero_approx(direction.x) else direction.x
direction.y = 0 if is_zero_approx(direction.y) else direction.y
```
How would I go about solving this?80Nov. 14, 2024
Lesson Q&A
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.