How can i make this IF ELIF 50lines cleaner ?

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

Hey there !
Working on the effect of attack_type vs enemy_armor and define a modifier’s damage to it.

I got it to work. But dear lord it’s ugly.
I can’t think of any other way to list it… Anyone has a “shorter” idea ? Match ?

#Let's define the modifier depending on attakinng atype and monster's armor
##### Attack type :
##### 1 normal
##### 2 pierce
##### 3 siege
##### 4 magic
##### 5 heroic : 100% all but divine armor

#####Armor type :
##### 0 Unarmored : 100%
##### 1 light armor
##### 2 medium armor
##### 3 heavy armor
##### 4 fortified armor
##### 5 divine armor : onlly 30% no matter what.

if attack_type == 1: #NORMAL ATTACK
	if armor_type == 1:
		damage_modifier = 1.5#%
	elif armor_type == 2:
		damage_modifier = 1#%
	elif armor_type == 3:
		damage_modifier = 1#%
	elif armor_type == 4:
		damage_modifier = 0.7#%
	elif armor_type == 5:
		damage_modifier = 0.3#%
		
elif attack_type == 2:
	if armor_type == 1:
		damage_modifier = 0.7#%
	elif armor_type == 2:
		damage_modifier = 1.5#%
	elif armor_type == 3:
		damage_modifier = 1#%
	elif armor_type == 4:
		damage_modifier = 1#%
	elif armor_type == 5:
		damage_modifier = 0.3#%

elif attack_type == 3:
	if armor_type == 1:
		damage_modifier = 1#%
	elif armor_type == 2:
		damage_modifier = 0.7#%
	elif armor_type == 3:
		damage_modifier = 1.5#%
	elif armor_type == 4:
		damage_modifier = 1#%
	elif armor_type == 5:
		damage_modifier = 0.3#%

elif attack_type == 4:
	if armor_type == 1:
		damage_modifier = 1#%
	elif armor_type == 2:
		damage_modifier = 1#%
	elif armor_type == 3:
		damage_modifier = 0.7#%
	elif armor_type == 4:
		damage_modifier = 1.5#%
	elif armor_type == 5:
		damage_modifier = 0.3#%

elif attack_type == 5: #Heroic type doe 100% but for divine armor
	if armor_type == 5:
		damage_modifier = 0.3#%
	else:
		damage_modifier = 1#%
		
:bust_in_silhouette: Reply From: deaton64

Hi,
Top of my head, you could store the damage modifiers values in separate arrays and do something like damage_modifier = dm_value2[3]
Then you’d only need the 4 ifs.
Should work. Haven’t tried it. Sent from my phone.

This is what I mean:

var attack_type = 1
var armor_type = 1
var damage_modifier

var dm1 = [0, 1.5, 1, 1, 0.7, 0.3]
var dm2 = [0, 0.7, 1.5, 1, 1, 0.3]
var dm3 = [0, 1, 0.7, 1.5, 1, 0.3]
var dm4 = [ 0, 1, 1, 0.7, 1.5, 0.3]
var dm5 = [ 0, 0.3, 1, 0, 0, 0]

if attack_type == 1: #NORMAL ATTACK
	damage_modifier = dm1[armor_type]		
elif attack_type == 2:
	damage_modifier = dm2[armor_type]	
elif attack_type == 3:
	damage_modifier = dm3[armor_type]
elif attack_type == 4:
	damage_modifier = dm4[armor_type]
elif attack_type == 5: #Heroic type doe 100% but for divine armor
	damage_modifier = dm5[armor_type]

print(damage_modifier)

sweet ! much cleaner.
thanks man !

quizzcode | 2020-05-21 21:56

:bust_in_silhouette: Reply From: deaton64

Also, you can get it down to one line by having a dictionary with keys and arrays.
That would get rid of any if statements.
Armor_type would have to start at 0, or you’d need to set a value in the array for the 0 value, if you wanted it to start at 1.

This seems to work for me:

extends Node2D

func _ready():

	var dm = { "dm1": [1.5, 1 , 1, 0.7, 0.3],
		   "dm2": [0.7, 1.5, 1, 1, 0.3 ],
		   "dm3": [1, 0.7, 1.5, 1, 0.3 ],
		   "dm4": [1, 1, 0.7, 1.5, 0.3 ],
		   "dm5": [0.3, 1, 0, 0, 0 ] }

	var attack_type = 4
	var armor_type = 2

	print(dm.get("dm"+str(attack_type))[armor_type])

Much better than the selected! No ifs at all is the way to go :slight_smile:

A112Studio | 2020-05-22 12:38