• 🏆 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!

Timer arrays and Hashtables

Status
Not open for further replies.
Level 1
Joined
May 23, 2019
Messages
2
Hi. I'm trying to use a timer array as explained in this post. The trigger I've came up with is supposed to print the texts "1", "2" and "3". But it's only printing "1". I can't understand why it doesn't work properly. Would you mind telling me what's going on? This is how I coded it:

  • Initialization
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Hashtable - Create a hashtable
      • Set hashtable = (Last created hashtable)
      • For each (Integer index) from 1 to 3, do (Actions)
        • Loop - Actions
          • Trigger - Add to TimerExpires <gen> the event (Time - timer[index] expires)
          • Custom script: set udg_timerHandle = udg_timer[udg_index]
          • Hashtable - Save index as 0 of (Key timerHandle) in hashtable
  • TimerStarts
    • Events
      • Player - Player 1 (Red) types a chat message containing Start as An exact match
    • Conditions
    • Actions
      • Countdown Timer - Start timer[1] as a One-shot timer that will expire in 5.00 seconds
      • Countdown Timer - Start timer[2] as a One-shot timer that will expire in 5.00 seconds
      • Countdown Timer - Start timer[3] as a One-shot timer that will expire in 5.00 seconds
  • TimerExpires
    • Events
    • Conditions
    • Actions
      • Set index = (Load 0 of (Key (Expiring timer)) from hashtable)
      • Game - Display to (All players) the text: (String(index))
      • Hashtable - Clear all child hashtables of child (Key (Expiring timer)) in hashtable
 
Level 13
Joined
May 10, 2009
Messages
868
My guess is that you probably forgot to increase the timer array size in the variable editor, from 1, to 3.

upload_2019-5-24_0-11-3.png


Timers must be created first in order to be used. By default, the editor will automatically tell the game to create and assign a timer to each timer-declared variable while the map is being loaded. If you leave the array size as 1, the editor will only call CreateTimer() function twice - for both 0 and 1 indices. Be wary, because this only applies to the GUI Variables.

For example:
upload_2019-5-24_0-12-26.png

JASS:
// This is generated by the editor automatically

//***************************************************************************
//*
//*  Global Variables
//*
//***************************************************************************


function InitGlobals takes nothing returns nothing
    local integer i= 0
    set i=0
    loop
        exitwhen ( i > 1 )
        set udg_timer[i]=CreateTimer()
        set i=i + 1
    endloop

    set udg_i=0
endfunction
Now, increasing array size to 3:
JASS:
//***************************************************************************
//*
//*  Global Variables
//*
//***************************************************************************


function InitGlobals takes nothing returns nothing
    local integer i= 0
    set i=0
    loop
        exitwhen ( i > 3 )
        set udg_timer[i]=CreateTimer()
        set i=i + 1
    endloop

    set udg_i=0
endfunction
However, do note that some data types (agents/handles) are not initialized by default by the editor/game. The hash table is one of them.

upload_2019-5-24_0-22-47.png


Note how the "Initial Value" field says "None".

In this case, the editor makes use of a global variable which stores the last created hash table. That means when using "Hashtable - Create Hashtable" function, a new hash table object will be assigned to the blizzard-declared variable (last created hashtable).

  • Hashtable - Create a hashtable
->
JASS:
function InitHashtableBJ takes nothing returns hashtable
    set bj_lastCreatedHashtable = InitHashtable()
    return bj_lastCreatedHashtable
endfunction
  • Set xxx = (Last created hashtable)
JASS:
set udg_xxx = GetLastCreatedHashtableBJ()

//====

function GetLastCreatedHashtableBJ takes nothing returns hashtable
    return bj_lastCreatedHashtable
endfunction

Also, instead, you could simply add these custom scripts at the top of your loop:
  • Custom script: if udg_timer[udg_index] == null then
  • Custom script: set udg_timer[udg_index]=CreateTimer()
  • Custom script: endif
But yeah... that would just make your code be a bit harder to be read. I, personally, would just increase the array size in the variable editor. hehe
 
Last edited:
Status
Not open for further replies.
Top