Snippet Event

tooltiperror

Super Moderator
Reaction score
231
No idea, I don't see a readme or anything in the JassHelper folder, other than the LICENSE file.

Edit: Figured it out, updating now.
 

dudeim

New Member
Reaction score
22
Hey I have a little problem with attaching a struct to an event using EventReg
here is what I do:

JASS:

set t.i = 5 //just a simple integer var in the struct that I set to 5 for testing purpose
call SetEventRegData(t.er,t) //t.er is the EventReg in the struct and t is the structs var
call OnEvent.fire()

Now this is what I use to obtain the struct in the trigger that goes when the event fires
JASS:

scope test initializer tes

private function go takes nothing returns nothing
local TestStruct t = GetEventRegData(GetTriggeringEventReg()) //I think this line is wrong cus even when I do "integer i = 57" in the struct var list and leave the t.i=5 line out it says 0
    call BJDebugMsg("The integer is [" + I2S(t.i) + "]" //this displays: The Integer is [0]
endfunction

private function tes takes nothing returns nothing
local trigger t = CreateTrigger()
    call TestStruct.registerEvent(t) //teststruct is the struct I used for testing how this all works, registerEvent(t) simply registers the event to this trigger
    call TriggerAddAction(t,function go)
endfunction
endscope


What am I doing wrong?
 

dudeim

New Member
Reaction score
22
Well this was a quick example I made, but I'll give you the full code (it's a damage and crit detection system trying to expand my skills):
(There are a few structs like Unit and Playor, Unit refers to a unit's struct and Playor refers to a players struct)
system
JASS:
library DDamage requires AIDS, ZTS, Event, UnitData

globals
    Event OnDamage
    Event OnCrit
    
    boolean damageEventEnabled = true
    boolean critEventEnabled = true
    
    integer LastEventDamageIndex
//    DDamage LastDamageEvent
    
    constant integer NORMAL_DAMAGE = 0
    constant integer SPELL_DAMAGE = 1
    constant integer PURE_DAMAGE = 2
    constant integer NORMAL_DAMAGE_JASS = 3
    constant integer INPUTNAMEHERE_DAMAGE = 4
endglobals

//enable and disable the events
function enableDamage takes nothing returns nothing
        set damageEventEnabled = true
endfunction

function disableDamage takes nothing returns nothing
        set damageEventEnabled = false
endfunction

function enableCrit takes nothing returns nothing
        set critEventEnabled = true
endfunction

function disableCrit takes nothing returns nothing
        set critEventEnabled = false
endfunction

struct DDamage
/*    private boolean damageEnabled = true
    private boolean critEnabled = true */
    unit target
    real damage
    boolean crit
    unit source
    real realdamage
    real threat
    real dmgmultiplier = 1.0
    integer dtype = NORMAL_DAMAGE
    EventReg er
    integer i = 67
    
    
    //global methods like the unit used in the event
    
    method getTargetUnit takes nothing returns unit
        return .target
    endmethod
    
    method getSourceUnit takes nothing returns unit
        return .source
    endmethod
    
    method getTarget takes nothing returns Unit
        return Unit(GetUnitId(.target))
    endmethod
    
    method getSource takes nothing returns Unit
        return Unit(GetUnitId(.target))
    endmethod
    
    method getTargetOwner takes nothing returns Playor
        return Playor(GetPlayerId(GetOwningPlayer(.target)))
    endmethod
    
    method getSourceOwner takes nothing returns Playor
        return Playor(GetPlayerId(GetOwningPlayer(.target)))
    endmethod
    
    //the Damage and Crit Methods (You can use all these methods for both)
    
    //dunno if everything resets it's a just incase
    private method onDestroy takes nothing returns nothing
        set .dtype = NORMAL_DAMAGE
        set .target = null
        set .damage = 0
        set .crit = false
        set .threat = 0
        set .dmgmultiplier = 1.0
        set .er = 0
        set .source = null
    endmethod
    //creating the events
    private static method onInit takes nothing returns nothing
        set OnDamage = Event.create()
        set OnCrit = Event.create()
    endmethod
    //you can check using this if the damageevent is enabled
    method isDamageEnabled takes nothing returns boolean
        return damageEventEnabled
    endmethod
    //register the event damage using this                           
    static method registerDamage takes trigger whichtrigger returns nothing
        call OnDamage.register(whichtrigger)
    endmethod    
    //unregister the event damage using this
    static method unregisterDamage takes trigger whichtrigger returns nothing
        call OnDamage.unregister(whichtrigger)
    endmethod
    //enable the damage event for a specific instance
    //check if the crit event is enabled
    method isCritEnabled takes nothing returns boolean
        return critEventEnabled
    endmethod
    //register the crit event
    static method registerCrit takes trigger whichtrigger returns nothing
        call OnCrit.register(whichtrigger)
    endmethod    
    //unregister the crit event
    static method unregisterCrit takes trigger whichtrigger returns nothing
        call OnCrit.unregister(whichtrigger)
    endmethod
    //enables the crit event for a specific instance
    //
    method getDamageMultiplier takes nothing returns real
        return .dmgmultiplier
    endmethod
    
    method setDamageMultiplier takes real multiplier returns nothing
        set .dmgmultiplier = multiplier
    endmethod
    
    method getDamage takes nothing returns real
        return .damage
    endmethod
    
    method isCrit takes nothing returns boolean
        return .crit
    endmethod
    
    method getRealDamage takes nothing returns real
        return .realdamage
    endmethod
    
    method getThreat takes nothing returns real
        return .threat
    endmethod
    
    method getType takes nothing returns integer
        return .dtype
    endmethod
    
    method isNormal takes nothing returns boolean
        if .dtype == NORMAL_DAMAGE then
            return true
        endif
        return false
    endmethod
    
    method isJass takes nothing returns boolean
        if .dtype == NORMAL_DAMAGE_JASS then
            return true
        endif
        return false
    endmethod
    
    method isNormalJass takes nothing returns boolean
        return .isJass()
    endmethod
    
    method isSpell takes nothing returns boolean
        if .dtype == SPELL_DAMAGE then
            return true
        endif
        return false
    endmethod
    
    method isPure takes nothing returns boolean
        if .dtype == PURE_DAMAGE then
            return true
        endif
        return false
    endmethod
    
 /*   method isINPUTNAMEHERE takes nothing returns boolean
        if .dtype == INPUTNAMEHERE_DAMAGE then
            return true
        endif
        return false
    endmethod */
    
    method doDamage takes unit source, unit target, real damage, boolean applythreat, real threatbonus, real critchance, real critmultiplier returns nothing
        set .damage = damage
        set .source = source
        set .target = target
        call .normaldamage(critchance,critmultiplier,applythreat,threatbonus)
    endmethod
    
    method doSpellDamage takes unit source, unit target, real damage, boolean applythreat, real threatbonus, real critchance, real critmultiplier returns nothing
        set .damage = damage
        set .source = source
        set .target = target
        call .spelldamage(critchance,critmultiplier,applythreat,threatbonus)
    endmethod
    
        method doPureDamage takes unit source, unit target, real damage, boolean applythreat, real threatbonus, real critchance, real critmultiplier returns nothing
        set .damage = damage
        set .source = source
        set .target = target
        call .puredamage(critchance,critmultiplier,applythreat,threatbonus)
    endmethod
    
    method normaldamage takes real critchance, real critmultiplier, boolean applythreat, real threatbonus returns nothing
        local real rand = GetRandomReal(0,100)
        local Unit u = Unit(GetUnitId(.source))
            set .dtype = NORMAL_DAMAGE_JASS
            if critchance > rand then
                if critEventEnabled == true then
                    call OnCrit.fire()
                    set .crit = true
                endif
                set .damage = .damage * critmultiplier
            endif
             call UnitDamageTarget(.source,.target,.damage,false,false,ATTACK_TYPE_CHAOS,DAMAGE_TYPE_NORMAL,WEAPON_TYPE_WHOKNOWS)
            if applythreat == true then
                set .threat = .threat * u.getThreatMultiplier() + threatbonus
                call ZTS_ModifyThreat(.source, .target, .threat, true)
            endif    
        call .destroy()
    endmethod
    
    method spelldamage takes real critchance, real critmultiplier, boolean applythreat, real threatbonus returns nothing
        local real rand = GetRandomReal(0,100)
        local Unit u = Unit(GetUnitId(.source))
            set .dtype = SPELL_DAMAGE
            if critchance > rand then
                if critEventEnabled == true then
                    call OnCrit.fire()
                    set .crit = true
                endif
                set .damage = .damage * critmultiplier
            endif
            call UnitDamageTarget(.source,.target,.damage,false,false,ATTACK_TYPE_CHAOS,DAMAGE_TYPE_UNIVERSAL,WEAPON_TYPE_WHOKNOWS)
            if applythreat == true then
                set .threat = .threat * u.getThreatMultiplier() + threatbonus
                call ZTS_ModifyThreat(.source, .target, .threat, true)
            endif 
        call .destroy()    
    endmethod
    
    method puredamage takes real critchance, real critmultiplier, boolean applythreat, real threatbonus returns nothing
        local real rand = GetRandomReal(0,100)
        local Unit u = Unit(GetUnitId(.source))
            set .dtype = PURE_DAMAGE
            if critchance > rand then
                if critEventEnabled == true then
                    call OnCrit.fire()
                    set .crit = true
                endif
                set .damage = .damage * critmultiplier
            endif
            if GetWidgetLife(.source) > .damage then
                call SetWidgetLife(.source,GetWidgetLife(.source)-damage)
                if damageEventEnabled == true then
                    call OnDamage.fire()
                endif
            else    
                call SetWidgetLife(.source,GetWidgetLife(.source)-damage+1)
                call UnitDamageTarget(.source,.target,.damage,false,false,ATTACK_TYPE_CHAOS,DAMAGE_TYPE_UNIVERSAL,WEAPON_TYPE_WHOKNOWS)
            endif
            if applythreat == true then
                set .threat = .threat * u.getThreatMultiplier() + threatbonus
                call ZTS_ModifyThreat(.source, .target, .threat, true)
            endif    
        call .destroy()    
    endmethod
    
     static method onDamage takes nothing returns boolean
        local DDamage d = DDamage.create()
        local unit un = GetEventDamageSource()
        local Unit u = Unit.create(un)
        local real dmg = GetEventDamage()
     //       set d.i = 5  */
        if damageEventEnabled == true and dmg != 0 then
            set d.damage = dmg
            set d.target = GetTriggerUnit()
            set d.source = un
            set d.realdamage = dmg
            
            set d.threat = d.damage * (u.getThreatMultiplier())
            call SetEventRegData(d.er,d)
            call SetWidgetLife(d.target,GetWidgetLife(d.target)+dmg)
            call OnDamage.fire()
        endif
        set d = GetEventRegData(d.er)
      //  set LastDamageEvent = d
        
        
        return false
    endmethod
    
  /*  static method CCreate takes integer index returns DDamage
        local DDamage d = DDamage(index)
        return d
    endmethod */

endstruct


//I copied this from your DD system
private struct Detect extends array
        //! runtextmacro AIDS()
        
        private static conditionfunc COND
        private trigger t
        
        private method AIDS_onCreate takes nothing returns nothing
            set .t=CreateTrigger()
            call TriggerAddCondition(.t,.COND)
            call TriggerRegisterUnitEvent(.t,.unit,EVENT_UNIT_DAMAGED)
        endmethod
        
        private method AIDS_onDestroy takes nothing returns nothing
            call DestroyTrigger(.t)
        endmethod
        
        private static method AIDS_onInit takes nothing returns nothing
            set .COND=Condition(function DDamage.onDamage)
        endmethod
    endstruct

endlibrary

function to check if it works
JASS:
scope test initializer tes

private function go takes nothing returns nothing
//local Event evs = ev.GetTriggeringEventReg
local DDamage d = GetEventRegData(GetTriggeringEventReg())

    call BJDebugMsg("The integer is: " + I2S(d.i)) //this displays The integer is: 0


endfunction

private function crit takes nothing returns nothing
    call BJDebugMsg("A crit occured")
endfunction

private function tes takes nothing returns nothing
local trigger t = CreateTrigger()
    call DDamage.registerDamage(t)
    call TriggerAddAction(t,function go)
    set t = CreateTrigger()
    call DDamage.registerCrit(t)
    call TriggerAddAction(t,function crit)
    
endfunction

endscope

The code is probably really messy I first wanted to get the main things done before cleaning up:p
 

dudeim

New Member
Reaction score
22
In the onDamage method I use this to set the data

call SetEventRegData(d.er,d)

Shouldn't that work?

Also what do you mean by the last line? Could you give me a little example of it:p
 

dudeim

New Member
Reaction score
22
Hmmmm maybe I got it all wrong from the start, but what I'm trying to do is bind a struct's instance to a specific instance of the event so I can retrieve the values from the specific struct's instance when the event fires. (so I can get the damage dealt and all other values at the time the event fires)
Ex:
JASS:
//I got some random struct set up and an event etc..
function bla ....
//some instance of the struct is created here lets say number 276 and when this reruns there is a new instance 277
//some values I set in the structs instance
endfunction

//here this function runs when the event I set up before fires
function run ...
//here I want to retrieve the values of the structs instance number 276, when the event fires first, not 277, I want the values from 277 when it fires for instance 277
//I hope this makes it a little bit clearer
//I left most things about but imagine I made them (the event,struct,the function to trigger this trigger, which is linked to the event created etc..)
endfunction
 

dudeim

New Member
Reaction score
22
Yep like that only can I still set values to the struct stored in the EventReg?
Like when I check if I need to fire the event or not?

JASS:
function fireevent takes.....
local mystruct m = //dunno what goes here then don't think MyStruct.create works here in this case as it's already created?
if /* something for ex. dmg != 0 */ then
//and here I still want to do stuff with the struct
set m.i = GetRandomInt(1,100) //some random integer that's on the struct as example so in the later trigger actions I can retrieve the correct random value chosen here
call event.fire()
endif
 

Nestharus

o-o
Reaction score
83
Yea, I just thought you should know that I think this is done pretty badly for trigger events-
JASS:

method registerTrigger takes trigger t returns nothing
        call TriggerRegisterVariableEvent(t,SCOPE_PRIVATE+"q",EQUAL,this)
endmethod


Turning the firing method into this-
JASS:

method fire takes nothing returns nothing
        set q=0
        set q=this
        call TriggerEvaluate(e[this])
endmethod



And ofc, the variable event firing is much, much, much, much faster than what you are doing right now.


You managed to turn like a 24 line script into a 100 line script
 

tooltiperror

Super Moderator
Reaction score
231
Hey guys, let's break maps using the optimizer!
 

Magthridon96

Member
Reaction score
2
Sorry for the bump, but Frotty is on the verge of creating a new optimizer (The Froptimizer)
Nestharus' Event is perfect. I don't think it should be deprecated only because Vexorian's
Failtimizer breaks NATIVE warcraft III features.
Plus, TriggerRegisterVariableEvent only breaks with concatenation.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • jonas jonas:
    Your website isn't blocked by GFW so much is for sure :p
  • tom_mai78101 tom_mai78101:
    Do you have family in taiwan? <-- Yes.
  • jonas jonas:
    Cool, enjoy your vacation!
  • The Helper The Helper:
    Have a great vacation Tom!
  • The Helper The Helper:
    Happy Friday Night!
    +1
  • V-SNES V-SNES:
    Happy Friday Night!
    +1
  • The Helper The Helper:
    Going out of a town for the weekend will be back sunday night! Hope everyone has a great weekend!
    +2
  • The Helper The Helper:
    Happy Monday!
  • Ghan Ghan:
    Monday? Speak for yourself. :p
    +2
  • The Helper The Helper:
    Happy Taco Tuesday!
    +1
  • The Helper The Helper:
    Tacos! :)
  • The Helper The Helper:
    Check out the discord for taco pictures :) You cannot put pictures in this chat
    +1
  • The Helper The Helper:
    Damn here come the bots again - 193 online but they are totally invisible to any stats - bunch of bots!'
  • C cubanismo:
    Re: Taco Tuesday, if there were some way to share edible tacos over the internet, technology would be complete.
    +2
  • The Helper The Helper:
    One can only wish!
  • tom_mai78101 tom_mai78101:
    I'm back from Taiwan
    +1
  • The Helper The Helper:
    I am reorganizing the site I know nobody will notice but I am not done quite yet but the main forums order has been changed and there is a new news category in Other News which is all the remaining headline news stuff not categorized - Headline News is just the stuff that shows on the main page now and the news archive is off the main forums page
  • The Helper The Helper:
    and the real archive lives off of headline news

    The Helper Discord

    Staff online

    Members online

    Affiliates

    Hive Workshop NUON Dome World Editor Tutorials

    Network Sponsors

    Apex Steel Pipe - Buys and sells Steel Pipe.
    Top