how do i use "match" when an animation is finished?

this code doesnt work

func _on_animation_player_animation_finished(anim_name):
	match anim_name:
		"Hide":
			isequipped = false
		"Weild":
			isequipped = true
		"ReloadA":
			isreloading = false
		"ReloadB":
			isreloading = false
		"Fire":
			isfiring = false
		"FireLast":
			isfiring = false
func _on_animation_player_animation_finished(anim_name):
    if anim_name == "Hide":
		isequipped = false

@Zelta said:

func _on_animation_player_animation_finished(anim_name):
    if anim_name == "Hide":
		isequipped = false

ok that works but why does using "match" not

Try adding a default case to the match, and print anim_name for that case:

_:
	print_debug("no match, anim_name=", anim_name)

The syntax looks correct. It's possible there is another issue.

@DaveTheCoder said: Try adding a default case to the match, and print anim_name for that case:

_:
	print_debug("no match, anim_name=", anim_name)

strange it prints out correct. wonder why it doesnt work and an "if" statement does could it be an alpha issue?

nope i was wrong. match doesnt work

@DJM said:

@Zelta said:

func _on_animation_player_animation_finished(anim_name):
    if anim_name == "Hide":
		isequipped = false

ok that works but why does using "match" not

not sure, but something like this work for me

func _on_AnimationPlayer_animation_finished(anim_name):
	match anim_name:
		"anim1":
			print("1")
		"anim2":
			print("2")

could you paste your code? like Cyb said, It's possible there is another issue.

heres my weapon script match anim_name doesnt work if statement does

extends Node3D
#nodepaths


@export var myAnimation_path: NodePath
@export var muzzleFlash_path: NodePath



@onready var myAnimation = get_node(myAnimation_path)
@onready var muzzleFlash = get_node(muzzleFlash_path)

#how do i get the player? $Player?
@onready var player = get_node("/root/World/Player")
@export var normalPos = Vector3.ZERO
@export var runPos = Vector3.ZERO
@export var aimPos = Vector3.ZERO
@export var runRotation = Vector3.ZERO
@export var normalRotation = Vector3.ZERO
var wantedrotation = Vector3.ZERO



@export var fireSound : AudioStreamSample

@export var readySound : AudioStreamSample
@export var reloadAsound : AudioStreamSample
@export var reloadBsound : AudioStreamSample
@export var emptySound : AudioStreamSample

@export var fireAnimSpeed = 1.5
@export var currentAmmo = 40
@export var totalAmmo = 400
var ammoInmag = 0

@export var shell : PackedScene
@export var shellposition_path: NodePath
@onready var shellposition = get_node(shellposition_path)

var isfiring = false
var canfire = true
var canreload = true
var canaim = true
var isaiming = false
var isreloading = false
var isequipped = false

var random = RandomNumberGenerator.new()

func _ready():
	
	random.randomize()
	ammoInmag = currentAmmo
	
	normalRotation = Vector3(deg2rad(normalRotation.x), deg2rad(normalRotation.y), deg2rad(normalRotation.z)) 
	runRotation =   Vector3(deg2rad(runRotation.x), deg2rad(runRotation.y), deg2rad(runRotation.z)) 
	
	
	
		
func _process(delta): 
	#do weaponlogic
	if totalAmmo <= 0 or currentAmmo == ammoInmag:
			canreload = false
	else:
		canreload = true
	if myAnimation.is_playing() == false:
		if Input.is_action_pressed("fire") and canfire :
			fire()
	
	
		if (Input.is_action_pressed("reload") or currentAmmo == 0 ) and canreload and !isfiring:
			reload()
	
	
	#addposition and rotation effects
	if (player.isrunning == true and player.is_on_floor()):
		canfire = false
		
		self.transform.origin = self.transform.origin.lerp(runPos, 10 * delta)
		wantedrotation = runRotation 
		isaiming = false
		#setrunfov
	else:
		canfire = true
		
		if Input.is_action_pressed("aim") and !isreloading and player.is_on_floor():
			self.transform.origin = self.transform.origin.lerp(aimPos, 10 * delta)
			wantedrotation = normalRotation
			isaiming = true
			#set aimFOV
		else:
			
			self.transform.origin = self.transform.origin.lerp(normalPos, 10 * delta)
			wantedrotation = normalRotation
			isaiming = false
			#setnormalfov
		
	
	var addrotation : Quaternion = Quaternion(wantedrotation)
	self.transform.basis = Basis(Quaternion(self.transform.basis).slerp(addrotation, 10* delta))
	
	
	
 
