• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Basics - Timers

Status
Not open for further replies.

NEL

NEL

Level 6
Joined
Mar 6, 2017
Messages
113
Part 1

Create a timer that fires a function "foo" after 2 seconds expired after map initialization.
In function "foo" the expired time must be printed on screen and the expired timer destroyed.

vJASS:
//! zinc
library Timers {
    private {
        timer Timer = CreateTimer();
       
        function foo() {
            DisplayTextToPlayer( GetLocalPlayer(), 0, 0, 
                R2S( TimerGetElapsed( Timer ) ) + " expired time."
            );
           
            DestroyTimer( Timer );
            Timer = null;
        }
       
        function onInit() {
            TimerStart( Timer, 2, false, function foo );
        }
    }
}
//! endzinc





Part 2

Following global variables must be declared and used (<type> , <name>):
  1. hashtable Hash
Create a unit on map initialization.
Create a timer on map initialiaztion.

The unit must be saved into hashtable, using the timer as ParentKey, which means that the used index must be directly related to the timer object itself.

The timer should periodically (each second) call a function "DamageUnit", which goal is to retrieve the saved unit from hashatable, and to decrease it's life by "10".

In case the unit is dead, the timer must be destroyed.

vJASS:
//! zinc
library Timers {
    private {
        timer Timer = CreateTimer();
        hashtable hash = InitHashtable();
       
        function DamageUnit() {
            integer h = GetHandleId( Timer );
            unit u = LoadUnitHandle( hash, h, 0 );
            real r = GetWidgetLife( u ) - 10;
           
            if( GetUnitState( u, UNIT_STATE_LIFE) <= 0 ) {   
                FlushChildHashtable( hash, h );
                DestroyTimer( Timer );
                Timer = null;
            } else {
                SetWidgetLife( u, r );
            }
            u = null;
        }
       
        function onInit() {
            SaveUnitHandle( hash, GetHandleId( Timer ), 0, 
                CreateUnit( Player( 0 ), 'njks', 0, 0, 0 )
            );
           
            TimerStart( Timer, 1, true, function DamageUnit );
        }
    }
}
//! endzinc
 

Attachments

  • [Crash Course] Basics - Timers.w3m
    23.8 KB · Views: 70
Status
Not open for further replies.
Top