函数系统的模块调用
小编温馨提醒:本站只提供游戏介绍,下载游戏推荐89游戏,89游戏提供真人恋爱/绅士游戏/3A单机游戏大全,点我立即前往》》》绅士游戏下载专区
对于一个完整的命令系统而言,模块一般可以分为三类:对执行顺序先后有要求的高频模块、对执行顺序先后无要求的高频模块、非高频模块。在函数系统中,我们同样可以将模块分成这三类。为了方便后续讲解。我们作这样的设定:
将 system:_main设为 glf ,并称之为主进程或者主时钟
对于上面讲到的三类模块,我们通过三种不同的方式去调用。
对执行顺序先后有要求的高频模块,在主进程中按照需要的顺序排列好来调用。对执行顺序先后没有要求的高频模块,在主进程中可以比较随意放置位置,但是一般不会考虑优先执行。特别地,如果这个模块是针对每一个玩家独立执行的,可以使用进度系统中的"tick"触发器来调用,而不需要放在主进程中。仅在特定情况下触发的非高频模块,在主进程中调用,但是辅以execute、scoreboard和选择器参数去控制其在合适的时候被调用,这里的选择器,包括了在1.12 pre-4中新增的if/unless的部分。
非高频模块在特定条件下激活,也在很大程度上减少了模块中大量重复出现execute的现象,并完全杜绝了超长的Conditional链,因为function中并不直接支持Conditional。不直接支持,说明可以间接支持,对吧。我们来看一个例子。
假设有红蓝两队,在开始前考虑到互殴问题不进行分队,而是采用挂tag的方式。
红队以tag=redTeam为标记,蓝队则以tag=blueTeam为标记,准备观战的玩家以tag=specTeam为标记
当玩家站在相应区域(红蓝两队的所有玩家还需要选择了职业)添加Ready的标记,视为准备就绪。
如果玩家不在相应区域时就移除Ready的标记。
选择了职业的玩家,其记分板项selectClass数值大于等于1
全部玩家准备就绪后,游戏进入倒计时,倒计时结束时游戏开始
倒计时未结束,有玩家脱离准备就绪的状态,则倒计时中断
条件比较多,我们先来看看怎么写这个模块,再进行分析。在这里,我们准备了一个名为gameStat的aec实体作为标记,所有游戏进程会以tag或者score的形式挂载到该实体上。请看指令部分
execute @p[tag=redTeam,score_selectClass_min=1] ~ ~ ~ execute @p[tag=blueTeam,score_selectClass_min=1] ~ ~ ~ scoreboard players tag @e[type=area_effect_cloud,name=gameStat,tag=notGaming] add allReady
execute @p[tag=!Ready,m=2] ~ ~ ~ scoreboard players tag @e[name=gameStat,type=area_effect_cloud,tag=notGaming] remove allReady
execute @p[tag=!Ready,m=2] ~ ~ ~ execute @e[name=gameStat,type=area_effect_cloud,tag=notGaming] ~ ~ ~ execute @s[tag=!allReady,score_waitTime_min=1] ~ ~ ~ title @a clear
execute @p[tag=!Ready,m=2] ~ ~ ~ execute @e[name=gameStat,type=area_effect_cloud,tag=notGaming] ~ ~ ~ execute @s[tag=!allReady,score_waitTime_min=1] ~ ~ ~ title @a reset
execute @p[tag=!Ready,m=2] ~ ~ ~ execute @e[name=gameStat,type=area_effect_cloud,tag=notGaming] ~ ~ ~ scoreboard players reset @s[tag=!allReady] waitTime
scoreboard players add @e[name=gameStat,tag=allReady] waitTime 1
execute @e[name=gameStat,score_waitTime=1,score_waitTime_min=1] ~ ~ ~ title @a times 10 140 10
execute @e[name=gameStat,score_waitTime=1,score_waitTime_min=1] ~ ~ ~ title @a subtitle [{"color":"aqua","text":"请玩家站在准备区域不要离开"}]
execute @e[name=gameStat,score_waitTime=1,score_waitTime_min=1] ~ ~ ~ title @a title [{"color":"yellow","text":"游戏即将开始"}]
execute @e[name=gameStat,score_waitTime=40,score_waitTime_min=40] ~ ~ ~ title @a subtitle [{"color":"aqua","text":"3"}]
execute @e[name=gameStat,score_waitTime=40,score_waitTime_min=40] ~ ~ ~ execute @a ~ ~ ~ playsound block.note.pling voice @p ~ ~ ~ 1 0
execute @e[name=gameStat,score_waitTime=60,score_waitTime_min=60] ~ ~ ~ title @a subtitle [{"color":"aqua","text":"2"}]
execute @e[name=gameStat,score_waitTime=60,score_waitTime_min=60] ~ ~ ~ execute @a ~ ~ ~ playsound block.note.pling voice @p ~ ~ ~ 1 0
execute @e[name=gameStat,score_waitTime=80,score_waitTime_min=80] ~ ~ ~ title @a subtitle [{"color":"aqua","text":"1"}]
execute @e[name=gameStat,score_waitTime=80,score_waitTime_min=80] ~ ~ ~ execute @a ~ ~ ~ playsound block.note.pling voice @p ~ ~ ~ 1 0
execute @e[name=gameStat,score_waitTime_min=100] ~ ~ ~ title @a times 10 30 10
execute @e[name=gameStat,score_waitTime_min=100] ~ ~ ~ title @a title [{"color":"gold","text":"游戏开始"}]
scoreboard players set @e[name=gameStat,type=area_effect_cloud,score_waitTime_min=100] gameStat 1
scoreboard players reset @e[name=gameStat,score_gameStat_min=1,score_gameStat=1] waitTime
scoreboard players tag @e[name=gameStat,score_gameStat_min=1,score_gameStat=1] remove allReady
execute @e[type=area_effect_cloud,name=gameStat,score_gameStat_min=1,score_gameStat=1] ~ ~ ~ function system:StartGame
接下来我们来慢慢分析。
首先是开始的条件。有红蓝两队,那么这两队都肯定需要有人,才能够开始,考虑到同一选择器中不能重复使用tag的参数,我们保留了区分队伍的参数,而不是区分是否准备就绪的参数。因此,第一条指令的意思是,当存在选了职业并选红队的玩家以及选了职业并选蓝队的玩家,我们给中心实体加上allReady这个标记,以表明可能满足开始条件。
至于满足条件吗?如果有未准备就绪的玩家,就说明不满足,那我们就让一个没有准备就绪的玩家来去掉allReady这个标记好了。
对于3~5行,我们放后面点讲。先看后面。满足开始条件以后,我们会给中心实体加分(使用waitTime这个记分板项),在第一刻加分后出现提示文字提示准备开始,然后进入循环计时,最后计时满了,调用system:startgame这个函数来开始游戏(这里不是例子的部分,不作说明)。
那么回过头来看3~5行,这里明显是打断的部分。打断,就是要清掉提示文字、重置计时器。如果此时都还没有进行过加分,那么我们就不必进行那三条指令,因此可以看到中间有个选择器里有score_waitTime_min=1的参数加以限制。
重点来了,我们看到这3条指令前面相当长一串execute是重复的。因为在以前用cb写的时候,这里我使用了Conditional,而现在函数不直接支持Conditional,所以我用了一大堆execute,但是这里我们可以稍作修改,对不对?请看下面
execute @p[tag=redTeam,score_selectClass_min=1] ~ ~ ~ execute @p[tag=blueTeam,score_selectClass_min=1] ~ ~ ~ scoreboard players tag @e[type=area_effect_cloud,name=gameStat,tag=notGaming] add allReady
execute @p[tag=!Ready,m=2] ~ ~ ~ scoreboard players tag @e[name=gameStat,type=area_effect_cloud,tag=notGaming] remove allReady
execute @p[tag=!Ready,m=2] ~ ~ ~ execute @e[name=gameStat,type=area_effect_cloud,tag=notGaming] ~ ~ ~ execute @s[tag=!allReady,score_waitTime_min=1] ~ ~ ~ function system:cond_breakstartcount
scoreboard players add @e[name=gameStat,tag=allReady] waitTime 1
execute @e[name=gameStat,score_waitTime=1,score_waitTime_min=1] ~ ~ ~ title @a times 10 140 10
execute @e[name=gameStat,score_waitTime=1,score_waitTime_min=1] ~ ~ ~ title @a subtitle [{"color":"aqua","text":"请玩家站在准备区域不要离开"}]
execute @e[name=gameStat,score_waitTime=1,score_waitTime_min=1] ~ ~ ~ title @a title [{"color":"yellow","text":"游戏即将开始"}]
execute @e[name=gameStat,score_waitTime=40,score_waitTime_min=40] ~ ~ ~ title @a subtitle [{"color":"aqua","text":"3"}]
execute @e[name=gameStat,score_waitTime=40,score_waitTime_min=40] ~ ~ ~ execute @a ~ ~ ~ playsound block.note.pling voice @p ~ ~ ~ 1 0
execute @e[name=gameStat,score_waitTime=60,score_waitTime_min=60] ~ ~ ~ title @a subtitle [{"color":"aqua","text":"2"}]
execute @e[name=gameStat,score_waitTime=60,score_waitTime_min=60] ~ ~ ~ execute @a ~ ~ ~ playsound block.note.pling voice @p ~ ~ ~ 1 0
execute @e[name=gameStat,score_waitTime=80,score_waitTime_min=80] ~ ~ ~ title @a subtitle [{"color":"aqua","text":"1"}]
execute @e[name=gameStat,score_waitTime=80,score_waitTime_min=80] ~ ~ ~ execute @a ~ ~ ~ playsound block.note.pling voice @p ~ ~ ~ 1 0
execute @e[name=gameStat,score_waitTime_min=100] ~ ~ ~ title @a times 10 30 10
execute @e[name=gameStat,score_waitTime_min=100] ~ ~ ~ title @a title [{"color":"gold","text":"游戏开始"}]
scoreboard players set @e[name=gameStat,type=area_effect_cloud,score_waitTime_min=100] gameStat 1
scoreboard players reset @e[name=gameStat,score_gameStat_min=1,score_gameStat=1] waitTime
scoreboard players tag @e[name=gameStat,score_gameStat_min=1,score_gameStat=1] remove allReady
execute @e[type=area_effect_cloud,name=gameStat,score_gameStat_min=1,score_gameStat=1] ~ ~ ~ function system:StartGame
system:cond_breakstartcount.mcfunction
title @a clear
title @a reset
scoreboard players reset @s waitTime
虽然这个独立出来的子模块只有3条指令,但是如果分离出来的是30条而不是3条呢?能够节省多少功夫想必不需要我解释了吧?
以上是关于函数系统模块调用的部分,当中有提到使用进度系统来调用部分独立模块,我们接下来来讲这一部分。
函数系统与进度系统的联动
advancement,亦简称adv,目前wiki翻译叫进度。这里就不多作介绍了。在17w17b中MOJANG允许进度返回指令作为达成进度的奖励,让不少玩家发现了新大陆。随后在17w18b中,MOJANG进一步完善进度系统,使其可以完全独立于命令方块而建立起一个命令系统;在1.12 pre1中,MOJANG又作出了修改,将进度系统中的命令部分拿出来做成了如今的函数系统。
但是这并不意味着进度系统就不可以参与到命令系统中来,因为如今的进度系统可以返回函数作为达成进度的奖励。
相信很多人已经知道进度系统的结构了,但仍有相当一部分朋友还没有了解,在这里我们不妨来温习一下。
自定义的进度,所有文件都保存在存档目录/data/advancements/下,在这里新建的文件夹同样都称为命名空间,命名空间下存放各种进度文件。进度文件使用 json 格式。这里展示一个用于进度命令系统的例子
所涉及的两个文件分别是data/advancements/system/HelloTitle.json和data/functions/system/HelloTitle.mcfunction,这里进度和函数都用同样的命名空间和文件名方便记忆和管理,可以看到函数文件是 .mcfunction,而进度文件是 .json
system:HelloTitle.json
{
"criteria":{
"custom_name":{
"trigger":"minecraft:tick"
}
},
"rewards":{
"function":"system:hellotitle"
}
}
system:HelloTitle.mcfunction
#revoke adv,用于下次再激活
advancement revoke @s only system:hellotitle
#命令部分
scoreboard objectives add helloTitle stat.leaveGame
scoreboard players tag @s[tag=HelloTitle,score_helloTitle_min=1] remove HelloTitle
tellraw @s[tag=!HelloTitle] ["",{"text":"Hello ","color":"yellow"},{"selector":"@s"},{"text":"! Welcome to Minecraft!","color":"yellow"}]
scoreboard players tag @s[tag=!HelloTitle] add HelloTitle
scoreboard players reset @s[score_helloTitle_min=1] helloTitle
这个进度会在下一个游戏刻达成,对象是全体在线玩家,达成进度后会执行HelloTitle.mcfunction中的指令。其实现的效果是,当玩家进入这个世界时,会在聊天框看见问候语(其他人看不到)。
可以看到,相比于以前命令方块高频,这里采用了进度系统的 tick 触发器和@s选择器。如果单纯用命令方块高频或者函数系统,那么只需要这样
scoreboard objectives add helloTitle stat.leaveGame
scoreboard players tag @a[tag=HelloTitle,score_helloTitle_min=1] remove HelloTitle
execute @a[tag=!HelloTitle] ~ ~ ~ tellraw @s ["",{"text":"Hello ","color":"yellow"},{"selector":"@s"},{"text":"! Welcome to Minecraft!","color":"yellow"}]
scoreboard players tag @a[tag=!HelloTitle] add HelloTitle
scoreboard players reset @a[score_helloTitle_min=1] helloTitle
区别就是选择器上的不一样。如果大家觉得进度系统很麻烦,可以不去使用,但是接下来我们会看到一个使用进度系统的其他触发器来调用函数的例子。例如,要让所有冒险模式玩家入水即死。
rules:DieInWater.json
{
"criteria":{
"1":{
"trigger":"enter_block",
"condition":{
"block":"water"
}
}
},
"rewards":{
"function":"rules:dieinwater"
}
}
rules:DieInWater.mcfunction
#revoke
advancement revoke @s only rules:dieinwater
#commands
scoreboard players tag @p[m=2,r=0] add waterKill
execute @s[tag=waterKill] ~ ~ ~ tellraw @a [{"selector":"@s"},{"color":"white","text":" 被水淹没了"}]
execute @s[tag=waterKill] ~ ~ ~ gamerule showDeathMessages false
kill @s[tag=waterKill]
execute @s[tag=waterKill] ~ ~ ~ gamerule showDeathMessages true
scoreboard players tag @s[tag=waterKill] remove waterKill
当玩家踏入水中时,我们要给玩家加上一个tag,然后杀掉他。至于为什么用@p而不用@s呢?因为@p不能选中死人,而@s可以,如果不想看到聊天框刷屏,就不要选择用@s。
以上是利用进度系统的 enter_block(玩家进入方块) 这一触发器来实现落水即死功能的,如果单纯依靠函数,不依靠进度系统去实现的话,可以这样写
rules:DieInWater_FUNCONLY.mcfunction
execute @a[m=2] ~ ~ ~ detect ~ ~ ~ water -1 scoreboard players tag @p[r=0] add waterKill
execute @a[tag=waterKill] ~ ~ ~ tellraw @a [{"selector":"@s"},{"color":"white","text":" 被水淹没了"}]
execute @a[tag=waterKill] ~ ~ ~ gamerule showDeathMessages false
kill @a[tag=waterKill]
execute @a[tag=waterKill] ~ ~ ~ gamerule showDeathMessages true
scoreboard players tag @a[tag=waterKill] remove waterKill
然后将这个函数扔进主进程中高频执行即可。
我们讲完了函数系统与进度系统的联动部分。道理而言已经讲完了函数系统的基础使用,那么在最后,我们来聊聊函数系统与命令方块系统的对比吧,看看它们各自的优缺点。