Code Redundances

Godot Version

4.7.1

Question

Hey so, the code I’m showing works but what I want to know is they’re any redundant code in this block. I’m still relatively new to Godot so idk if the things I’m doing is not needed or can be shorten.

class_name PlayerState extends LimboState

@export var player : PlayerController = agent as PlayerController
@export var action_anims : AnimationPlayer
@onready var input: PlayerInput = %Input
@onready var state_machine: LimboHSM = %StateMachine

static var direction: float
static var last_direction: float
static var speed: float
static var acceleration: float
static var deceleration: float
static var jump_force: float
static var gravity: float

static var can_air_combo: bool = true
static var has_jumped: bool = false
static var jump_buffered: bool = false


func apply_movement() -> void:
	direction = blackboard.get_var(BBNames.direction_var)
	speed = blackboard.get_var(BBNames.speed_var)
	acceleration = blackboard.get_var(BBNames.acceleration_var)
	deceleration = blackboard.get_var(BBNames.deceleration_var)

	var acceleration_factor: float = speed * acceleration
	var deceleration_factor: float = speed * deceleration

	if not is_zero_approx(direction):
		player.velocity.x = move_toward(
			player.velocity.x, speed * direction, acceleration_factor)

		blackboard.set_var(BBNames.last_direction_var, direction)
		last_direction = blackboard.get_var(BBNames.last_direction_var)
	else:
		player.velocity.x = move_toward(player.velocity.x, 0.0, deceleration_factor)
	
	player.move_and_slide()


func apply_jump_force() -> void:
	jump_force = blackboard.get_var(BBNames.jump_force_var)
	player.velocity.y = -jump_force


func apply_gravity(delta: float) -> void:
	if player.velocity.y < 0.0:
		gravity = blackboard.get_var(BBNames.jump_gravity_var)
	else:
		gravity = blackboard.get_var(BBNames.fall_gravity_var)

	if not player.is_on_floor():
		player.velocity.y += gravity * delta


func apply_coyote_timer(coyote_timer: Timer, input_pressed: bool) -> void:
	if has_jumped:
		return

	if input_pressed and not coyote_timer.is_stopped():
		apply_jump_force()
		has_jumped = true
		coyote_timer.stop()


func apply_jump_buffer(buffer_timer: Timer, input_pressed: bool) -> void:
	if jump_buffered:
		return

	if input_pressed:
		jump_buffered = true
		buffer_timer.start()

What’s blackboard?

is where my variables get saved to, so the state machine, sub state machines, and leaf states can access them (LimboAI Plugin)

Yes but what type of object it is? What is get_var() doing?

Those static variables look like they could and should be local variables.

its a resource and return type is variant

Well post the code. We can’t know if it’s doing something redundant without seeing what is it doing.

its built into the plugin don’t know how to access those properties since i didn’t make it.

You can look at the code.

by hovering over the method or try finding it in the addons folder?

Whichever works for you.

the get_var method has the return type Variant and blackboard is actually a refcounted my bad.

weird case of knowing how use it but not what it is.

is there more questions you have about the code before answering the main question?

Return type is irrelevant. If you’re asking for redundancies in the code, you need to post all of the code that gets executed. Your code calls some code in Blackboard class and it’s impossible to tell if there are redundancies in there if you don’t post its code.

Other than that your code seems fine.

sry for no reply i was at work.

well i cant access the limbo ai plugins code because the file looks like this:

and i didn’t make the plugin so no dice there.

but as i said before the blackboard is a something that holds variables so the state machine and states can use (also part of the plugin).

the code initially showed was for the in between for unique things for the player might need for each state.

like for the floor state:

# Floor state
extends PlayerState


func _enter() -> void:
	can_air_combo = true
	has_jumped = false
	input.sprint_pressed.connect(_on_sprint_pressed)
	input.jump_pressed.connect(_on_jump_pressed)
	input.attack_pressed.connect(_on_attack_pressed)


func _exit() -> void:
	input.sprint_pressed.disconnect(_on_sprint_pressed)
	input.jump_pressed.disconnect(_on_jump_pressed)
	input.attack_pressed.disconnect(_on_attack_pressed)


func _update(_delta: float) -> void:
	apply_movement()

	if jump_buffered:
		dispatch(&"JUMPING")

	if not player.is_on_floor():
		dispatch(&"AIRBORNE")


func _on_jump_pressed(pressed: bool) -> void:
	if pressed and jump_buffered:
		dispatch(&"JUMPING")


func _on_attack_pressed(pressed: bool) -> void:
	if pressed and player.is_on_floor():
		dispatch(&"GROUND_COMBO")


func _on_sprint_pressed(pressed: bool) -> void:
	if pressed:
		blackboard.set_var(BBNames.speed_var, 150)
	else:
		blackboard.set_var(BBNames.speed_var, 100)

	if pressed:
		if not is_zero_approx(player.velocity.x):
			action_anims.play(AnimNames.RUN)
		else:
			action_anims.play(AnimNames.IDLE_RUN)
	else:
		if not is_zero_approx(player.velocity.x):
			action_anims.play(AnimNames.WALK)
		else:
			action_anims.play(AnimNames.IDLE)

Oh, it’s extension code.

If there are no performance bottleneck that you can measure in the profiler - you can consider your code to be fine.

Ok now two questions:

  1. I’ve looked at the profiler but don’t now really what is specifically it’s saying. Is there documentation on it or video guide on how to read it?
  2. like for the apply movement function in player state, is any code that would be considered redundant? if you have questions on some parts of it i’ll answer it

These are just minor things, but you could simplify the line, where you update last_direction to

last_direction = direction

and you could combine the conditionals in apply_coyote_timer and apply_jump_buffer.

if jump_buffered or not input_pressed:
    return

Why do you worry so much about something being “redundant”?

Better spend that energy on learning how to use the profiler. There’s a decent description in the official docs:

Me not knowing that much about something gives me an annoying nagging feeling that i’m doing something wrong even know they’re a many ways to do it so that’s the main reason i’m asking.