Character Swaping

Godot Version

4.3

Question

I am new to coding and using godot and I wanted to make a game with multiple characters that you can swap between by pressing a key. It is a 2d top down game and I have movement and animation for the two sprites im going to use.

Sorry, I should have been more clear. I have tried using a tutorial for split-screen, using variables to move the characters, because I couldn’t move them by just having two sets of inputs for two different characters. This is the video I watched: https://www.youtube.com/watch?v=tkBgYD0R8R4&t=200s . My main problem is updating the code from the older version of godot, and figuring out the switching part because I know nothing about GD script and I was just wondering if you could help me. Thanks.

1 Like

This is how I did that for a 3D game: I just used the same script for all the characters, and then created buttons to switch between them all. They all had an is_player value, and if it was true, player input affected them (and I moved the camera to them.)

func _on_test_tools_gui_knight_selected():
	$Barbarian.is_player = false
	$Mage.is_player = false
	
	$Knight.is_player = true
	$Knight.get_node("FollowCamera/Camera3D").set_current(true)


func _on_test_tools_gui_mage_selected():
	$Barbarian.is_player = false
	$Knight.is_player = false
	
	$Mage.is_player = true
	$Mage.get_node("FollowCamera/Camera3D").set_current(true)
	

func _on_test_tools_gui_barbarian_selected():
	$Knight.is_player = false
	$Mage.is_player = false
	
	$Barbarian.is_player = true
	$Barbarian.get_node("FollowCamera/Camera3D").set_current(true)
func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta

	# Get movement
	if is_player:
		get_move_input(delta)

	# Apply Movement
	#   The character can't move when blocking or attacking,
	#   but it can move while dodging.
	if (!blocking and !is_attacking()) or is_dodging() or jumping:
		move_and_slide()
	
	# Player Facing
	if is_player:
		##Line the player up with the camera
		if velocity.length() > 1.0:
			model.rotation.y = lerp_angle(model.rotation.y, spring_arm.rotation.y, rotation_speed * delta)
func get_move_input(delta):
	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity
		do_action("jump")
		
	# We just hit the floor after being in the air
	if is_on_floor() and not grounded:
		do_action("land")
	grounded = is_on_floor()
	
	# Falling (We're in the air, but we didn't jump)
	if not is_on_floor() and not jumping:
		do_action("fall")
	
	#Combination Attacks
	if Input.is_action_pressed("attack") and Input.is_action_pressed("forward"):
		do_action("attack_stab")
	if Input.is_action_pressed("attack") and Input.is_action_pressed("back"):
		do_action("attack_chop")
	if Input.is_action_pressed("attack") and Input.is_action_pressed("left"):
		do_action("attack_slice_horizontal")
	if Input.is_action_pressed("attack") and Input.is_action_pressed("right"):
		do_action("attack_slice_horizontal")
	
	# Walking/Running
	var input_dir = Input.get_vector("left", "right", "forward", "back")
	var direction = Vector3(input_dir.x, 0, input_dir.y).rotated(Vector3.UP, spring_arm.rotation.y)
	if direction:
		velocity.x = direction.x * speed
		velocity.z = direction.z * speed
	else:
		velocity.x = move_toward(velocity.x, 0, speed)
		velocity.z = move_toward(velocity.z, 0, speed)

	#Determine Walk/Run Animation
	velocity = lerp(velocity, direction * speed, acceleration * delta)
	var vl = velocity * model.transform.basis
	anim_tree.set("parameters/IWR/blend_position", Vector2(vl.x, -vl.z) / speed)
func _unhandled_input(event):
	if is_player:
		if event is InputEventMouseMotion:
			spring_arm.rotation.x -= event.relative.y * mouse_sensitivity
			spring_arm.rotation_degrees.x = clamp(spring_arm.rotation_degrees.x, -90.0, 30.0)
			spring_arm.rotation.y -= event.relative.x * mouse_sensitivity
		
		do_action(Actions.get_action(event))

how does the character movement work? did you use variables instead of inputs?

Do you mean for the non-active characters?

For both, when I code the character’s movements, I use the inputs from the input map, and when you have two sets of movements (at least for me), the movement doesn’t work.

Edit: omg I’m stupid sorry it works fine thank you! I still dont know how to turn off the second characters movements when its not selected though. also did you use the same WASD for all of the characters?

Another Edit: I figured it out thank you so much!

1 Like

Glad I could help! It’s nice when your code is useful to other people.