啊丹 发表于 2006-3-28 15:46:24

[薪野][面向句柄的局部变量]

正文

大家都知道普通T里有个action可以把一个整数指定给一个单位/物品, 如
Unit - Set the custom value of (UnitA) to 0
需要的时候再取出来.

而接下来这个方法可以把一个整数,一个实数甚至一个句柄(handle)指定给一个句柄! 句柄有很多子类型, 包括比如单位(unit),玩家(player),区域(region),T(trigger)等等, 也就是说这里你可以"把一个整数指定给一个T", 或者"把一个T指定给一个单位", 或者"把一个玩家指定给一个区域", 方便函数间传递. 相当有用吧?

准备工作
1, 需要在地图自定义脚本里按顺序加入以下几个函数










function H2I takes handle h returns integer
return h
return 0
endfunction

//**********************************************************
//They are like custom values but for any handle, and with any label, to use them you require:
//
//http://www.wc3sear.ch/index.php?p=JASS&ID=252 (H2I function)
//
//and a global gamecache variable called handlevars

//==================================================================================================
function LocalVars takes nothing returns gamecache
if udg_handlevars==null then
set udg_handlevars=InitGameCache("jasslocalvars.w3v")
endif
return udg_handlevars
endfunction

//==================================================================================================
function SetHandleInt takes handle subject, string name, integer value returns nothing
if value==0 then
call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)
else
call StoreInteger(LocalVars(), I2S(H2I(subject)), name, value)
endif
endfunction

//==================================================================================================
function SetHandleReal takes handle subject, string name, real value returns nothing
if value==0 then
call FlushStoredReal(LocalVars(), I2S(H2I(subject)), name)
else
call StoreReal(LocalVars(), I2S(H2I(subject)), name, value)
endif
endfunction

//==================================================================================================
function SetHandleHandle takes handle subject, string name, handle value returns nothing
call SetHandleInt( subject, name, H2I(value) )
endfunction

//==================================================================================================
function GetHandleInt takes handle subject, string name returns integer
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
endfunction

//==================================================================================================
function GetHandleReal takes handle subject, string name returns real
return GetStoredReal(LocalVars(), I2S(H2I(subject)), name)
endfunction

//==================================================================================================
function GetHandleHandle takes handle subject, string name returns handle
return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
return null
endfunction

//==================================================================================================
function FlushHandleLocals takes handle subject returns nothing
call FlushStoredMission(LocalVars(), I2S(H2I(subject)) )
endfunction
要把句柄还原为其子类型, 你还要相应的函数. 比如将句柄还原为单位, 需要
function H2U takes handle h returns unit
return h
endfunction

将句柄转为T
function H2T takes handle h returns trigger
return h
endfunction




以次类推


2, 在编辑器里建一个类型为gamecache的变量: handlevars


3, ok, 万事具备, 想要指定, 比如把一个T(句柄)指定给一个单位(句柄):
call SetHandleHandle(那个单位,"取个名字标识",那个T)

要用到的时候就把这个T取出来:
GetHandleHandle(那个单位,"名字标识")

然后把取出的句柄还原为T:
H2T(GetHandleHandle(那个单位,"名字标识"))

比如你有个trigger局部变量叫temp_trigger, 把那个T取出来赋值给它(前提你在头顶加了H2T这个函数):
set temp_trigger = H2T(GetHandleHandle(那个单位,"名字标识"))

然后记得打扫清理寄主句柄
call FlushHandleLocals(那个单位)

把一个实数指定给一个T, 当然就:
call SetHandleReal(那个T,"取个名字标识",那个T)

取出来时:
set temp_real = GetHandleReal(那个T,"名字标识")

用完了清理
call FlushHandleLocals(那个T)


例子就不举了, 反正特方便. 没这个东东我图里的许多技能都不好做(然而即使用了也还是很麻烦-.-), 衷心希望大家好好利用! 欢迎提出各种创意用法 :)



附录
A. 句柄子类型参考
http://jass.sourceforge.net/doc/api/index.shtml

The End. 有错漏还请补充指教.



WE Forever!
页: [1]
查看完整版本: [薪野][面向句柄的局部变量]