• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Damage Help

Status
Not open for further replies.
Level 7
Joined
Nov 4, 2006
Messages
153
So um...I'm just trying to learn JASS and I can't seem to do anything correctly! First attempt at JASS. Anyways, I am trying to make a sort of damage system type thing. It will go like this (it is similar to some of my maps if you have seen any of them):

- when unit tries to attack a unit, it will create a trigger called "Damage Trigger"
- the "Damage Trigger" will remain in play so that I may use it for special passive effects or something
- when the unit that was attacked is killed, the "Damage Trigger" is destroyed for that unit so there's not a whole bunch at once

I am probably doing everything incorrectly, so any feedback is welcome, except comments like "freaking dumba$$" :). Hope to learn a lot...or at least retain some knowledge. Btw, using NewGen but trying to do regular JASS first (I don't know the difference yet).

JASS:
globals
    group Ug_DamageSys //This will be used to hold units that "have" the DamageTrigger. They are removed from it once they die.
endglobals

function AttackTrigger_Conditions takes nothing returns boolean
    return (IsUnitInGroup(GetTriggerUnit(), Ug_DamageSys) == false) and (GetUnitAbilityLevel(GetAttacker(), 'A002') > 0) //Checks that the unit does not already have the DamageTrigger AND at least has the ability "Roots Passive"
endfunction

function DamageTrigger_Conditions takes nothing returns boolean
    return true //Just returns true...Do I need to put this function at all? - I will try removing it later
endfunction

function DeathTrigger_Conditions takes nothing returns boolean
    return true //Same as above
endfunction

function DeathTrigger_Actions takes nothing returns nothing
    local unit AttackedUnit = GetTriggerUnit()
    // local string message = "The Death Trigger is Working"
    // local force players = GetPlayersAll()
    
    call GroupRemoveUnit(Ug_DamageSys, AttackedUnit) //Unit died, so it no longer has the DamageTrigger
    // call DisplayTextToForce( players, message )
    // set players = null
    // set message = null
    set AttackedUnit = null
endfunction

function DamageTrigger_Actions takes nothing returns nothing
    local unit AttackedUnit = GetTriggerUnit()
    local unit AttackingUnit = GetEventDamageSource()
    local trigger DamageTrigger = GetTriggeringTrigger()
    
    local string message = "The Damage Trigger is Working"
    local force players = GetPlayersAll()
    
    call DisplayTextToForce( players, message )
    call UnitDamageTarget(AttackingUnit, AttackedUnit, GetEventDamage(), true, false, ATTACK_TYPE_CHAOS, DAMAGE_TYPE_UNIVERSAL, null)
    set DamageTrigger = null
    set AttackingUnit = null
    set AttackedUnit = null
    
    set players = null
    set message = null
endfunction

function AttackTrigger_Actions takes nothing returns nothing
    local unit AttackingUnit = GetAttacker() //We store the attacking unit
    local unit AttackedUnit = GetTriggerUnit() //We store the attacked unit - this one will take the damage
    local trigger DamageTrigger = CreateTrigger() //We create the damage trigger
    local trigger DeathTrigger = CreateTrigger()
    call TriggerRegisterUnitEvent(DamageTrigger, AttackedUnit, EVENT_UNIT_DAMAGED)
    call TriggerAddCondition(DamageTrigger, Condition(function DamageTrigger_Conditions))
    call TriggerAddAction(DamageTrigger, function DamageTrigger_Actions)
    call GroupAddUnit(Ug_DamageSys, AttackedUnit)
    set DamageTrigger = null
    
    call TriggerRegisterDeathEvent(DeathTrigger, AttackedUnit)
    call TriggerAddCondition(DeathTrigger, Condition( function DeathTrigger_Conditions))
    call TriggerAddAction( DeathTrigger, function DeathTrigger_Actions)
    set DeathTrigger = null
    
    set AttackingUnit = null
    set AttackedUnit = null
endfunction

function InitTrig_AttackDamageDetection takes nothing returns nothing
    local trigger AttackTrigger = CreateTrigger()

    call TriggerRegisterAnyUnitEventBJ(AttackTrigger, EVENT_PLAYER_UNIT_ATTACKED)
    call TriggerAddCondition(AttackTrigger, Condition( function AttackTrigger_Conditions))
    call TriggerAddAction(AttackTrigger, function AttackTrigger_Actions)
    set AttackTrigger = null
endfunction
 
Level 8
Joined
Aug 4, 2006
Messages
357
reza sword is correct. all global variables should have the prefix "udg_".

ok so i'm not really sure what you're trying to do with your triggers, but i'll try to help nonetheless.

1. you should get rid of those conditions that just return true. triggers are not required to have conditions. also remove the lines that add the conditions to the triggers.
2. you can put the attack trigger condition inside an if statement, in AttackTrigger_Actions to make it more efficient.
i fixed up your AttackTrigger_Actions trigger a bit:
JASS:
function AttackTrigger_Actions takes nothing returns nothing
    local unit AttackingUnit = GetAttacker()
    local unit AttackedUnit = GetTriggerUnit()
    local trigger DamageTrigger //i don't initialize these triggers unless the condition is true (see below)
    local trigger DeathTrigger
    if(IsUnitInGroup(AttackedUnit, udg_DamageSys) == false and GetUnitAbilityLevel(AttackingUnit, 'A002') > 0)then  //here's your condition in the form of an if statement, using the local variables. this way, you don't have to do more than one call of GetTriggerUnit or GetAttacker
        set DamageTrigger = CreateTrigger()
        call TriggerRegisterUnitEvent(DamageTrigger, AttackedUnit, EVENT_UNIT_DAMAGED)
        call TriggerAddAction(DamageTrigger, function DamageTrigger_Actions)
        call GroupAddUnit(udg_DamageSys, AttackedUnit)
        set DamageTrigger = null
    
        set DeathTrigger = CreateTrigger()
        call TriggerRegisterDeathEvent(DeathTrigger, AttackedUnit)
        call TriggerAddAction( DeathTrigger, function DeathTrigger_Actions)
        set DeathTrigger = null
    endif
    set AttackingUnit = null
    set AttackedUnit = null
endfunction
 
Status
Not open for further replies.
Top