[ 編集このページを編集する ]

トップ > MAP作成 > Jass Script > ライブラリ集 > Handles


ライブラリ

//hashtableはgamecacheと同じような働きを持ちますが、値の参照にStringではなくintegerを用いることで処理がより安全で軽量になります。
globals
    hashtable HashT = InitHashtable()
endglobals

//ライブラリ
library Handles
    //GetHandleIdは以前のH2Iと同じ働きを持ちます。
    //StringHashはS2Iと同じですが、これはあらゆる文字列が扱えます。
    
    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けて、Integerを保存
    function SetHandleInteger takes handle whichhandle, string whichVariable, integer value returns nothing
       call SaveInteger(HashT,GetHandleId(whichhandle),StringHash(whichVariable), value)
    endfunction

    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けられた、Integerを取得
    function GetHandleInteger takes handle whichhandle, string whichVariable returns integer
       return LoadInteger(HashT,GetHandleId(whichhandle),StringHash(whichVariable))
    endfunction

    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けて、Realを保存
    function SetHandleReal takes handle whichhandle, string whichVariable, real value returns nothing
       call SaveReal(HashT, GetHandleId(whichhandle),StringHash(whichVariable),value)
    endfunction

    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けられた、Realを取得
    function GetHandleReal takes handle whichhandle, string whichVariable returns real
       return LoadReal(HashT, GetHandleId(whichhandle),StringHash(whichVariable))
    endfunction

    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けて、Booleanを保存
    function SetHandleBoolean takes handle whichhandle, string whichVariable, boolean value returns nothing
       call SaveBoolean(HashT, GetHandleId(whichhandle),StringHash(whichVariable),value)
    endfunction

    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けられた、Booleanを取得
    function GetHandleBoolean takes handle whichhandle, string whichVariable returns boolean
       return LoadBoolean(HashT, GetHandleId(whichhandle),StringHash(whichVariable))
    endfunction

    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けて、文字列(string)を保存
    function SetHandleString takes handle whichhandle, string whichVariable, string value returns nothing
       call SaveStr(HashT, GetHandleId(whichhandle),StringHash(whichVariable), value)
    endfunction

    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けられた、文字列(string)を取得
    function GetHandleString takes handle whichhandle, string whichVariable returns string
       return LoadStr(HashT,GetHandleId(whichhandle),StringHash(whichVariable))
    endfunction
 
    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けられた、任意の値(Agent)を保存
    function SetHandleAgent takes handle whichHandle, string whichVariable, agent value returns nothing
       call SaveAgentHandle(HashT, GetHandleId(whichHandle),StringHash(whichVariable),value)
    endfunction

    //*******************************************************
    // タイマー以外のHandle型変数を取得する関数を作るときは
    //
    // 1,  ここの関数を、このライブラリ内にコピペ
    // 2,  関数名を適当に変更(たとえば、GetHandleUnit,GetUnitHandle)
    // 3,  関数内のAPIを変更 (たとえば、LoadTimerHandle  ->  LoadUnitHandle)これは、common.jに記載されている
    // 4,  戻り値を変更   (たとえば、returns timer    ->  returns unit)
    //
    // と、するように。
    //
 
    // whichHandle(任意のhandle型変数) + whichVariable(任意のキーワード) に関連付けられた、timerを取得
    function GetHandleTimer takes handle whichHandle, string whichVariable returns timer
       return LoadTimerHandle(HashT,GetHandleId(whichHandle),StringHash(whichVariable ))
    endfunction

    //*******************************************************

    function FlushHandle takes handle whichHandle returns nothing
       call FlushChildHashtable(HashT, GetHandleId(whichHandle))
    endfunction
endlibrary

使い方のサンプル

ユニットやタイマーに、データを関連付けて使うことが多いだろう。

たとえば、ユニットであれば、ノックバックを利用する場合の移動速度などなど。あらゆる情報を関連付けられる。

また、タイマーであれば、TimerStart 関数を使うときに、引数として利用できる。

  • function Test を実行後、10秒でユニットを殺す例
  • Handlesのライブラリに追加する項目
        function GetHandleUnit takes handle subject,string name returns unit
           return GetUnitHandle(GetHandleId(subject) , StringHash(name))
        endfunction
  • メイン。function test がまず実行される。
        // TimerStart で呼ばれる関数なので、引数を設定できない。
        function KillVictim takes nothing returns nothing
    
            //GetHandleUnitを使うために、CountDownTimerを取得する
            local timer CountDownTimer = GetExpiredTimer()
    
            //VICTIM をHashtableから取得
            local unit VICTIM          = GetHandleUnit(CountDownTimer, "UnitToKill")
    
    
                //VICTIMを殺す
                call KillUnit(VICTIM)
    
            //使い終わったので、CountDownTimerに関連付けられた全てのHashtableを破棄。
            call FlushHandle(CountDownTimer)
    
            //使い終わったタイマーを削除。削除の前にポーズをする。
            call PauseTimer(CountDownTimer)
            call DestroyTimer(CountDownTimer)  
     
            //変数のnull化
            set CountDownTimer = null
            set VICTIM         = null
        endfunction
    
        function test takes unit VICTIM returns nothing
            //タイマーを作成
            local timer CountDownTimer = CreateTimer()
    
            // CountDownTimer に、VICTIM を、"UnitToKill"というキーワードをつけて保存
            call SetHandleAgent(CountDownTimer,"UnitToKill", VICTIM)
            //タイマーを起動
            call TimerStart(CountDownTimer, 10.0, false, function KillVictim)
    
            //変数のnull化
            set CountDownTimer = null
        endfunction

変数の保存

上の例にならって説明する。

書式
call SetHandleAgent( handle AnyHandle1 , string Key , handle AnyHandle2 )

AnyHandle1 に対して、Key というキーワードをつけて、 AnyHandle2 を保存。

保存したい値が整数の場合は SetHandleInteger、実数の場合は SetHandleReal、文字列の場合は SetHandleString、Boolean型の場合は SetHandleBoolean を使う。それ以外のものは"変数名"の部分を保存したい変数にあわせて変える(UnitならSetHandleUnit,TimerならSetHandleTimer等)。ver1.24bからは、すべてSetHandleAgentを使う。

Key には、わかりやすいキーワードをつけるといい。たとえば

    call SetHandleInteger( UnitA, "GoldCost", 1200)

という風に。

変数の取得

上の例にならって説明する。

書式
call GetHandleTimer( handle AnyHandle1 , string Key)

AnyHandle1 に対して、Key というキーワードで保存してある timer を取得する。

取得したい値が整数の場合は GetHandleInteger、実数の場合は GetHandleReal、文字列の場合は GetHandleString、Boolean型の場合は GetHandleBoolean を使う。それ以外のものはすべて handle型変数 なので、 GetHandle〜 が使える。

値が存在しない場合、nullが返される。

上の例なら

    set i = GetHandleInteger( UnitA, "GoldCost")

という風に。i の値には 1200 が代入される。

より賢い使い方

構造体を使えば、より書式を簡略化できる。

構造体の正体はIntegerなので、変数をそのまま SetHandleInteger するだけで、関連項目全てを記憶できるほか、値を変更したときも SetHandleInteger をやり直す必要がない。

詳しくは、構造体のページで。