[SOLVED] trying to call a lua function from one file to another

edited July 2017 in Questions

I have lua function in a subdirectory in a file called npc_upgrades.lua which goes as follows:

function TowerUpgrade( keys )
    local caster = keys.caster
    print( "calling TowerUpgrade" )

I am trying to call the above function from a lua function from the parent directory as follows:

LinkLuaModifier("modifier_extended_tower_buffs_base", "hero/npc_upgrades", LUA_MODIFIER_MOTION_NONE)

However TowerUpgrade is never called as: print( "calling TowerUpgrade" ) never prints . What am I doing wrong?


  • edited July 2017 Posts: 146

    Sorry for the double post, thought I solved it but did not. still an ongoing issue.

  • did you require your file?

    require( "hero/npc_upgrades" )

  • Posts: 146


    Thanks for the quick reply. Actually I did not, but now that I included it still did not work. I am calling

    LinkLuaModifier("modifier_extended_tower_buffs_base", "hero/npc_upgrades", LUA_MODIFIER_MOTION_NONE)

    from an OnAllPlayersLoaded function. Strange.

  • edited July 2017 Posts: 272

    You don't need require. Your function is not tied to any event. That's why its never called.

    What are you trying to do anyway?

    If you apply this modifier: modifier_extended_tower_buffs_base somewhere. Your npc_upgrades.lua should contain something like this:

    if modifier_extended_tower_buffs_base == nil then
        modifier_extended_tower_buffs_base = class({})
    function modifier_extended_tower_buffs_base:OnCreated(params)
       local unit_that_has_a_modifier = self:GetParent()
       local caster = self:GetCaster()
       print( "modifier is created/applied" )

    This is just an example that will print the text when the modifier is applied to the unit. I don't know what you really want. Why use modifier_lua? You need to check everytime which functions can be used on server and/or client.

  • edited July 2017 Posts: 146


    I am guessing as you say, its not an event, its just a function. What i'm trying to do is simply call the function, however I am not sure how to call it because:

    function TowerUpgrade( keys )
        local caster = keys.caster
        print( "calling TowerUpgrade" )

    has an argument ( keys ) and i'm not sure what to pass in to satisfy the keys argument requirement when I call it.

  • edited July 2017 Posts: 16

    You just need to pass a table with a caster field.

    local my_table = {caster= my_caster}
  • Posts: 272


    i'm trying to do is simply call the function

    Where are you trying to call your function TowerUpgrade? This can easily be solved if you place your function in the right .lua file and with right arguments. If you just need the caster, table is not needed.

    And I ask again, what are you trying to do exactly? I am asking because it looks like you are over-complicating stuff and doing something wrong. You don't have to tell me if its a secret or something, but with vague answers like this I cannot truly help.

  • edited July 2017 Posts: 146

    I am trying to call it from my addon_game_mode.lua. Full repository can be found here.

    The file that has the function the hero/npc_upgrades.lua file which is in subdirectory hero of the addon_game_mode.lua file. What I am trying to do is upgrade my towers health and damage, though I have not found a way to do the latter specifically. I am using imba code but unlike the original I want this to be called as soon as the game is loaded. Below is the full function of the tower upgrade code:

    function TowerUpgrade( keys )
        local caster = keys.caster
        local ability = keys.ability
        local ability_level = ability:GetLevel() - 1
        local modifier_buffs = keys.modifier_buffs
        print( "calling TowerUpgrade" )
        -- Parameters
        local base_health_per_tier = ability:GetLevelSpecialValueFor("base_health_per_tier", ability_level) * TOWER_POWER_FACTOR
        -- Calculate tower tier
        --  Arkatakor
        --local tower_tier_multiplier = 0
        local tower_tier_multiplier = 1
        --  Arkatakor
        --if string.find(caster:GetUnitName(), "tower1") then   
        --  return nil
        if string.find(caster:GetUnitName(), "tower2") then
            tower_tier_multiplier = 1
        elseif string.find(caster:GetUnitName(), "tower3") then
            tower_tier_multiplier = 2
        elseif string.find(caster:GetUnitName(), "tower4") then
            tower_tier_multiplier = 3
        -- Adjust health
        SetCreatureHealth(caster, caster:GetMaxHealth() + base_health_per_tier * tower_tier_multiplier, true)
        -- Adjust damage/armor/attack speed
        AddStacks(ability, caster, caster, modifier_buffs, tower_tier_multiplier * TOWER_POWER_FACTOR, true)
  • Posts: 272

    Ok, answer to your main question: How to call a function inside a function thats located in another .lua file? You need require('file_location')

    Complicated solution to your problem:

    Inside extended.lua you need this:


    somewhere here.

    And here you can add this (without changing anything else):

    local your_custom_ability = building:FindAbilityByName("ability_you_want_to_add")
    local table = {}
    table.caster = building
    table.ability = your_custom_ability
    table.modifier_buffs = your_custom_modifier
    TowerUpgrade( table )

    If ability_you_want_to_add is actually extended_tower_buffs. Then you need to change that extended_tower_buffs ability in npc_abilities_custom.txt too (by deleting OnCreated block).

    Simpler solution (without changing much) if you didn't change extended_tower_buffs ability in npc_abilities_custom.txt already, notice:

          "ScriptFile"      "hero/npc_upgrades.lua"
          "Function"        "TowerUpgrade"
          "modifier_buffs"      "modifier_extended_tower_buffs_stack"

    inside a passive modifier of that ability. This means TowerUpgrade function will be activated when a tower acquires this ability. So simply you can just add this:

    local tower_upgrade_ability = building:FindAbilityByName("extended_tower_buffs")

    here and tower upgrade (health and damage) should work just like you wanted.

  • edited July 2017 Posts: 146


    I see you have taken the time to really look through the repository and what I am trying to achieve. I am especially grateful that you showed me exactly where to put in the code to invoke the tower upgrade function. I chose your second (simpler) solution which now works.

    Prior to posting here, I had been studying the framework and was trying to understand how TowerUpgrade got invoked via the npc_abilities_custom.txt file. It seemed it used to be invoked upon game start (counter reaches 0:00), so I wanted to invoke it manually. Now with this change there is no need for that as it is invoked upon game load and not on game start which was exactly what I was out after.

    Thanks again for your detailed, non-assumptions approach to your explanations :)

  • Posts: 272

    You are welcome man. I am glad I could help.