Difference between revisions of "Script:Persist.js"

From Spherical
Jump to: navigation, search
(Download: fix link)
(Quick start: fill)
Line 15: Line 15:
  
 
==Quick start==
 
==Quick start==
 +
<ol>
 +
<li>Download persist.js into your <var>scripts/</var> folder.</li>
 +
<li>Put this in your game function:
 +
<syntaxhighlight>
 +
RequireScript("persist.js");
 +
function game()
 +
{
 +
  persist.init();
 +
  // ...
 +
}</syntaxhighlight>
 +
</li>
 +
<li>Create a <var>maps/</var> folder in your scripts folder, so it looks like <var>scripts/maps/</var></li>
 +
<li>For each <var>some_map.rmp</var>, make a new script file <var>scripts/maps/some_map.js</var></li>
 +
<li>Start writing in <var>scripts/maps/some_map.js</var>:
 +
<syntaxhighlight>
 +
({
 +
  visited: false,
 +
 +
  enter: function (self) {
 +
    if (!self.visited) {
 +
      Say("Welcome to " + GetCurrentMap() + "!");
 +
      self.visited = true;
 +
    }
 +
  },
 +
 +
  Billy: {
 +
    times: 0,
 +
 +
    talk: function (self) {
 +
      self.times += 1;
 +
      Say("Hi! We've spoken " + self.times + " before.");
 +
    }
 +
  }
 +
})
 +
</syntaxhighlight>
 +
This will greet the player when they enter the map, and let the person named "Billy" say how many times the player has spoken to them.</li>
 +
</ol>
 +
 
==Reference==
 
==Reference==
 
==How to&hellip;==
 
==How to&hellip;==
  
 
[[Category:Scripts]]
 
[[Category:Scripts]]

Revision as of 22:47, 3 June 2013

persist.js lets you store variables in persons and maps, and use them in event scripts.

At its simplest, you can store simple quest information, e.g. if you've talked to an NPC, or collected an item. Power users can take full advantage of JavaScript, and write templates for common person patterns, e.g. one-line townsfolk, store keepers, inn keepers, save points, chests, items, door switches, bosses blocking passages, and warps.

It helps you to organise your game data. If you ever got sick of making global variables and confusing data structures to hold your game info, this is your answer.

Download

  • persist.js (17.9 KB JavaScript file)
  • persist.js demo game (15.6 KB ZIP archive, requires Sphere)

The code (embedded Gist)

<gist>5702052</gist>

Quick start

  1. Download persist.js into your scripts/ folder.
  2. Put this in your game function:
    RequireScript("persist.js");
    function game()
    {
      persist.init();
      // ...
    }
  3. Create a maps/ folder in your scripts folder, so it looks like scripts/maps/
  4. For each some_map.rmp, make a new script file scripts/maps/some_map.js
  5. Start writing in scripts/maps/some_map.js:
    ({
      visited: false,
     
      enter: function (self) {
        if (!self.visited) {
          Say("Welcome to " + GetCurrentMap() + "!");
          self.visited = true;
        }
      },
     
      Billy: {
        times: 0,
     
        talk: function (self) {
          self.times += 1;
          Say("Hi! We've spoken " + self.times + " before.");
        }
      }
    })
    This will greet the player when they enter the map, and let the person named "Billy" say how many times the player has spoken to them.

Reference

How to…