Code Critique: Looking for cleaner code formatting

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MoxieTouch

I’m looking for input on my code formatting and what I can do to improve readability. Below are a few of ones I’m not proud of.

# uses four step .ogg from the file directory
func _on_step_finished():
    if rand_step < 4: rand_step += 1 
    else: 			  rand_step = 1

    var strang = "res://Assets/1_Audio/sfx/footsteps/footsteps/step" + str(rand_step) + ".ogg"
    var step_audio : AudioStreamOGGVorbis = load(strang)

    #print(step_audio, rand_step)	
    $Audio/ = step_audio

Another function.

func spawn_fauna(var damage_fauna : int = 0):
# TODO: Code monster-spawning depending on the type of 
#		monster.
# TODO: Find code that automatically finds the scene 
# 		in the file	system, then use that instead of the 
#		string below

var monster = "f_" + mon_grappled
var prelim = "res://Entities/Fauna/f_subtypes/"

var fauna_loc = prelim + monster + "/" + monster + ".tscn"

var fauna = load(fauna_loc)
var fauna_inst = fauna.instance()
fauna_inst._func_ready_take_damage = damage_fauna


Should I also batch my variables at the top of the script like below? Or just put variables near functions that need them?

export (int) var   acceleration = 50
export (float) var slowdown     = .20
export (float) var afterglow_time = 0
export (Array) var player_states_i_can_saucy = []

onready var Obj
onready var enterDetect = get_node("detectBodies/enterDetect")
onready var exitDetect  = get_node("detectBodies/exitDetect")
onready var attackRange = get_node("detectBodies/attackRange")
onready var world = get_parent()

var afterglow : bool = false
var _func_ready_take_damage : int = 0

var player_near_chasing_range : bool = false
var hurt_in_day  = false
var is_alive = true
var active_attack : bool = false
:bust_in_silhouette: Reply From: phiz

Have you seen the GDScript style guide?

When variables are only relevant/referenced within one function it’s probably best to declare them within the function scope.

If you are going to type cast variables then do so consistently, eg. both player_near_chasing_range and hurt_in_day seem to be bool but only one is explicitly cast.

Small comment about being wary of building strings with the + operator, you can call str() with arbitrary arguments eg. var fauna_loc = str(prelim, monster, "/", monster, ".tscn"), just keep in mind your other options such as format strings, and that you can use a similar arg syntax in print() and prints() [adds spaces between args].