bryanwillis7
bryanwillis7

Reputation: 131

Coffeescript load contents of text file into string variable

branches =
branch0:
  name: "Start" 
  text: "This is a wall of text. #1 {Terra: Hello there! My name is Terra! What's yours?} You find your desperation outweighing Cora’s vague warning. You feel miserable, paranoid, and your skin grows more raw and drenched by the minute. This is the fourth wall of text. This is the fifth wall of text. {Terra: My you're cute!} This is the sixth wall of text."
  options: 
    branch1: "Bad"
    branch2: "Good"
branch1:
  name: "Bad Intro"
  text: "You're a bad person!"
  options:
    branch3: "To Gate"
branch2:
  name: "Good Intro"
  text: "You've done good things!"
  options:
    branch3: "To Gate"

So I have a text-based game I'm working on. The main branching paths are separated into objects, and the story text is a property called "text"; the main game logic involves reading the giant string inside the "text:" property and writing it to an HTML document.

For the sake of readability, is there any way at all I can read my text from separate files and somehow have the contents appear as a string of my "text:" property? It's getting messy and difficult to work on this project with a 7000+ word story cluttering my main code like this.

Upvotes: 0

Views: 316

Answers (1)

kba
kba

Reputation: 1177

You should extract the game data to a file or set of files. This can be either JSON (full conversation tree / a file for every branch) or just the texts (e.g. load the text for branch branch1 from text/branch1.txt).

In general, it's a good idea to make your game data expressive, so you can keep data and code apart and define most of the game's behavior from a set of data files (i.e. JSON files). Besides making your game more robust, it will allow non-programmers to produce content and try it out without altering source code.

Upvotes: 1

Related Questions