• 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] timer use .?

Status
Not open for further replies.

ABM

ABM

Level 7
Joined
Jul 13, 2005
Messages
279
hi to remove some triggersleepaction i changed a little bit my functions, i wonder if i did it correctly...
i am not used to play with timers.

[Jass=]
private function Display takes player owner, integer msg returns nothing
if IsPlayerInForce(owner, udg_Team[1]) == true then
if msg == 1 then
call DisplayTimedTextToForce( bj_FORCE_ALL_PLAYERS, 6.00, ("The Gods respect the BloodSpear Tribe !! They gave power to the Totem of " + GetPlayerName(owner) + "." ))
elseif msg == 2 then
call DisplayTimedTextToForce( bj_FORCE_ALL_PLAYERS, 6.00, ( "|c009966ffOld Troll:|r |c00ffff64" + GetPlayerName(owner) + " from the Bloodspear Tribe" + " haz been stricken by lightning!|r" ))
endif
elseif IsPlayerInForce(owner, udg_Team[2]) == true then
if msg == 1 then
call DisplayTimedTextToForce( bj_FORCE_ALL_PLAYERS, 6.00, ("The Gods respect the GoldTooth Tribe !! They gave power to the Totem of " + GetPlayerName(owner) + "." ))
elseif msg == 2 then
call DisplayTimedTextToForce( bj_FORCE_ALL_PLAYERS, 6.00, ( "|c009966ffOld Troll:|r |c00ffff64" + GetPlayerName(owner) + " from the Goldtooth Tribe" + " haz been stricken by lightning!|r" ))
endif
elseif IsPlayerInForce(owner, udg_Team[3]) == true then
if msg == 1 then
call DisplayTimedTextToForce( bj_FORCE_ALL_PLAYERS, 6.00, ("The Gods respect the StoneSkin Tribe !! They gave power to the Totem of " + GetPlayerName(owner) + "." ))
elseif msg == 2 then
call DisplayTimedTextToForce( bj_FORCE_ALL_PLAYERS, 6.00, ( "|c009966ffOld Troll:|r |c00ffff64" + GetPlayerName(owner) + " from the Stoneskin Tribe" + " haz been stricken by lightning!|r" ))
endif
endif
endfunction

private function FlashUnit takes integer t returns nothing
local real x
local real y
local real dmg
local player owner
call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUTIN, 0.10, "ReplaceableTextures\\CameraMasks\\White_mask.blp", 100.00, 100.00, 90.00, 0 )
call DisplayTimedTextToPlayer( Player(0), 0, 0, 6.00, ( "Units: " + GetUnitName(ztarget[t]) ) )
set x = GetUnitX(ztarget[t])
set y = GetUnitY(ztarget[t])
set i = GetUnitTypeId(ztarget[t])
set owner = GetOwningPlayer(ztarget[t])
if i == TOTEM_ID then
call SetUnitState(ztarget[t], UNIT_STATE_MANA, 500.)
call Display(owner,1)
elseif IsUnitType(ztarget[t], UNIT_TYPE_STRUCTURE) then
set dmg = GetRandomReal(25.00, 50.00)
elseif IsUnitType(ztarget[t], UNIT_TYPE_HERO) then
set dmg = GetRandomReal(100.00, 200.00)
call Display(owner,2)
else
set dmg = GetRandomReal(50.00, 300.00)
endif
if GetUnitState(ztarget[t],UNIT_STATE_LIFE) > dmg then
call SetUnitState(ztarget[t], UNIT_STATE_LIFE, (GetUnitState(ztarget[t], UNIT_STATE_LIFE) - dmg) )
else
call KillUnit( ztarget[t] )
if IsUnitType(ztarget[t], UNIT_TYPE_HERO) then
set udg_Die_Lightning[GetPlayerId(owner)+1] = ( udg_Die_Lightning[GetPlayerId(owner)+1] + 1 )
endif
endif
set lightbolt = AddSpecialEffect( "Doodads\\Cinematic\\Lightningbolt\\Lightningbolt.mdl", x, y )
call PlaySoundBJ( gg_snd_LightningBolt1 )
call TriggerSleepAction( 0.10 )
call DestroyEffect( lightbolt )
set owner =null
endfunction

private function FlashTree takes integer t returns nothing
local real x
local real y
local unit fire
local destructable d
call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUTIN, 0.10, "ReplaceableTextures\\CameraMasks\\White_mask.blp", 100.00, 100.00, 90.00, 0 )
set x = GetDestructableX(udg_Tree[t])
set y = GetDestructableY(udg_Tree[t])
call DisableTrigger( gg_trg_DropItemDestructible__JASS )
call RemoveDestructable(udg_Tree[t])
call EnableTrigger( gg_trg_DropItemDestructible__JASS )
set d = CreateDestructable( 'NTtw', x, y, 0., 0.90, GetRandomInt(0, 9) )
call TriggerRegisterDeathEvent( gg_trg_DropItemDestructible__JASS, d )
set lightbolt = AddSpecialEffect( "Doodads\\Cinematic\\Lightningbolt\\Lightningbolt.mdl", x, y )
if GetRandomInt(1, 20) == 1 then
set fire = CreateUnit( Player(PLAYER_NEUTRAL_AGGRESSIVE), 'n00S', x, y, bj_UNIT_FACING )
call SetUnitUserData( fire, 2)
if udg_Rain == false and udg_Wind == false then
call SetUnitState(fire, UNIT_STATE_LIFE, GetRandomReal(20., 60.) )
call SetUnitAbilityLevel( fire, 'A0H7', 2 )
else
call SetUnitState(fire, UNIT_STATE_LIFE, GetRandomReal(20., 40.) )
if udg_Rain then
call SetUnitAbilityLevel( fire, 'A0H7', 1 )
else
call SetUnitAbilityLevel( fire, 'A0H7', 3 )
endif
endif
call DisplayTimedTextToForce( bj_FORCE_ALL_PLAYERS, 4.00, "|c009966ffOld Troll:|r |c00ffff64Oh, no, mon, a forest fire!|r" )
call PingMinimap( x, y, 4.00 )
endif
call PlaySoundBJ( gg_snd_LightningBolt1 )
call TriggerSleepAction( 0.10 )
call DestroyEffect( lightbolt )
set d =null
set fire =null
endfunction