func fire():
	isfiring = true
	if currentAmmo <=1:
		#fireLast
		myAnimation.play("FireLast", -1, fireAnimSpeed)
		currentAmmo -= 1
		muzzleFlash.restart()
		#this is broken
		#$CamRoot/HeadBob/MainCamera/RayCaster.firebullet()
		ejectshell()
		
		
		var asp = AudioStreamPlayer.new()
		self.add_child(asp)
		asp.stream = fireSound
		asp.pitch_scale = random.randf_range(0.8, 1.2)
		asp.play()
		if !asp.is_playing():
			asp.queue_free()
	else:
		#firenormal
		myAnimation.play("Fire", -1, fireAnimSpeed)
		currentAmmo -= 1
		muzzleFlash.restart()
		
		#this is broken
		#$CamRoot/HeadBob/MainCamera/RayCaster.firebullet()
		ejectshell()
		
		var asp = AudioStreamPlayer.new()
		self.add_child(asp)
		asp.stream = fireSound
		asp.pitch_scale = random.randf_range(0.8, 1.2)
		asp.play()
		if !asp.is_playing():
			asp.queue_free()
		
	
	

func ejectshell():
	
	
	var shell_instance = shell.instantiate()
	get_tree().get_root().add_child(shell_instance)
	
	
	shell_instance.global_transform.origin = shellposition.global_transform.origin
	shell_instance.global_transform.basis = shellposition.global_transform.basis.orthonormalized()

	var wantedVector = shellposition.global_transform.basis.x.normalized()
	shell_instance.apply_central_impulse(wantedVector * -2)
	shell_instance.rotate_object_local(Vector3(1,0,0), random.randf_range(-45, 45))
	

func reload():
	
	isreloading = true
	
	if currentAmmo == 0:
		myAnimation.play("ReloadB")
		var asp = AudioStreamPlayer.new()
		self.add_child(asp)
		asp.stream = reloadBsound
		
		asp.play()
		
		
		
	else:
		myAnimation.play("ReloadA")
		var asp = AudioStreamPlayer.new()
		self.add_child(asp)
		asp.stream = reloadAsound
		
		asp.play()
		
	
	var ammo_needed = ammoInmag - currentAmmo
	if totalAmmo >= ammo_needed:
		totalAmmo -= ammo_needed
		currentAmmo = ammoInmag
	else:
		currentAmmo += totalAmmo
		totalAmmo = 0
	
	
func updateAmmo():
	var ammo_needed = ammoInmag - currentAmmo
	if totalAmmo >= ammo_needed:
		totalAmmo -= ammo_needed
		currentAmmo = ammoInmag
	else:
		currentAmmo += totalAmmo
		totalAmmo = 0

	
func show_weapon():
	visible = true

func hide_weapon():
	visible = false
	

func _on_animation_player_animation_finished(anim_name):
	if anim_name == "Hide":
		isequipped = false
	if anim_name == "Weild":
		isequipped = true
	if anim_name == "ReloadA":
		isreloading = false
	if anim_name == "ReloadB":
		isreloading = false
	if anim_name == "Fire":
		isfiring = false
	if anim_name == "FireLast":
		isfiring = false
	
	
	match anim_name:
		
	#	"Hide":
	#		isequipped = false
	#	"Weild":
	#		isequipped = true
	#	"ReloadA":
	#		isreloading = false
	#	"ReloadB":
	#		isreloading = false
		"Fire":
			isfiring = false
			print_debug("no match, anim_name=", anim_name)
	#	"FireLast":
	#		isfiring = false

@DJM said: could it be an alpha issue?

You're using Godot 4.0 alpha? You should have mentioned that in your first post. You might make a minimal project that simply tests the match statement, and use it to submit a bug report if the match isn't working correctly.

@DaveTheCoder said:

@DJM said: could it be an alpha issue?

You're using Godot 4.0 alpha? You should have mentioned that in your first post. You might make a minimal project that simply tests the match statement, and use it to submit a bug report if the match isn't working correctly.

When the code is behaving unexpectedly, add lots of print() or print_debug() statements, or use the debugger (set breakpoints and inspect variables).

yeah im on alpha 4, should have mentioned that, sorry. ill post an issue on git

godot4 has new syntax i should have been using>

match anim_name:
	&"Fire":
		pass

For reference, this was cross-posted on GitHub: https://github.com/godotengine/godot/issues/60145

match does strict type matching and anim_name is a StringName here, not a String. This is why you need the & prefix to convert the String literal to a StringName literal.

8 months later