请选择 进入手机版 | 继续访问电脑版

 找回密码
 点一下
查看: 8229|回复: 23

关于code变量的用法

[复制链接]
发表于 2006-12-29 13:31:24 | 显示全部楼层 |阅读模式
在我的jass教程里面就说过,code是一个指向takes nothing 和returns nothing的jass函数的指针。当然指针也是一种变量哈,别糊涂了。
我们暂时把takes nothing returns nothing 的函数或code叫作函式以方便本文的叙述。
我们看一个自定义函数。
//负责运行一个函式
[jass]
function RunCodeFunction takes code s returns nothing
local trigger t=CreateTrigger()
call TriggerRegisterTimerEvent(t,0,false)
call TriggerAddAction(t,s)
endfunction
[/jass]
由于code是变量,我们就可以随时改变一个code变量的值。从而修改一个预先设置好的动作。看一个例子。
[jass]
globals
code s=null
endglobals
function code1 takes nothing returns nothing
endfunction
function code2 takes nothing returns nothing
endfunction
function actionFunc takes nothing returns nothing
call RunCodeFunction(s)
endfunction
function controlFunc takes nothing returns nothing
set s=function code2
endfunction
function theMain takes nothing returns nothing
local trigger t=null
set t=CreateTrigger()
call TriggerRegisterTimerEvent(t,5,true)//每隔5秒运行actionFunc.
call TriggerAddAction(t,function actionFunc)
set s=function code1
set t=CreateTrigger()
call TriggerRegisterTimerEvent(t,8,false)//8秒后运行controlFunc.
call TriggerAddAction(t,function controlFunc)
endfunction
[/jass]
可以预料到的是,0s时不运行任何函式,5s时运行code1,10s时运行code2。
当然上面只是举个例子而已,我们可以写更复杂的控制系统。
例如用一个code数组做一个函式表。
[jass]
globals
code array s
endglobals
function InitGlobals takes nothing returns nothing
set s[0]=function code0
set s[1]=function code1
set s[2]=function code2
...
set s[32]=function code32
endfunction
function RandomCall takes nothing returns nothing
call RunCodeFunction(s[GetRandomInt(0,32)])
endfunction
[/jass]
怎么样,一个随机效果的技能就可以这样来写了。
关于code还有更多的用法,这里就不再一一列举了。
希望本文能起到一个抛砖引玉的作用。

评分

参与人数 1威望 +5 收起 理由
白银の游戏王 + 5

查看全部评分

发表于 2006-12-29 13:42:08 | 显示全部楼层
实际上LS的方法素对if XXX then call XXX的很大的简化
并且具有更大的灵活性...
貌似现在对code滴研究还不多...
555...精料精料...支持研究...
回复

使用道具 举报

发表于 2006-12-29 17:49:09 | 显示全部楼层

值得继续研究的东西
回复

使用道具 举报

发表于 2006-12-29 20:38:13 | 显示全部楼层
学习了
回复

使用道具 举报

发表于 2007-1-1 17:15:40 | 显示全部楼层
[jass]
function RunCodeFunction takes code s returns nothing
local trigger t=CreateTrigger()
call TriggerRegisterTimerEvent(t,0,false)
call TriggerAddAction(t,s)
endfunction
[/jass]
这种方式运行效率也太低了,
直接用TimerStart运行code不就行了
回复

使用道具 举报

 楼主| 发表于 2007-1-6 15:20:57 | 显示全部楼层
LS有理...
本文果然起到了抛砖引玉的作用。
回复

使用道具 举报

发表于 2008-2-29 21:59:10 | 显示全部楼层
可惜。。。。code变量必须指向一个无参数的函数。如果能带上参数,那就更方便,更强大了。。。就不再需要GameCache和Return Bug 了。
回复

使用道具 举报

发表于 2008-3-19 17:53:30 | 显示全部楼层
globals
code s=null
endglobals
function code1 takes nothing returns nothing
endfunction
function code2 takes nothing returns nothing
endfunction
function actionFunc takes nothing returns nothing
call RunCodeFunction(s)
endfunction
function controlFunc takes nothing returns nothing
set s=function code2
endfunction
function theMain takes nothing returns nothing
local trigger t=null
set t=CreateTrigger()
call TriggerRegisterTimerEvent(t,5,true)//每隔5秒运行actionFunc.
call TriggerAddAction(t,function actionFunc)
set s=function code1
set t=CreateTrigger()
call TriggerRegisterTimerEvent(t,8,false)//8秒后运行controlFunc.
call TriggerAddAction(t,function controlFunc)
endfunction好东西啊
回复

使用道具 举报

发表于 2008-5-16 16:45:03 | 显示全部楼层
[quote]引用第4楼Red_Wolf于2007-01-01 17:15发表的  :
这种方式运行效率也太低了,
直接用TimerStart运行code不就行了quote]

那怎么才能获得code运行后
   call PauseTimer(t)
      call DestroyTimer(t)
      set t= null
回复

使用道具 举报

发表于 2008-5-17 16:55:30 | 显示全部楼层
引用第6楼sdhexu于2008-02-29 21:59发表的  :
可惜。。。。code变量必须指向一个无参数的函数。如果能带上参数,那就更方便,更强大了。。。就不再需要GameCache和Return Bug 了。
我也有过这个美好愿望。
回复

使用道具 举报

发表于 2008-5-17 17:31:05 | 显示全部楼层
可惜不能带参数也不能返回值
回复

使用道具 举报

发表于 2008-7-12 10:53:47 | 显示全部楼层
悲剧啊
我发现WAR是不支持code array的...
保存了地图就不能读了
回复

使用道具 举报

发表于 2008-7-12 11:00:58 | 显示全部楼层
原来这个不是坟,确实是不能用 code数组的
回复

使用道具 举报

发表于 2008-7-13 10:19:08 | 显示全部楼层
lz是怎么测试的呢..
回复

使用道具 举报

发表于 2008-7-14 01:48:32 | 显示全部楼层
莫非这只是一个遐想。。。
美好的愿望。。。
回复

使用道具 举报

发表于 2008-7-14 06:37:14 | 显示全部楼层
又见挖坟
回复

使用道具 举报

发表于 2008-7-14 09:50:03 | 显示全部楼层
~~
((*^__^*) 嘻嘻……很久之后的处理)
回复

使用道具 举报

发表于 2010-3-7 10:54:19 | 显示全部楼层
LZ,CODE变量能使用数组?你也是在搞笑。
回复

使用道具 举报

发表于 2010-3-7 10:54:52 | 显示全部楼层
LZ,CODE变量能使用数组?你也是在搞笑。
回复

使用道具 举报

发表于 2010-3-7 10:55:12 | 显示全部楼层
LZ,CODE变量能使用数组?你也是在搞笑。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-17 04:47 , Processed in 0.362690 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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