做一段 NPC 对话
这一篇做一个对话画面:底部一个对话框,NPC 的台词一个字一个字打出来,右边几个选项,点了换下一句。做完以后用一条指令就能从 NPC 插件、任务插件、指令方块把它叫出来。
用到的图片都是云界面自带的(images/typewriter/ 和 images/main/),不用自己画。
第 1 步:先让对话框出现
在 plugins/CloudUI/screens/ 新建一个文件 tut_npc.yml,写入:
portrait:
enabled: false
camera:
animation: false
elements:
panel:
type: image
image: typewriter/panel.png
x: 222
y: 754
line:
type: text
text: '欢迎来到这里,旅人。'
font: text
size: 28
color: '#E2EEF4'
x: 304
y: 824
进游戏输入 /cui reload,等它提示重新载入完成,再输入 /cui open tut_npc。画面下方应该出现一个对话框,里面有一行字。按 F 或 Shift 关掉。
开头两段的意思:portrait.enabled: false 是不要玩家自己的角色站出来,camera.animation: false 是不要镜头飞行。两个都关掉,画面就直接盖在玩家眼前,玩家还看得到面前的 NPC。
第 2 步:让台词逐字出现
给 line 加四行:
line:
type: text
text: '欢迎来到这里,旅人。\n有什么需要帮忙的吗?'
font: text
size: 28
line-height: 44
color: '#E2EEF4'
x: 304
y: 824
type-speed: 1
type-delay: 6
type-sound: 'ui.button.click 0.12 1.8'
type-sound-every: 3
/cui reload 后再打开,台词会一个字一个字打出来,每三个字响一声。
| 键 | 说明 |
|---|---|
type-speed | 每个字隔几 tick 出现,20 tick 是一秒。1 就是一秒 20 个字,0 是整段一次显示 |
type-delay | 画面出现后等几 tick 才开始打 |
type-sound | 打字声,写成「音效 音量 音高」。不写就没有声音 |
type-sound-every | 每几个字响一声,默认 1 |
line-height | 有换行时,两行之间的距离(画布像素) |
台词里写 \n 就换行。这个对话框一行大约放 46 个中文字,最多三行,长台词自己用 \n 断开。
第 3 步:台词改成从外面送进来
台词写死在文件里,一个 NPC 就要做一个画面。改成变量,一个画面所有 NPC 都能用。
在文件最上面加一段 values:,再把 line 的 text 改成 '{line}',另外加上说话者的名牌:
values:
speaker: 村长
line: '欢迎来到这里,旅人。\n有什么需要帮忙的吗?'
nameplate:
type: image
image: typewriter/nameplate.png
x: 258
y: 728
speaker:
type: text
text: '{speaker}'
font: text
size: 28
color: '#E6D3A1'
align: center
x: 462
y: 758
values: 是没有带值时显示的默认内容。/cui reload 后,在后台或游戏里(需要 cloudui.admin 权限)试这两条指令,Steve 换成你自己的名字:
/cui open tut_npc Steve speaker=铁匠 line=要打一把新剑吗?
/cui set Steve line=材料带够了再来找我。
第一条打开画面并带上说话者和台词。第二条在画面开着时只换台词,画面不关,新的一句重新从第一个字打起。
key=value 后面没有 = 的字会接到前一个值后面,所以台词里有空格也没关系。值里写 \n 一样会换行。
第 4 步:加选项按钮
选项就是普通的按钮,点击动作用 console: 执行 cui set,把下一句台词送给点的那位玩家。在 elements: 下面加:
ask-who:
type: button
image: typewriter/choice.png
hover: typewriter/choice_hover.png
x: 1160
y: 560
hit: [1160, 560, 520, 56]
click:
left: ['console: cui set {player} speaker=村长 line=我是这座村子的村长。\n有困难尽管开口。']
ask-who-text:
type: text
text: '你是谁?'
font: text
size: 22
color: '#E2EEF4'
x: 1188
y: 578
按钮本身不写字,把一个文字元素放在按钮范围里就行。hit 是点击范围,四个数字是画布上的 x、y、宽、高,和按钮图片一样大。
要做「再见」这种说完就关的选项,在后面接 delay: 和 close:
click:
left: ['console: cui set {player} speaker=村长 line=一路顺风,旅人。', 'delay: 50', 'close']
先送出最后一句,等 50 tick(两秒半)让它打完,再关闭画面。
选项也可以直接执行别的指令,例如任务插件的接任务指令:['close', 'console: 你的任务插件指令 {player}']。close 写在前面时,后面的动作会在画面关掉之后才执行。
第 5 步:打完才出现的「继续」记号
对话框右下角放一个 ▼,台词打完才出现并上下浮动:
cont:
type: image
image: typewriter/cont.png
state: '{line.typed}'
states: {hidden: ''}
timeline: [{t: 0, y: 0, alpha: 1}, {t: 10, y: 6, alpha: 0.55}, {t: 20, y: 0, alpha: 1}]
timeline-play: '{line.typed}'
x: 1616
y: 930
{line.typed} 的意思是「名字叫 line 的那段文字打完了没有」:打字中是 hidden,打完是 on。拿它当 state,打字中图片就不显示。按钮也可以写 state: '{line.typed}',这样台词没打完时选项不出现,玩家没办法跳过。
第 6 步:让玩家打字回答
想让 NPC 问玩家一句话、玩家自己打字回答,用对话框输入。在文件最外层加一段 dialogs:,再加一个选项按钮去打开它:
dialogs:
myname:
title: 村长在问你
body: 你叫什么名字?
ok: 告诉他
inputs:
name:
label: 名字
max: 12
required: true
pattern: '[A-Za-z0-9_一-龥]{1,12}'
error: 名字只能用中文、英文、数字和下划线
actions:
- 'console: cui set {player} speaker=村长 line=原来你叫 {input.name},好名字。'
tell-name:
type: button
image: typewriter/choice.png
hover: typewriter/choice_hover.png
x: 1160
y: 628
hit: [1160, 628, 520, 56]
click:
left: ['dialog: myname']
玩家点这个选项会跳出一个输入窗口,按确定后执行 actions,里面的 {input.name} 换成玩家填的内容。
console: 是用控制台的权限执行的。上面的 pattern 把名字锁成只有中文、英文、数字和下划线,玩家就没办法多打空格或 = 去改变整条指令。没有 pattern 的输入栏不要放进 console:。
对话框输入的全部写法见 对话框输入。
第 7 步:从 NPC、任务、指令方块触发
只要那边能执行控制台指令,就能把对话叫出来。指令前面不用加 /。
Citizens 这类 NPC 插件,给 NPC 设一条点击时执行的控制台指令,玩家名字用那个插件自己的变量写法:
cui open tut_npc <player> speaker=村长 line=你好,旅人。
任务插件在某个阶段完成时执行:
cui open tut_npc %player% speaker=村长 line=干得好,这是给你的报酬。
指令方块:
cui open tut_npc @p speaker=石碑 line=此处长眠着一位无名的旅人。
画面已经开着时,再 cui open 同一个画面只会换值,不会重开。
自己写插件的话,用 API 直接带值打开,见 开发者 API。
完整文件
plugins/CloudUI/screens/tut_npc.yml,整份复制就能用:
values:
speaker: 村长
line: '欢迎来到这里,旅人。\n有什么需要帮忙的吗?'
portrait:
enabled: false
camera:
animation: false
transition:
slide-x: 0
slide-y: 24
out-ticks: 3
in-ticks: 6
dialogs:
myname:
title: 村长在问你
body: 你叫什么名字?
ok: 告诉他
inputs:
name:
label: 名字
max: 12
required: true
pattern: '[A-Za-z0-9_一-龥]{1,12}'
error: 名字只能用中文、英文、数字和下划线
actions:
- 'console: cui set {player} speaker=村长 line=原来你叫 {input.name},好名字。'
elements:
panel:
type: image
image: typewriter/panel.png
x: 222
y: 754
motion: {x: 0, y: 40}
nameplate:
type: image
image: typewriter/nameplate.png
x: 258
y: 728
motion: {x: -40, y: 0, delay: 1}
speaker:
type: text
text: '{speaker}'
font: text
size: 28
color: '#E6D3A1'
align: center
x: 462
y: 758
motion: {x: -40, y: 0, delay: 1}
line:
type: text
text: '{line}'
font: text
size: 28
line-height: 44
color: '#E2EEF4'
x: 304
y: 824
type-speed: 1
type-delay: 6
type-sound: 'ui.button.click 0.12 1.8'
type-sound-every: 3
motion: {x: 0, y: 20, delay: 2}
cont:
type: image
image: typewriter/cont.png
state: '{line.typed}'
states: {hidden: ''}
timeline: [{t: 0, y: 0, alpha: 1}, {t: 10, y: 6, alpha: 0.55}, {t: 20, y: 0, alpha: 1}]
timeline-play: '{line.typed}'
x: 1616
y: 930
ask-who:
type: button
image: typewriter/choice.png
hover: typewriter/choice_hover.png
x: 1160
y: 492
hit: [1160, 492, 520, 56]
click:
left: ['console: cui set {player} speaker=村长 line=我是这座村子的村长。\n有困难尽管开口。']
motion: {x: 64, y: 0, delay: 2}
ask-who-text:
type: text
text: '你是谁?'
font: text
size: 22
color: '#E2EEF4'
x: 1188
y: 510
motion: {x: 64, y: 0, delay: 2}
ask-where:
type: button
image: typewriter/choice.png
hover: typewriter/choice_hover.png
x: 1160
y: 560
hit: [1160, 560, 520, 56]
click:
left: ['console: cui set {player} speaker=村长 line=这里是边境的小村子。\n往北走过石桥,就是主城了。']
motion: {x: 64, y: 0, delay: 3}
ask-where-text:
type: text
text: '这里是哪里?'
font: text
size: 22
color: '#E2EEF4'
x: 1188
y: 578
motion: {x: 64, y: 0, delay: 3}
tell-name:
type: button
image: typewriter/choice.png
hover: typewriter/choice_hover.png
x: 1160
y: 628
hit: [1160, 628, 520, 56]
click:
left: ['dialog: myname']
motion: {x: 64, y: 0, delay: 4}
tell-name-text:
type: text
text: '告诉他你的名字'
font: text
size: 22
color: '#E2EEF4'
x: 1188
y: 646
motion: {x: 64, y: 0, delay: 4}
bye:
type: button
image: typewriter/choice.png
hover: typewriter/choice_hover.png
x: 1160
y: 696
hit: [1160, 696, 520, 56]
click:
left: ['console: cui set {player} speaker=村长 line=一路顺风,旅人。', 'delay: 50', 'close']
motion: {x: 64, y: 0, delay: 5}
bye-text:
type: text
text: '再见'
font: text
size: 22
color: '#E2EEF4'
x: 1188
y: 714
motion: {x: 64, y: 0, delay: 5}
leave:
type: button
image: main/close.png
hover: main/close_hover.png
x: 1664
y: 22
hit: [1664, 22, 236, 52]
click:
left: ['close']
motion: {x: 0, y: -70}
leave-text:
type: text
text: '离开'
font: text
size: 20
color: '#BECDD7'
align: center
x: 1782
y: 39
motion: {x: 0, y: -70}
放好后 /cui reload,再 /cui open tut_npc。应该看到:对话框和名牌滑进来,台词逐字打出,打完右下角出现 ▼;点右边的选项台词会换;点「告诉他你的名字」跳出输入窗口;点「再见」说完最后一句后画面自己关掉。
常见问题
执行 cui set 提示「这位玩家没有开着界面」
/cui set 只能换已经开着的画面的值。玩家还没打开对话画面时,用 cui open tut_npc 玩家 line=…,它在画面没开时打开、开着时换值,两种情况都能用。
台词里的字显示成方块或缺字
写在画面文件里的字会自动做进资源包。从指令送进来的台词是执行时才知道的,靠的是常用字库,config.yml 的 fonts.text.common 被关掉的话就可能缺字,见 配置文件。
点「告诉他你的名字」提示对话框开不起来
玩家看到的是「对话框开不起来,请通知管理员」,原因写在后台,找 The dialog … could not be shown 那一行。按钮写的 dialog: 名字 在 dialogs: 里找不到时不会有提示,后台会有一行 the screen has no such dialog,检查两边名字是不是一样。
选项的点击位置不对,点 A 触发了 B
独立按钮的 hit 是整个画布上的坐标,不是从按钮自己的左上角算起。按钮在 x: 1160, y: 560,hit 就要写 [1160, 560, 520, 56]。
相关页面
- 打字机对话:打字效果的完整说明和默认示例画面
- 文字:文字元素的全部选项
- 画面变量 values:
values:和找值的顺序 - 全部动作一览:
console:、delay:、close、dialog:等动作 - 对话框输入:五种输入栏和格式检查