トップ > 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 関数を使うときに、引数として利用できる。
変数の保存上の例にならって説明する。
AnyHandle1 に対して、Key というキーワードをつけて、 AnyHandle2 を保存。 保存したい値が整数の場合は SetHandleInteger、実数の場合は SetHandleReal、文字列の場合は SetHandleString、Boolean型の場合は SetHandleBoolean を使う。それ以外のものは Key には、わかりやすいキーワードをつけるといい。たとえば call SetHandleInteger( UnitA, "GoldCost", 1200) という風に。 変数の取得上の例にならって説明する。
AnyHandle1 に対して、Key というキーワードで保存してある timer を取得する。 取得したい値が整数の場合は GetHandleInteger、実数の場合は GetHandleReal、文字列の場合は GetHandleString、Boolean型の場合は GetHandleBoolean を使う。それ以外のものはすべて handle型変数 なので、 GetHandle〜 が使える。 値が存在しない場合、nullが返される。 上の例なら set i = GetHandleInteger( UnitA, "GoldCost") という風に。i の値には 1200 が代入される。 より賢い使い方構造体を使えば、より書式を簡略化できる。 構造体の正体はIntegerなので、変数をそのまま SetHandleInteger するだけで、関連項目全てを記憶できるほか、値を変更したときも SetHandleInteger をやり直す必要がない。 詳しくは、構造体のページで。 |