找回密码
 点一下
查看: 4533|回复: 0

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

  [复制链接]
发表于 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, 需要在地图自定义脚本里按顺序加入以下几个函数





  1. function H2I takes handle h returns integer
  2. return h
  3. return 0
  4. endfunction
  5. //**********************************************************
  6. //They are like custom values but for any handle, and with any label, to use them you require:
  7. //
  8. //http://www.wc3sear.ch/index.php?p=JASS&ID=252 (H2I function)
  9. //
  10. //and a global gamecache variable called handlevars
  11. //==================================================================================================
  12. function LocalVars takes nothing returns gamecache
  13. if udg_handlevars==null then
  14. set udg_handlevars=InitGameCache("jasslocalvars.w3v")
  15. endif
  16. return udg_handlevars
  17. endfunction
  18. //==================================================================================================
  19. function SetHandleInt takes handle subject, string name, integer value returns nothing
  20. if value==0 then
  21. call FlushStoredInteger(LocalVars(),I2S(H2I(subject)),name)
  22. else
  23. call StoreInteger(LocalVars(), I2S(H2I(subject)), name, value)
  24. endif
  25. endfunction
  26. //==================================================================================================
  27. function SetHandleReal takes handle subject, string name, real value returns nothing
  28. if value==0 then
  29. call FlushStoredReal(LocalVars(), I2S(H2I(subject)), name)
  30. else
  31. call StoreReal(LocalVars(), I2S(H2I(subject)), name, value)
  32. endif
  33. endfunction
  34. //==================================================================================================
  35. function SetHandleHandle takes handle subject, string name, handle value returns nothing
  36. call SetHandleInt( subject, name, H2I(value) )
  37. endfunction
  38. //==================================================================================================
  39. function GetHandleInt takes handle subject, string name returns integer
  40. return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
  41. endfunction
  42. //==================================================================================================
  43. function GetHandleReal takes handle subject, string name returns real
  44. return GetStoredReal(LocalVars(), I2S(H2I(subject)), name)
  45. endfunction
  46. //==================================================================================================
  47. function GetHandleHandle takes handle subject, string name returns handle
  48. return GetStoredInteger(LocalVars(), I2S(H2I(subject)), name)
  49. return null
  50. endfunction
  51. //==================================================================================================
  52. function FlushHandleLocals takes handle subject returns nothing
  53. call FlushStoredMission(LocalVars(), I2S(H2I(subject)) )
  54. endfunction
  55. 要把句柄还原为其子类型, 你还要相应的函数. 比如将句柄还原为单位, 需要
  56. function H2U takes handle h returns unit
  57. return h
  58. endfunction
  59. 将句柄转为T
  60. function H2T takes handle h returns trigger
  61. return h
  62. 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!
您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-4-25 18:57 , Processed in 0.080552 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表