private function Lightning takes integer chance, integer count returns nothing
local integer t
if ( GetRandomInt(1, chance) == 1 ) then
set t = GetRandomInt(0, count)
if GetUnitState(ztarget[t], UNIT_STATE_LIFE) > 0.00 then
call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUTIN, 0.10, "ReplaceableTextures\\CameraMasks\\White_mask.blp", 100.00, 100.00, 90.00, 0 )
call TimerStart(flashTimer, 0.2, false, function FlashUnit(t))
endif
else
set t = GetRandomInt(1, udg_Tree_count)
if GetDestructableLife(udg_Tree[t]) > 0 then
call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUTIN, 0.10, "ReplaceableTextures\\CameraMasks\\White_mask.blp", 100.00, 100.00, 90.00, 0 )
call TimerStart(flashTimer, 0.2, false, function FlashUnit(t))
endif
endif
endfunction
[/code]

the variable wich aren't in the locals are globals, ztarget (unit array) , lightbolt (effect)

before FlashUnit and FlashTree were all inside Lightning...
there was a call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUTIN, 0.10, "ReplaceableTextures\\CameraMasks\\White_mask.blp", 100.00, 100.00, 90.00, 0 )
then a wait 0.10 and again a call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUTIN, 0.10, "ReplaceableTextures\\CameraMasks\\White_mask.blp", 100.00, 100.00, 90.00, 0 )...
i added a globals timer "flashTimer" to wait 0.20 and then launch the rest of the function.

also i would like to know how to clean the timer, or can i use it many time without leak?
the timer is a single shot wich simulate a wait period. i need to use it many time, i created it once in the library globals as a privet globals timer...
 
Level 4
Joined
Jan 27, 2010
Messages
133
First of all, you cannot pass arguments to the function executed by the timer. You can use globals:

JASS:
 // WRONG: Cannot send t
call TimerStart(flashTimer, 0.2, false, function FlashUnit(t))

// RIGHT
set global_variable = t
call TimerStart(flashTimer, 0.2, false, function FlashUnit)

// Now use global_variable in FlashUnit

Secondly, I would personally use TriggerSleepAction in this case. It would give you cleaner code.

TSA is all about knowing when it's okay to use it, and what the consequences might be. TSA is inaccurate at low intervals, and does not stop when game is paused. You basically want a lightning and a little while later (~0.2s) a unit is supposed to be struck. This is an ideal case for wait! It doesn't really matter if it would happen after 0.1s or 0.3s...

Thirdly... do you ever get the feeling that you are writing the same code multiple times? In those cases there is almost always a better approach.

JASS:
    globals
        private string array teamName
    endglobals

    private function Init takes nothing returns nothing
        set teamName[1] = "BloodSpear"
        set teamName[2] = "GoldTooth"
        set teamName[3] = "StoneSkin"
    endfunction

   private function Display takes player owner, integer msg returns nothing
        local integer team
        if IsPlayerInForce(owner, udg_Team[1]) == true then
            set team=1
        elseif IsPlayerInForce(owner, udg_Team[2]) == true then
            set team=2
        elseif IsPlayerInForce(owner, udg_Team[3]) == true then
            set team=3
        else
            call BJDebugMsg("scope ??: function Display: Invalid team")
        endif

        if msg == 1 then
            call DisplayTimedTextToForce( bj_FORCE_ALL_PLAYERS, 6.00, ("The Gods respect the "+teamName[team]+" Tribe !! They gave power to the Totem of " + GetPlayerName(owner) + "." ))
        elseif msg == 2 then
            call DisplayTimedTextToForce( bj_FORCE_ALL_PLAYERS, 6.00, ( "|c009966ffOld Troll:|r |c00ffff64" + GetPlayerName(owner) + " from the + teamName[team] + " haz been struck by lightning!|r" ))
        endif
    endfunction

JASS:
 // This can be kept out of the other functions
function FlashEffect takes nothing returns nothing
        call CinematicFadeBJ( bj_CINEFADETYPE_FADEOUTIN, 0.10, "ReplaceableTextures\\CameraMasks\\White_mask.blp", 100.00, 100.00, 90.00, 0 )

        call PlaySoundBJ( gg_snd_LightningBolt1 )
        call TriggerSleepAction( 0.10 )
        call DestroyEffect( lightbolt )
endfunction
 

ABM

ABM

Level 7
Joined
Jul 13, 2005
Messages
279
ok thanks...
i finally re added the wait, and just gave up on the timer thing...
if not needed, it is too much trouble and chaos in the structure of the library...
 
Status
Not open for further replies.
Top