Godot Version
4
Question
So idk whats wrong with the code because i did define speed earlier in the code, but when I hover over it its value is null.
extends CharacterBody2D
class_name Player
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var animated_sprite_2d = $Sprite2D as PlayerAnimatedSprite
@onready var area_collision_shape = $"Area2D/area collision shape"
@onready var body_collision_shape = $bodycollisionshape
@onready var area_2d = $Area2D
@export_group ("Locomotion")
@export var run_speed_damping = 0.5
@export var speed = 200.0
@export var jump_velocity = -400
@export_group ("")
@export_group("stomping enemies")
@export var min_stomp_degree = 35
@export var max_stomp_degree = 145
@export var stomp_y_velocity = -150
@export_group("")
@export_group("camera sync")
@export var camera_sync: Camera2D
@export var should_camera_sync: bool = true
@export_group("")
func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
if Input.is_action_just_released("jump") and velocity.y < 0:
velocity.y *= 0.5
var direction = Input.get_axis("left","right")
if direction:
velocity.x = lerpf(velocity.x, speed * direction, run_speed_damping * delta)
else:
velocity.x = move_toward(velocity.x, 0, speed * delta)
move_and_slide()
im also following along with a tutorial rn, but he doesn’t seem to have any trouble in the video
You did define speed as an export with a default value.
None of the code you show changes the value for speed and since it has a default value it should not be nil.
Don’t rely on hovering your mouse over it. Either print it out or put in a breakpoint and look at the value in the debug window.
You also aren’t really stating a problem here. What exactly is going on?
Are you getting an error message?
You may need to check the export values in the Inspector and re-save them. It would also help to add types to your exported variables, this prevents null errors and should always show an accurate value in the Inspector even if zero.
@export var speed: float = 200.0
yes the error message is this: E 0:00:00:825 Player._physics_process: Invalid operands ‘Nil’ and ‘float’ in operator ‘*’.
this error applies to the line
else:
velocity.x = move_toward(velocity.x, 0, speed * delta)
sorry if this wasn’t well explained
What happens if you remove @export annotation from speed declaration line?
yeah it looks like that worked! thank you!
Why was @export there in the first place?
Why would having @export cause that bug?
i was following along with a tutorial for this bit and they used it. idk why though
heres the link: https://youtu.be/JOytyzK7Z94?si=S1p5rX_sNX9TBYDE
Hard to tell what exactly happened but the default value must have gotten overridden somehow.