Weird issue in lua script

edited February 2017 in Questions

Hey guys, i have a really weird issue that puzzles me.

First some context, what i want to achieve is that whenever a hero spawns, he gets tracked and if there was a hero already tracked before, that hero gets his track removed. The effect of this being that only the last spawned hero is tracked.

Now here is my lua OnNPCSpawned function

function COverthrowGameMode:OnNPCSpawned( event )
    local spawnedUnit = EntIndexToHScript( event.entindex )
    if spawnedUnit:IsRealHero() then

        if self.firstTrack ~= nil then
            print(self.firstTrack:GetName())
         :fire: --self.superBounty:CastAbilityOnTarget(self.firstTrack, self.remove_track, 0)
        end

        self.firstTrack = spawnedUnit

        print("hero is " .. self.firstTrack:GetName())

        self.superBounty:CastAbilityOnTarget(self.firstTrack, self.globaltrack2, 0)
         :fire: self.superBounty:CastAbilityOnTarget(self.firstTrack, self.remove_track, 0)
    end
end

self.firstTrack, is a global variable, initialized to "nil" in Initgamemode and it contains the currently tracked unit.

self.superBounty represents a custom made unit (a bounty hunter) that applies the track to the players.

self.gobaltrack2 is the tracking spell

self.remove_track is the spell that removes the track


if i run the code like this, with the untrack spell immediately following the track spell, it works. The hero is not tracked and the print statements inside the untrack vscript file, print out to the vconsole.

BUT, when i comment out the untrack that immediatelly follows the tracking spell, and uncomment the untracking spell more higher up in the code , like so

function COverthrowGameMode:OnNPCSpawned( event )
    local spawnedUnit = EntIndexToHScript( event.entindex )
    if spawnedUnit:IsRealHero() then

        if self.firstTrack ~= nil then
            print(self.firstTrack:GetName())
         :fire: self.superBounty:CastAbilityOnTarget(self.firstTrack, self.remove_track, 0)
        end

        self.firstTrack = spawnedUnit

        print("hero is " .. self.firstTrack:GetName())

        self.superBounty:CastAbilityOnTarget(self.firstTrack, self.globaltrack2, 0)
     :fire: --self.superBounty:CastAbilityOnTarget(self.firstTrack, self.remove_track, 0)
    end
end

Then it doesnt work, i.e. when i spawn another unit, the track does not go away on the previously tracked unit, and both heroes are now tracked, while if i use the 1th version, the unit is not tracked at the start (because it got removed instantly)

I just cant figure out whats going on here. Its the exact same function with the same variables, just at a later point in time.

I have done some checks on the variables (the variables are self.firstTrack and self.remove_track, and they all are what they are suposed to be)

I hope i gave a good explanation of my problem, if something is unclear i will give more information asap.

Comments

  • Posts: 125

    @fallia, I'm not sure but maybe CastAbilityOnTarget uses cast point? So spell didn't start because it is interrupted by track spell?

  • Posts: 329

    @Yunten, im not 100% sure what u mean, there are 2 possibilities

    1) u have misinterpreted my question and switched the 2 scenarios, the scenarion where the 2 functions are called right after the other, is the scenario that is working

    2) u are saying that the reason that scenario 1 "works" is because the track spell just doesnt get called, because it gets interupted by the remove track spell, and therefor the reason that the unit isnt tracked, is because the spell didnt get called in the first place.

    Note that the second case is also not possible, because in scenario 1, i dont see print statements printed out in vconsole, (because remove_track doesnt get called), while in scenario 2 i do see them. If the problem was that the 2nd cast of a spell, interupted the first call, then the remove_track spell would also be called in scenario 2, which it doesnt.

    I am sure making this complex i realize now, but thats the best i can explain my reasoning

  • Posts: 858

    Instead of casting the "remove_track" ability try removing the track modifier instead via RemoveModifierByName

    MAKE CUSTOM GAMES GREAT AGAIN

    Finished-
    kv checker - https://arhowk.github.io
    panorama video series - https://moddota.com/forums/discussion/2021/inclusive-panorama-tutorial-quest-box

    My pet project This Cursed World/Crestfallen (name pending)
    https://moddota.com/forums/discussion/986/this-cursed-world-rpg#latest

  • edited February 2017 Posts: 329

    yeah! nice, it worked, from the first try, ez pz, thanks