Selection screen for characters problem

I tried to create a selection screen to select characters (male or female) from a youtube video, I tried many variations because it didn’t work for me. I created a Character2d and made 2 animated sprite it’s child named ‘fem’ and ‘male’. The animation names are the same in both animated sprites. I really want that if you click on one character on the selection screen it would use only that animated sprite and ignore the other but I can’t make it happen. I would really appreciate if someone could help me out because I really don’t know where to continue. Here is the script for the Selection screen

extends Node2D

func _on_male_pressed():
	var character = 'male'
	get_tree().change_scene_to_file("res://Scenes/main.tscn")

func _on_fem_pressed():
	var character = 'fem'
	get_tree().change_scene_to_file("res://Scenes/main.tscn")

And here is the script for the player:

extends CharacterBody2D

var speed = 200
var health = 100
const jump_velocity = -400
var direction = Vector2()
var jetpack_fuel = 100
var points = 0
var male = true
var fem = false
var helm_on = false

@onready var sprite_2d = $Sprite2D
@onready var fuel_label = $Camera2D/jetpack_fuel 
@onready var health_label = $Camera2D/main_health
@onready var health_bar = $Camera2D/health_bar
@onready var fuel_bar = $Camera2D/fuel_bar
@onready var character

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var animation = ''

func _ready():
	health_bar.value = health
	fuel_bar.value = jetpack_fuel
	
func damage(delta):
	if Input.is_action_just_pressed('Fire'):
		health -= 50 * delta
	health_bar.value = health
#Function for moving

func _jetpacking(delta):
	if Input.is_action_pressed("Jump") and jetpack_fuel > 0:
		velocity.y = jump_velocity * 0.5 
		jetpack_fuel -= 10 * delta 
		animation = 'jetpacking' 
	else:
		velocity.y += gravity * delta 
		if not is_on_floor():
			animation = 'jumping' 
	if not is_on_floor():
		speed = 250
	fuel_bar.value = jetpack_fuel

func _walking_n_running():
	direction = Input.get_axis("GoLeft", "GoRight")
	if direction != 0:
		velocity.x = direction * speed
		if speed > 200 and is_on_floor():
			animation = "running"
		if speed <= 200 and is_on_floor():
			animation = "walking"
	else:
		velocity.x = move_toward(velocity.x, 0, 16)
		if is_on_floor():
			animation = "idle_helm"  

func _jump(delta):
	if is_on_floor() and Input.is_action_just_pressed("Jump"):
		velocity.y = jump_velocity
		animation = 'jumping'
	elif not is_on_floor():
		_jetpacking(delta)

func _running():
	if Input.is_action_pressed('Sprint') and is_on_floor():
		speed = 280
	else:
		speed = 200

func _flip():
	if Input.is_action_just_pressed('GoLeft'):
		sprite_2d.flip_h = true
	if Input.is_action_just_pressed('GoRight'):
		sprite_2d.flip_h = false
#Function for moving end

func _male_or_fem():
	if male == true:
		male.animation = animation
	else:
		fem.animation = animation


func _physics_process(delta):
	#movements
	
	_jump(delta)
	_walking_n_running()
	_running()
	_flip()
	move_and_slide()
	
	#labelsetc_

	fuel_label.text = str(int(jetpack_fuel))
	health_label.text = str(int(health))
	_male_or_fem()
	#healthchanges
	damage(delta)

I also made some screenshots
Képernyőkép 2024-08-25 170218



Képernyőkép 2024-08-25 170207

You have a bit misunderstanding how vars works, or this is ChatGPT code

  1. I recommend find video about how to create singletons (or autoloads)
  2. Create singleton, for ex. Globals with var character
  3. In character selection code use Globals.character = 'male' and Globals.character = 'fem'
  4. In Player script
var sprite_2d = null
...
@onready var male_sprite= $mal
@onready var female_sprite= $fem
...
func _ready():
 if Globals.character = 'fem':
   sprite_2d = female_sprite
 else:
   sprite_2d = male_sprite
...

instead_male_or_fem() use sprite_2d.play(animation)

I changed the main code to this:

extends CharacterBody2D

var speed = 200
const jump_velocity = -400
var direction = Vector2()
var jetpack_fuel = 100
var points = 0
var helm_on = false
var sprite_2d = null
@onready var fuel_label = $Camera2D/jetpack_fuel 
@onready var health_label = $Camera2D/main_health
@onready var health_bar = $Camera2D/health_bar
@onready var fuel_bar = $Camera2D/fuel_bar
@onready var male = $male_sprite
@onready var fem = $female_sprite

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var animation = sprite_2d.play('')

func _ready():
	if Global.character == 'fem':
		sprite_2d = fem
		male.visible = false
	else:
		sprite_2d = male
		male.visible = true
	health_bar.value = Global.health
	fuel_bar.value = jetpack_fuel

func damage(delta):
	if Input.is_action_just_pressed('Fire'):
		Global.health -= 50 * delta
		health_bar.value = Global.health
#Function for moving

func _jetpacking(delta):
	if Input.is_action_pressed("Jump") and jetpack_fuel > 0:
		velocity.y = jump_velocity * 0.5 
		jetpack_fuel -= 10 * delta 
		animation = 'jetpacking' 
	else:
		velocity.y += gravity * delta 
		if not is_on_floor():
			animation = 'jumping' 
	if not is_on_floor():
		speed = 250
	fuel_bar.value = jetpack_fuel

func _walking_n_running():
	direction = Input.get_axis("GoLeft", "GoRight")
	if direction != 0:
		velocity.x = direction * speed
		if speed > 200 and is_on_floor():
			animation = "running"
		if speed <= 200 and is_on_floor():
			animation = "walking"
	else:
		velocity.x = move_toward(velocity.x, 0, 16)
		if is_on_floor():
			animation = "idle_helm"  

func _jump(delta):
	if is_on_floor() and Input.is_action_just_pressed("Jump"):
		velocity.y = jump_velocity
		animation = 'jumping'
	elif not is_on_floor():
		_jetpacking(delta)

func _running():
	if Input.is_action_pressed('Sprint') and is_on_floor():
		speed = 280
	else:
		speed = 200

func _flip():
	if Input.is_action_just_pressed('GoLeft'):
		sprite_2d.flip_h = true
	if Input.is_action_just_pressed('GoRight'):
		sprite_2d.flip_h = false
#Function for moving end



func _physics_process(delta):
	#movements
	
	_jump(delta)
	_walking_n_running()
	_running()
	_flip()
	move_and_slide()
	
	#labelsetc_

	fuel_label.text = str(int(jetpack_fuel))
	health_label.text = str(int(Global.health))
	#healthchanges
	damage(delta)

and the selection screen to this:

extends Node2D


func _on_male_pressed():
	Global.character = 'male'
	get_tree().change_scene_to_file("res://Scenes/main.tscn")

func _on_fem_pressed():
	Global.character = 'fem'
	get_tree().change_scene_to_file("res://Scenes/main.tscn")

now I can actually press the buttons and move to the main scene but it shows both sprites and they are not moving at all

fem.visible = false instead male.visible = true

I have no wish to check all this code, there are a lot of videos about movement. I recommend stat with basics, only left-right, then add jump then add sprint, etc.

I had a working code for movements but now that there are 2 of the characters it’s not working. Maybe it’s bc I have 2 animated sprite instead of one, tbh idk

No, in reality movings CharacterBody2D, not animated sprite

I don’t see any big problems in movement, if godot reported red errors below, try to fix it.