Friday 28 March 2014

The "tale" variable

This is following on from the post on the "state" variable. My start page looks like this, and there are two other pages.

<<set $x = 4>>
<<set alert(JSON.stringify(tale))>>
<<set $y = 7>>
[[Two]]

In Sugarcube I get this (after a little formatting):

{
"passages":{
  "Three":{"title":"Three","id":0,"domId":"passage-three","text":"[[Start]]","textExcerpt":"Start…","tags":[],"classes":[],"className":""},
  "Two":{"title":"Two","id":1,"domId":"passage-two","text":"A second page.\n<<set $z = 10>>\n[[Three]]","textExcerpt":"A second page. Three…","tags":[],"classes":[],"className":""},
  "Start":{"title":"Start","id":2,"domId":"passage-start","text":"A first page.\n\n<<set $x = 4>>\n<<set alert(JSON.stringify(tale))>>\n<<set $y = 7>>\n[[Two]]","textExcerpt":"A first page. Two…","tags":[],"classes":[],"className":""},
  "StoryTitle":{"title":"StoryTitle","id":3,"domId":"passage-storytitle","text":"Untitled Story","textExcerpt":"Untitled Story…","tags":[],"classes":[],"className":""},
  "StoryAuthor":{"title":"StoryAuthor","id":5,"domId":"passage-storyauthor","text":"Anonymous","textExcerpt":"Anonymous…","tags":[],"classes":[],"className":""}
},
"title":"Untitled Story",
"domId":"untitled-story"
}


So tale has three properties, passages (a hash table of all the passages), title and domId (a computer friendly - slugified - form of the title). Each member of the passages hash table looks like this, after formating:


"Three":{
  "title":"Three",
  "id":0,
  "domId":"passage-three",
  "text":"[[Start]]",
  "textExcerpt":"Start…",
  "tags":[],
  "classes":[],
  "className":""
},

Some of this obvious; text is the text, tags is an array of tags, title is the name of the passage (and this is also used as the key for the hash table). The id property looks to be a unique number for each passage in the game. After that, I do not know.

In Sugarcane it looks like this:


{
"storysettings":{},
"passages":{
  "Three":{"title":"Three","id":0,"tags":[],"text":"[[Start]]"},
  "Two":{"title":"Two","id":1,"tags":[],"text":"A second page.\n<<set $z = 10>>\n[[Three]]"},
  "Start":{"title":"Start","id":2,"tags":[],"text":"A first page.\n\n<<set $x = 4>>\n<<set alert(JSON.stringify(tale))>>\n<<set $y = 7>>\n[[Two]]"},
  "StoryTitle":{"title":"StoryTitle","id":3,"tags":[],"text":"Untitled Story"},
  "StoryAuthor":{"title":"StoryAuthor","id":5,"tags":[],"text":"Anonymous"}
}
}

So we just have two properties; storysettings and passages. Passages is again a hash table, and each entry has a title string, a tags array, a text string and an id number. All the esoteric properties are missing.

Tale has a number of methods. This is what I found in Sugarcube.


setTitle: Sets the title (and domId).
has: Returns true if the story has a passage with the given name or id.
get: Returns the passage from the given name or id.
lookup:  Returns an array of passages. As far as I can see, this can only be used for looking up tags, though it is coded to handle any array property of a passage. The first parameter should be "tags", the second the tag to look for. An optional third parameter will have the array sorted by the given property (defaults to title).
reset: Resets the passage.

Here is an example of using lookup. It gets an array of passages with the tag "testtag", sorted by title, and pops up a message box with the name of the first.

ary = tale.lookup("tags", "testtag");
alert(ary[0].title);

No comments:

Post a Comment