找回密码
 点一下
查看: 3004|回复: 14

学习GameCache的问题

[复制链接]
发表于 2009-9-15 16:35:53 | 显示全部楼层 |阅读模式
[jass]//缓存创建
function CacheValue takes nothing returns gamecache
  return InitGameCache("WOW8")
  endfunction

//句柄转换
function H2I takes handle h returns integer
  return h
  return 0
  endfunction
 
function I2H takes integer i returns handle
  return i
  return null
  endfunction
 
function I2U takes integer i returns unit
  return i
  return null
  endfunction
 
//缓存操作
function FlushCache takes integer i, string s returns nothing
  call FlushStoredInteger(CacheValue(),I2S(i),s)   //清除缓存
  endfunction

function setint takes integer i, string s, integer int returns nothing
  call StoreInteger(CacheValue(),I2S(i),s,int)   //保存整数
  endfunction   
 
function setunit takes integer i, string s, unit u returns nothing
  call StoreInteger(CacheValue(),I2S(i),s,H2I(u))   //保存单位
  endfunction
 
function getint takes integer i, string s returns integer
  return GetStoredInteger(CacheValue(),I2S(i),s)   //获取缓存内的整数
  endfunction
 
function getunit takes integer i, string s returns unit
  return I2U(GetStoredInteger(CacheValue(),I2S(i),s))   //获取缓存内的单位
  endfunction
 
function DT takes nothing returns nothing
  local location p
  local unit u
  set p = GetRectCenter(GetPlayableMapRect())   //获取地图中心点
  call CreateNUnitsAtLoc(1,'hfoo',Player(0),p,bj_UNIT_FACING) //在地图中心给玩家1创建一个农民,面对默认角度
  set u = GetLastCreatedUnit()  //设置u为最后创建的单位
  set p = null  //清除点p
  call setunit(H2I(gg_trg_display),I2S(u),u)   //将单位u载入缓存
  call DisplayTextToPlayer(Player(0),0,0,UnitId2StringBJ(GetUnitTypeId(getunit(H2I(gg_trg_display)),I2S(u))))  //对玩家显示单位的名字
  call FlushCache(H2I(gg_trg_display),I2S(u))   //清除缓存
  set u = null  //清除单位变量u
  endfunction



function display takes nothing returns nothing
  local trigger gg_trg_display
  set gg_trg_display = CreateTrigger()  //创建触发器
  call TriggerRegisterPlayerChatEvent(gg_trg_display,Player(0),"display",true)  //创建玩家输入匹配事件
  call TriggerAddAction(gg_trg_display,function DT)  //给触发器gg_trg_display创建动作,运行DT
[/jass]

缓存的存储与取出 有问题 不知道问题出在哪- -
是 H2I(gg_trg_display)么?
怎么解决呢?

还有call TriggerAddAction(gg_trg_display,function DT)
这句哪里语法错误?
发表于 2009-9-15 16:41:29 | 显示全部楼层
好亮啊。
回复

使用道具 举报

发表于 2009-9-15 18:13:15 | 显示全部楼层
[jass]globals
    gamecache GC
endglobals

function CacheValue takes nothing returns gamecache
    if GC == null then
        call FlushGameCache( InitGameCache("WOW8.w3v") )
        set GC = InitGameCache("WOW8.w3v")
        return GC
    else
        return GC
    endif
endfunction
[/jass]
这样………………这是使用GC最基本的常识………………
回复

使用道具 举报

 楼主| 发表于 2009-9-15 19:05:58 | 显示全部楼层
globals 好像是 vj才能用吧- -
jass里面如何解决我的上述问题呢- -...
还有..谁顺手给我个vj的资料吧- -
比如怎么把 vj 写进地图- -vj的学习教程~``
回复

使用道具 举报

发表于 2009-9-15 19:41:20 | 显示全部楼层
……………………你这个人………………
globals
直接写在地图头文件里………………
回复

使用道具 举报

 楼主| 发表于 2009-9-15 20:33:10 | 显示全部楼层
郁闷- -...刚在群里问 globals- -..
别人说 不能直接在触发器里写- -...
回复

使用道具 举报

发表于 2009-9-15 20:39:09 | 显示全部楼层
直接定义全局变量就好了啊,触发编辑器定义变量不用教的吧
记住这样定义的全局变量有udg_前缀就好了
回复

使用道具 举报

发表于 2009-9-15 22:04:51 | 显示全部楼层
引用第2楼血戮魔动冰于2009-09-15 18:13发表的  :
[jass]globals
    gamecache GC
endglobals

function CacheValue takes nothing returns gamecache
.......
我重来不用这样的方法……
因为不会……
存储系统基本上不怎么用GC
直接自己写HT了……
也用过没效率的TL……
回复

使用道具 举报

 楼主| 发表于 2009-9-16 13:51:51 | 显示全部楼层
[jass]
globals
  gamecache GC
  trigger gg_trg_display
  endglobals
//缓存创建
function CacheValue takes nothing returns gamecache
  if GC == null then
  call FlushGameCache(InitGameCache("WOW8.w3v"))
  set GC = InitGameCache("WOW8.w3v")
  return GC
  else
  return GC
  endif
  endfunction

//句柄转换
function H2I takes handle h returns integer
  return h
  return 0
  endfunction
  
function I2H takes integer i returns handle
  return i
  return null
  endfunction
  
function I2U takes integer i returns unit
  return i
  return null
  endfunction
  
//缓存操作
function FlushCache takes integer i, string s returns nothing
  call FlushStoredInteger(CacheValue(),I2S(i),s)   //清除缓存
  endfunction

function setint takes integer i, string s, integer int returns nothing
  call StoreInteger(CacheValue(),I2S(i),s,int)   //保存整数
  endfunction   
  
function setunit takes integer i, string s, unit u returns nothing
  call StoreInteger(CacheValue(),I2S(i),s,H2I(u))   //保存单位
  endfunction
  
function getint takes integer i, string s returns integer
  return GetStoredInteger(CacheValue(),I2S(i),s)   //获取缓存内的整数
  endfunction
  
function getunit takes integer i, string s returns unit
  return I2U(GetStoredInteger(CacheValue(),I2S(i),s))   //获取缓存内的单位
  endfunction
  
function DT takes nothing returns nothing
  local location p
  local unit u
  set p = GetRectCenter(GetPlayableMapRect())   //获取地图中心点
  call CreateNUnitsAtLoc(1,'hfoo',Player(0),p,bj_UNIT_FACING) //在地图中心给玩家1创建一个农民,面对默认角度
  set u = GetLastCreatedUnit()  //设置u为最后创建的单位
  set p = null  //清除点p
  call setunit(H2I(gg_trg_display),I2S(H2I(u)),u)   //将单位u载入缓存
  call DisplayTextToPlayer(Player(0),0,0,UnitId2StringBJ(GetUnitTypeId(getunit(H2I(gg_trg_display),I2S(H2I(u))))))//对玩家显示单位的名字
  call FlushCache(H2I(gg_trg_display),I2S(H2I(u)))   //清除缓存
  set u = null  //清除单位变量u
  endfunction



function Init_display takes nothing returns nothing
  set gg_trg_display = CreateTrigger()  //创建触发器
  call TriggerRegisterPlayerChatEvent(gg_trg_display,Player(0),"display",true)  //创建玩家输入匹配事件
  call TriggerAddAction(gg_trg_display,function DT)
  endfunction
[/jass]
为什么没效果呢- -郁闷
回复

使用道具 举报

 楼主| 发表于 2009-9-16 16:12:40 | 显示全部楼层
- -我知道了- -因为是 自己写的 触发器- -
所以 没有 在 main里运行- -
所以 这个 触发器没有作用- -

请问一下,不导出.jass的话
如何让这个 触发器注册呢?
回复

使用道具 举报

 楼主| 发表于 2009-9-17 10:43:18 | 显示全部楼层
找到答案,因为 GC 没有 赋初始值 导致 if GC == null 造成死循环跳出
[jass]
globals
  gamecache GC
  endglobals
//缓存创建
function CacheValue takes nothing returns gamecache
  if GC == null then
  call FlushGameCache(InitGameCache("WOW8.w3v"))
  set GC = InitGameCache("WOW8.w3v")
  return GC
  else
  return GC
  endif
  endfunction

//句柄转换
function H2I takes handle h returns integer
  return h
  return 0
  endfunction
  
function I2H takes integer i returns handle
  return i
  return null
  endfunction
  
function I2U takes integer i returns unit
  return i
  return null
  endfunction
  
//缓存操作
function FlushCache takes integer i, string s returns nothing
  call FlushStoredInteger(CacheValue(),I2S(i),s)   //清除缓存
  endfunction

function setint takes handle i, string s, integer int returns nothing
  call StoreInteger(CacheValue(),I2S(H2I(i)),s,int)   //保存整数
  endfunction  
  
function setunit takes handle i, string s, unit u returns nothing
  call StoreInteger(CacheValue(),I2S(H2I(i)),s,H2I(u))   //保存单位
  endfunction
  
function getint takes handle i, string s returns integer
  return GetStoredInteger(CacheValue(),I2S(H2I(i)),s)   //获取缓存内的整数
  endfunction
  
function getunit takes handle i, string s returns unit
  return I2U(GetStoredInteger(CacheValue(),I2S(H2I(i)),s))   //获取缓存内的单位
  endfunction
  
function DT takes nothing returns nothing
  local location p
  local unit u
  set p = GetRectCenter(GetPlayableMapRect())   //获取地图中心点
  call CreateUnitAtLoc(Player(0),'hfoo',p,bj_UNIT_FACING) //在地图中心给玩家1创建一个农民,面对默认角度
  set u = GetLastCreatedUnit()  //设置u为最后创建的单位
  set p = null  //清除点p
  call setunit(u,"aa",u)
  call DisplayTextToPlayer(Player(0),0,0,"hello world")
  set u = null  //清除单位变量u
  endfunction



function InitTrig_display takes nothing returns nothing
  set gg_trg_display = CreateTrigger()  //创建触发器
  call TriggerRegisterPlayerChatEvent(gg_trg_display,Player(0),"display",true)  //创建玩家输入匹配事件
  call TriggerAddAction(gg_trg_display,function DT)
  endfunction
[/jass]
通过导出.j文件解决了globals问题- -...

但是 触发执行到call setunit(u,"aa",u)这里后面就不执行了- -
DisplayTextToPlayer()
就不显示了...
set u 应该没问题- -
那么我想 应该是 call setunit出现了问题- -
不知道是 call  StoreInteger出现了问题还是CacheValue()出现了问题....求证
回复

使用道具 举报

发表于 2009-9-17 13:42:21 | 显示全部楼层
1.检测u是否为null。
2.试着把handle i 换成unit i。
3.其余问题自己想……暂时没看出来……
回复

使用道具 举报

发表于 2009-9-17 13:47:40 | 显示全部楼层
gamecache GC => gamecache GC = null

有这样试过么?
调用未初始化的变量似乎会导致执行终止得
回复

使用道具 举报

 楼主| 发表于 2009-9-17 16:02:27 | 显示全部楼层
[jass]
globals
  gamecache GC = null
  endglobals
//缓存创建
function CacheValue takes nothing returns gamecache
  if GC == null then
  call FlushGameCache(InitGameCache("WOW8.w3v"))
  set GC = InitGameCache("WOW8.w3v")
  endif
  return GC
  endfunction

//句柄转换
function H2I takes handle h returns integer
  return h
  return 0
  endfunction
  
function I2H takes integer i returns handle
  return i
  return null
  endfunction
  
function I2U takes integer i returns unit
  return i
  return null
  endfunction
  
//缓存操作
function FlushCache takes integer i, string s returns nothing
  call FlushStoredInteger(CacheValue(),I2S(i),s)   //清除缓存
  endfunction

function setint takes handle i, string s, integer int returns nothing
  call StoreInteger(CacheValue(),I2S(H2I(i)),s,int)   //保存整数
  endfunction  
  
function setunit takes handle i, string s, unit u returns nothing
  call StoreInteger(CacheValue(),I2S(H2I(i)),s,H2I(u))   //保存单位
  endfunction
  
function getint takes handle i, string s returns integer
  return GetStoredInteger(CacheValue(),I2S(H2I(i)),s)   //获取缓存内的整数
  endfunction
  
function getunit takes handle i, string s returns unit
  call DisplayTextToPlayer(Player(0),0,0,"1")
  return I2U(GetStoredInteger(CacheValue(),I2S(H2I(i)),s))   //获取缓存内的单位
  endfunction
  
function DT takes nothing returns nothing
  local location p
  local unit u
  set p = GetRectCenter(GetPlayableMapRect())   //获取地图中心点
  //在地图中心给玩家1创建一个农民,面对默认角度
  set u = CreateUnitAtLoc(Player(0),'hfoo',p,bj_UNIT_FACING)  //设置u为最后创建的单位
  set p = null  //清除点p
  call setunit(u,"aa",u)
  call DisplayTextToPlayer(Player(0),0,0,UnitId2StringBJ(GetUnitTypeId(getunit(u,"aa"))))//对玩家显示单位的名字
  call DisplayTextToPlayer(Player(0),0,0,"hello world")
  set u = null  //清除单位变量u
  endfunction



function InitTrig_display takes nothing returns nothing
  set gg_trg_display = CreateTrigger()  //创建触发器
  call TriggerRegisterPlayerChatEvent(gg_trg_display,Player(0),"display",true)  //创建玩家输入匹配事件
  call TriggerAddAction(gg_trg_display,function DT)
  endfunction
[/jass]

最终代码- -...解决了- -
原来
set u = LastCreateUnit()
无法获取啊- -
set u = CreateUnitAtLoc()
才可以啊...终于结束了这个问题- -
从中学到- -
globals 里 声明变量
导出 .j 脚本查看运行原理
set u = CreateUnitAtLoc()
是无法获取的- -
变量赋初始值会导致跳出运行如
[jass]
globals
  gamecache CG
endglobals

function test takes nothing returns noting
  if CG == null then
   .....
  endif
  endfunction
[/jass]

在 if CG == null 处 出错
回复

使用道具 举报

 楼主| 发表于 2009-9-17 16:51:31 | 显示全部楼层
我自己写的触发器 如何让他 运行起来呢- -
虽然注册了事件
但是 还是没有用- -
导出 .j
看与其他的函数也没声明区别- -也都在 globals里声明了- -

InitCustomTriggers()里运行了...
我导出.j
直接在 触发器里编写 如何让 这个触发器能够运行呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

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

GMT+8, 2024-5-4 02:23 , Processed in 0.037298 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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