简体   繁体   中英

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.

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".

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 在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM