[英]Distinguishing when a variable with a string is read
branch1:
text:
body: "You see two people talking #{branch1.text.person1} #{branch1.text.person2.1} #{branch1.text.person2.2} "
person1: "Who are you?"
person2:
1: "my name is Terra."
2: "What is your name?"
I'm using raw coffeescipt to build a choose your own adventure game. 我正在使用原始的浓咖啡来制作自己的冒险游戏。 I have different story paths organized into objects, and then those objects further divided into text objects.
我将不同的故事路径组织为对象,然后将这些对象进一步划分为文本对象。 I have a for loop set up that reads the text.body property I set up, and it loops through every character and writes it to the HTML document when the user clicks their mouse.
我有一个for循环设置,可以读取设置的text.body属性,当用户单击鼠标时,它将循环遍历每个字符并将其写入HTML文档。
The question is, I need to attribute dialogue correctly, and have only the dialogue display for that specific character. 问题是,我需要正确地将对话归因,并且只显示该特定角色的对话。 So as it stands, what you are seeing above would get printed like:
这样看来,您在上面看到的将被打印为:
"You see two people talking. Who are you? My name is Terra. What is your name?"
When it needs to read like this: 需要阅读以下内容时:
"You see two people talking."
Person1: "Who are you?"
Person2: "My name is Terra."
Person2: "What is your name?"
I want to do this by having some logic set up in my loop that can somehow know when it's reading a variable that contains a loop. 我想通过在循环中设置一些逻辑来做到这一点,该逻辑可以以某种方式知道何时读取包含循环的变量。 In other words, when my loop gets to the: #{branch1.text.person1} part, the computer will understand "Oh! This is a variable with a string attached to it, I'd better treat this differently".
换句话说,当我的循环进入到#{branch1.text.person1}部分时,计算机将理解“哦!这是一个附加了字符串的变量,我最好以不同的方式对待”。
But the trouble is finding a way to get the computer to distinguish variables nestled within strings like that. 但是麻烦在于找到了一种方法,使计算机能够区分出像这样的字符串中嵌套的变量。 Any ideas?
有任何想法吗?
PS: The end result is to create something that looks kinda like this PS:最终结果是创建了看起来像这样的东西
The problem is that when you use CoffeeScript's string interpolation (with those #{}
blocks), those are compiled to +
statements and evaluated at runtime as JavaScript strings, so you will not be able to know what the expression that constructed the value looked like. 问题在于,当您使用CoffeeScript的字符串插值(与那些
#{}
块)时,它们将被编译为+
语句并在运行时作为JavaScript字符串进行评估,因此您将无法知道构造该值的表达式是什么样的。
If you want to be able to detect these patterns at runtime, you will need a new representation for the data that encapsulates that it's an expression. 如果您希望能够在运行时检测到这些模式,则需要一种新的表示形式来封装数据,该表示形式就是表达式。 For example, you may choose to represent with a regular string, or a function.
例如,您可以选择用常规字符串或函数表示。 It all depends on what your code needs to do.
这完全取决于您的代码需要做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.