+1 vote

Hey there !
Working on the effect of attacktype vs enemyarmor 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#%
in Engine by (184 points)

2 Answers

+1 vote
Best answer

Hi,
Top of my head, you could store the damage modifiers values in separate arrays and do something like damagemodifier = dmvalue2[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)
by (2,055 points)
selected by

sweet ! much cleaner.
thanks man !

+1 vote

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])
by (2,055 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.