Doing arithmetic with KV

edited January 2015 in Tutorials

Hello!

This short tutorial is here to inform you that it is possible to do arithmetic calculations in the KV fields!

In this example the AbilitySpecial field air_time has the value of 3

"Knockback"
{
    "Target"    "TARGET"
    "Center"    "CASTER"
    "Distance"    "0"
    "Duration"    "%air_time / 2"
    "Height"    "200"
    "IsFixedDistance"    "1"
}

The duration of the Knockback will be 1.5 seconds in this case(it is also possible to do calculations with more than one AbilitySpecial value in the same field)

So far I haven't run into any fields that don't work with this, in case something changes then I will update the post in the future.

testing signature

Comments

  • Maybe I'm just ignorant, but I'm unsure of what this could possibly be used for, unless you can put multiple % values in, like %duration * %distance or something, but even then..

    Ohgodohgodohgodohgodohgodohgodohgod wheredidalltheabilitiesgo ohgodohgodohgodohgodohgod whatdidido ohgodohgodohgod howdoicomputer

  • Posts: 96

    Added to that, in some fields, the arithmetic calculation is not possible. I stomped on this problem a while ago but did not remember which field was it.

  • Yeah thats what I meant, perhaps I should make it more clear

    As for the use, it was used in some of the spells in the Spell Library project, one of them being the Mirana Leap

    testing signature

  • edited January 2015 Posts: 1,668

    I'm unsure of what this could possibly be used for

    Well for example you can do things like "Do X Damage on a center radius and Y damage on the outside radius" without having to fake the center damage:

    "Damage"
    {
        "Target"
        {
            "Center" "CASTER"
            "Radius" "%center_radius"
            "Teams"  "DOTA_UNIT_TARGET_TEAM_ENEMY"
            "Types"  "DOTA_UNIT_TARGET_BUILDING"
        }
        "Type"   "DAMAGE_TYPE_MAGICAL"
        "Damage" "%dps_to_buildings_center - %dps_to_buildings_outside"
    }
    
    "Damage"
    {
        "Target"
        {
            "Center" "CASTER"
            "Radius" "%outside_radius"
            "Teams"  "DOTA_UNIT_TARGET_TEAM_ENEMY"
            "Types"  "DOTA_UNIT_TARGET_BUILDING"
        }
        "Type"   "DAMAGE_TYPE_MAGICAL"
        "Damage" "%dps_to_buildings_outside"
    }
    

    Isn't that beautiful?

    Without the simple arithmetic I would needed to either hardcode the first damage, or make a different tooltip just for that.

    It's also useful for doing small damage intervals, like say you have an effect that does %damage_per_second but you want this damage to be split in 0.1 sec intervals:

    "ThinkInterval" "0.1"
    "OnIntervalThink"
    {
        "Damage"
        {
            "Target" "TARGET"
            "Type"   "DAMAGE_TYPE_MAGICAL"
            "Damage" "%damage_per_second / 10"
        }
    }
    

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||

  • Ok yea that's actually really cool, not sure why I didn't think of either of those. Awesome!

    Ohgodohgodohgodohgodohgodohgodohgod wheredidalltheabilitiesgo ohgodohgodohgodohgodohgod whatdidido ohgodohgodohgod howdoicomputer

  • Posts: 6

    I have a question about random values, frostivus uses question marks to specify randomness (like this: "?50 750") ; is that something we can use ? or do they parse the string afterwards in lua and adapt from there ?

  • edited January 2015 Posts: 1,668

    Yes, "Damage" "? 20 100" (not sure if the space is needed) will do Random Damage on that interval

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||

  • On the topic of random damage, is it possible to set certain "jumps" in random damage? Like instead of any number between 10-100 it could be like one of the following "10, 20, 30, 40, 50, 60, 70, 80, 90, 100" kinda like how Chaos Knight's stun works. Because most of the time when I set a spell to deal random damage it sits around the 50% mark and almost never deviates more than 25 or so damage.

    Ohgodohgodohgodohgodohgodohgodohgod wheredidalltheabilitiesgo ohgodohgodohgodohgodohgod whatdidido ohgodohgodohgod howdoicomputer

  • Posts: 17

    Can we do vector arithmetic with keyvalues as well? If so, how?

    For example, in case I want a spell to fire twice - once at "POINT" (target point), then again 200 units to the right of "POINT", would this be feasible?

  • edited January 2015 Posts: 1,668

    @Davoness: You could add some "Random" { "OnSuccess" { ..do damage random.. } "OnFailure" { ..do damage random.. } } to get more control from it

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||

  • Posts: 96

    @d2imba, I don't think that works with only KV since vector arithmetic has multiple things to work with such as rotation, multiple axes, etc. For complicated stuffs, I do not really suggest to do it in KV only since it's gonna be way harder to debug/fix.

  • Posts: 17

    @kritth yep, I agree! was just taking a shot at the dark here, but thanks! :)

  • Posts: 1,668

    Leaving a note here: inside the Properties values, arithmetic with special values is not possible. This has no effect:

    "Properties"
    {
        "MODIFIER_PROPERTY_ATTACKSPEED_BONUS_CONSTANT" "%bonus_attack_speed / 2"
    }
    

    Same with minus, plus or multiplications.

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||

  • @Noya Yea I figured that out the hard way... Tried to make something similar to CM's Aura that gave double the mana regen to the owner and ended up with no mana regen at all. :tears:

    Ohgodohgodohgodohgodohgodohgodohgod wheredidalltheabilitiesgo ohgodohgodohgodohgodohgod whatdidido ohgodohgodohgod howdoicomputer

  • KV arithmetic seems to be broken. I keep getting console errors: "CalculateExpression failed with expression %range / %projectile_speed"

  • Yes. Its broken for now. I hope they fix it.

  • Posts: 19

    seems still broken