简体   繁体   English

区分何时读取带有字符串的变量

[英]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.

相关问题 使用onclick函数传递参数时,应将其作为字符串读取,将其作为变量读取 - passing a parameter with onclick function is read as a variable when it should be read as a string 在jQuery中区分字符串包含1和字符串包含11 - Distinguishing string contain 1 with string contain 11 in jQuery 使编译器将字符串读取为变量 - get the compiler to read a string as a variable 使用字符串读取配置文件变量 - Use String to Read Config File Variable 流星:“无法读取未定义的属性'xxx'”并用变量替换字符串 - Meteor: “cannot read property 'xxx' of undefined” & replace string with variable 如何将变量作为字符串传递以找到要由d3读取的文件 - How to pass a variable as a string to locate a file to be read by d3 如何将字符串作为道具传递并使组件像变量一样读取它? - How to pass a string as a props and make the component read it like a variable? 无法读取我知道是字符串的变量的“长度”属性(javascript) - Can't read "length" propery on a variable that I know is a string (javascript) 如何读取XML文件的内容并将其保存为字符串形式的javascript变量? - How to read the contents of a XML file and save it in a javascript variable as string? 如何以纯文本格式读取JS文件并将其作为字符串放入变量中 - How to read a JS file as plain text and put it as string into a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM