• 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] Structs, Methods, and Timers?

Status
Not open for further replies.
Level 4
Joined
Jun 8, 2007
Messages
89
EDIT: It appears to be working with the code I have posted currently; however, the spell cast seems to have a delay to it. Is there any way to fix this?

So, I am attempting to make a library for using dummy casters for my map, however I seem to have run into an issue.
I have units that spawn on timers, and they are successfully removed; however, they never cast their spells. I haven't really worked much with dummy casters before, and don't really know what I'm doing wrong. Any help would be greatly appreciated.

JASS:
library DummyCasters initializer init

globals
    private constant real       DUMMY_DURATION          = 5.0
    private constant real       DUMMY_TIMER_INTERVAL    = 0.25

    private DummyCaster array   DummyCasters
    private integer             ActiveDummyCasters
    private timer               DummyCasterTimer
    private integer             index                   = 0
endglobals

private function DummyCaster_Timer takes nothing returns nothing
    local DummyCaster   dc
    set index   = 0
    
    loop
        // loop through all dummy casters
        exitwhen (index >= ActiveDummyCasters)
        // retrieve the dummy information
        set dc          = DummyCasters[index]
        
        // decrement the caster duration
        call dc.decrement()
        
        // if the life has expired
        if (dc.duration <= 0) then
            call dc.destroy()
        endif
        
        set index = index + 1
    endloop
    
    // pause timer if no active casters exist
    if (ActiveDummyCasters == 0) then
        call PauseTimer(DummyCasterTimer)
    endif
endfunction

struct DummyCaster
    unit    dummy
    real    duration
    
    // initialize the struct
    static method create takes nothing returns DummyCaster
        local DummyCaster dc                    = DummyCaster.allocate()
        
        // initialize vars
        set dc.dummy                            = CreateUnit(Player(11), 'h002', 0, 0, bj_UNIT_FACING)
        set dc.duration                         = DUMMY_DURATION
        
        // start the timer if paused
        if (ActiveDummyCasters == 0) then
            call TimerStart(DummyCasterTimer, DUMMY_TIMER_INTERVAL, true, function DummyCaster_Timer)
        endif
        
        // add to the array
        set DummyCasters[ActiveDummyCasters]    = dc
        set ActiveDummyCasters                  = ActiveDummyCasters + 1
        
        call BJDebugMsg("DC.create()")
        
        return dc
    endmethod
    
    method cast takes unit caster, unit target, integer abilityId, integer abilityLevel, string order returns nothing
        // move the dummy to the source caster
        call SetUnitPosition(.dummy, GetUnitX(caster), GetUnitY(caster))
        
        // add the ability
        call UnitAddAbility(.dummy, abilityId)
        // level up the ability
        if (abilityLevel > 0) then
            call SetUnitAbilityLevel(.dummy, abilityId, abilityLevel)
        endif
        
        // issue the spell cast order
        call IssueTargetOrder(.dummy, order, target)
    endmethod
    
    method decrement takes nothing returns nothing
        set .duration   = .duration - DUMMY_TIMER_INTERVAL
    endmethod
    
    method onDestroy takes nothing returns nothing
        set DummyCasters[index]                 = DummyCasters[ActiveDummyCasters - 1]
        set ActiveDummyCasters                  = ActiveDummyCasters - 1
    
        // garbage collect
        call RemoveUnit(.dummy)
        set .dummy  = null
        call BJDebugMsg("dc.onDestroy()")
    endmethod
endstruct

//===========================================================================
function init takes nothing returns nothing
    set ActiveDummyCasters  = 0
    set DummyCasterTimer    = CreateTimer()
endfunction

endlibrary
 
Level 4
Joined
Jun 8, 2007
Messages
89
me neither but why not using the xe system from Vexorian its nice at is and deals with all what u need from dummys

Yeah, idk what was up with it, but it seems to be working now...
I actually just saw that before I went to bed yesterday (the xe system that is). I don't really like using other people's systems if I can avoid it, as I like to know exactly what it is I'm doing, but it looks pretty sweet and I think I'll need to look into it, at least for ideas.

Thanks.
 
Level 8
Joined
Feb 15, 2009
Messages
463
i had the same thoughts sometime before, but then somebody showed me the advantages
I'll do it for you again:
-We have 2009 not using the Standarts uptodate is just stupid
-Allows you to write loads of shorter and cleaner codes then ever before
-Deals with loads of WE evils and goes for a workaround to avoid them

Also (not for this problem ) i'm looking forward to see everyone coding with scripts like GroupUtils and TimerUtils and systems like xe which improving your code a lot
 
Level 4
Joined
Jun 8, 2007
Messages
89
i had the same thoughts sometime before, but then somebody showed me the advantages
I'll do it for you again:
-We have 2009 not using the Standarts uptodate is just stupid
-Allows you to write loads of shorter and cleaner codes then ever before
-Deals with loads of WE evils and goes for a workaround to avoid them

Also (not for this problem ) i'm looking forward to see everyone coding with scripts like GroupUtils and TimerUtils and systems like xe which improving your code a lot

Yeah, I know what you mean, these things definitely tend to have their advantages, but I still like to try to rewrite them on my own, just so I really know what's going on, and everything is easier to remember that way. My viewpoint may change as I realize just how much code I'll be writing, but we'll see. :wink:
 
Status
Not open for further replies.
Top