【YarnSnipper】 unity剧情对话插件--语法

Yarn 语法

节点

定义

游戏中长长的剧本被分割成一段段的对话,Node节点就是用来分离故事为很多小的部分,这样使长的故事和分支容易管理。

  • 每个节点之后有一个标题和主体。标题很重要,因为你的游戏使用节点标题告诉Yarn Snipper从哪个节点开始运行,以及跳转到哪个节点。游戏的标题不会显示给玩家。
  • 节点标题可以以字母开始,可以包含字母、下划线和数字。节点标题中不可以含有如“a.”格式的字符。

规范

一个正确的节点应该如下所示:

title: Node_Title
---
Here are some lines!
Wow!
===
第一行是标题,用“:”进行分隔。

“---”是正文开始的标识,在此之后,你可以进行对话或者脚本逻辑的编写。
“===”是当前节点结束的标识,在这个节点之后,你可以写新的节点了。


This is a line of dialogue, without a character name.
Speaker: This is another line of dialogue said by a character called "Speaker".
如上面的语句第2行,如果在“:”之前有一个字符串,那么这个字符串将被标记成说这句话的人名,因此我们可以根据该人名进行语句的特殊效果。而第一行的意思就是没有指明谁说的这句话。

跳转

如果出现游戏中的选择,可以使用"->"字符串来标识该行是一个选择,如:

Companion: Hi there! What do you feel like doing today?

-> Player: I want to go swimming.
    Companion: Okay, let's go swimming.
-> Player: I'd prefer to go hiking.
    Companion: Cool, we'll go hiking then.
    
Player: Sounds good!
"->"标记的两行对应的两种选择。在Player选择之后,Companion会立马做出回应,最后Player再做出回应。

嵌套选择

有时候,游戏中的选项之中又包含选项,于是我们就要用到嵌套选择,如下的对话:

Companion: Hi there! What do you feel like doing today?

-> Player: I want to go swimming.
    Companion: Okay, let's go swimming.
    Companion: Where do you want to swim?
    -> Player: The lake!
        Companion: Nice! It's a great day for it.
    -> Player: The swimming pool!
        Companion: Oh, awesome! I heard they installed a new slide.
-> Player: I'd prefer to go hiking.
    Companion: Cool, we'll go hiking then.
    
Player: Sounds good
逻辑也比较简单,就是抛出一个选项的时候,如果Player选择的是第一个,那么接着又会有新的选项问他去哪里游。这样写的话语句比较少,但是不够直观,因此以上对话还有下面的这样一种写法:
title: Start
---
Companion: Hi there! What do you feel like doing today?

-> Player: I want to go swimming.
    Companion: Okay, let's go swimming.
    <<jump Swimming>>
-> Player: I'd prefer to go hiking.
    Companion: Cool, we'll go hiking then.
    <<jump Hiking>>
===
title: Swimming
---
Companion: Where do you want to swim?
-> Player: The lake!
    Companion: Nice! It's a great day for it.
-> Player: The swimming pool!
    Companion: Oh, awesome! I heard they installed a new slide.

<<jump Done>>
===
title: Hiking
---
Companion: Have you got your hiking boots ready?
-> Player: Yes.
    Companion: Great, let's go!
-> Player: No.
    Companion: We can swing by your place and pick them up!

<<jump Done>>
===
title: Done
---
Player: Sounds good!
===
以上通过<<jump标题>>的形式跳转到另外一个节点去,这样的逻辑比较直观。

变量

变量类型

变量类型总共有3种

  • Number:代表数字如 1,2.2,-200等
  • String:字符串类型,比如"Hello World"
  • Boolean:布尔类型,true 或false

变量用dollar字符表示,如:$hp

设置变量

  • 通过<>来设置如:<<set $greeting to "Hello, Yarn!">>,意思就是将$greeting的变量设置为“Hello, Yarn!”
  • 变量可以在之后进行更改,但是他的类型不可以更改,比如初始设置为数字类型,值是3,后面可以改成8,但是不可以将他从数字类型改为字符串类型。
  • 还可以通过运算来赋值,比如:<<set $numberOfSidesInATriangle = 2 + 1>>,但只有同类型才可以。

在语句中使用变量

<<set $variableName to "a string value">>
The value of variableName is {$variableName}.
以上是在语句中使用变量的案例,输出的结果会是:The value of variableName is a string value.

条件控制

if 语句

if语句是条件语句,通常用来控制某条信息是否显示,以<<if 条件>>开始,<>进行结尾。如果if之后的天剑满足,则进入if和endif之间的语句,否则进入。

<<set $gold_amount to 5>>

Player: I'd like to buy a pie!

<<if $gold_amount < 10>>
    Baker: Well, you can't afford one!
<<endif>>

else if 和 else

每个else if语句后面会跟一个条件判断,如果之前的if或者任何的else if都不满足而当前的这条满足,则进入当前语句,如下:

Player: I'd like to buy a pie!

<<if $gold_amount < 10>>
    Baker: Well, you can't afford one!
<<elseif $gold_amount < 15>>
    Baker: You can almost afford one!
<<else>>
    Baker: Here you go!
<<endif>>
如果gold_amount是小于10,则显示:Well, you can't afford one! ;如果是小于15,则显示You can almost afford one! 即if满足,则不再判断后面的语句。

选项是否可用

条件选择在语句中出现,用来判断一个选项是否能被玩家选择,如下:

Guard: You're not allowed in!

-> Sure I am! The boss knows me! <<if $reputation > 10>>
-> Please?

如果第一个选项中的条件不满足时,则该选项不可以被选择。

posted @   LemonInCup  阅读(470)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示