Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all create a namespace for your game, e.g. <code>Game</code>:</p> <pre><code>var Game = {}; </code></pre> <p>From now on, all variables that have to do with your game go there. This is to minimize the use of global scope (which is evil).</p> <p>Then say you want to have a state with <code>score</code> and <code>level</code>. You do it like that:</p> <pre><code>Game.state = { score: 0, level: 1 } </code></pre> <p>During the game it can modified, e.g.:</p> <pre><code>Game.state.score = 42; Game.state.level++; </code></pre> <p>To save it, send the Ajax request:</p> <pre><code>$.ajax({ url: "save.php", data: { state: JSON.stringify(Game.state) // sending Game.state as JSON string }, dataType: 'json', type: 'POST', success: function (json_object) { console.log(json_object); $("#data").text("Data has been saved."); }, error: function (json_object) { console.log(json_object); $("#data").text("Failed to save data !"); } } </code></pre> <p><code>JSON.stringify(Game.state)</code> will be a string that looks like this: <code>'{"level":0,"score":42}'</code>.</p> <p>Okay, so after the save you store the sent JSON object as String on your server or something and at some point in the future you want to retrieve it. You can achieve it by doing:</p> <pre><code>$.ajax({ url: "load.php", data: "", // no data, we are just loading dataType: 'json', type: 'POST', success: function (state) { Game.state = state; // okay, state loaded and assigned to our Game.state! } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload