Keep in mind that match (and switch) statements were initially introduced to be more efficient with compilers than if-else blocks. Now under the hood, modern languages often convert if-else chains to match/switch statements to optimize the running of code. So using them is purely aesthetic.
From a purely academic perspective, if you want to do matching like you’re talking about, you’d be better off using Regular Expressions (RegEx). They’re very fast and powerful for string comparisons. Or (as I will show you below) using String built-in functions - which is also faster than shoe-horning match.
Now let’s talk about your code. You are using match incorrectly. It’s like watching someone use a screwdriver as a crowbar. Potentially because you didn’t tell us enough about the data structures you’re using. Potentially because you’re not using GDScript naming conventions and so your naming conventions are confusing.
Is R_DamageType an Enum? Because its name indicates it is. But your code indicates it’s potentially a Resource? So I’m gonna go with that.
class_name Damage extends Resource
enum Type {
SLASHING,
PIERCING,
BLUNT,
FIRE,
}
@export var name: String = "Slashing"
@export var type: Type = Type.SLASHING
# name in ["head","torso","leg_right","leg_left","arm_right","arm_left","hand_right","hand_left"]
func get_crit(damage: Damage) -> void:
if name == "head":
match damage.type:
Damage.Type.SLASHING:
chopped_off()
mob.health.death()
elif name =="torso":
mob.health.death()
elif name.contains("leg"):
mob.collapse()
match damage.type:
Damage.Type.SLASHING:
chopped_off()
elif name.contains("arm"):
match damage.type:
Damage.Type.SLASHING:
chopped_off()
elif name.contains("hand"):
match damage.type:
Damage.Type.SLASHING:
chopped_off()
If you are using a switch statement, there’s no need to wildcard things. Be specific. An Enum is a fast way to have a certain set of options. Leave the name for text output, etc. (Though you can actually convert the enum keys to text if you need to.)
Naming is also important in helping your code be clear. If you REALLY need to know that Damage is a Resource, call it DamageResource. But your code becomes much more readable if you only have one object called Damage.
As for pattern matching, the String class contains a function contains() that returns true if the string you are looking for is contained in the String you use it on. This is faster than using match, and it’s much more readable in code.
Hope that helps.
A Final Note
There’s nothing wrong with asking questions. You started by saying you wonder why no one ever even thought about your solution. The answer people were trying to tell you was that your proposed solution created problems - it didn’t solve them - and they tried to explain why. You appear to have been upset by that.
No one was trying to hurt your feelings. They were trying to explain why there are other solutions. However, as @normalized pointed out, you started by posting a solution without the attendant problem. Most of the people responding to you have years - if not decades - of professional programming experience. We are just trying to help.