<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How best to implement an undo&#x2F;rewind on the server?]]></title><description><![CDATA[<p>Hi,<br />
I'm trying to work out the best way of implementing an undo or rewind function on the server. I realise the JSONPatch stuff gives a delta of what changed but not what it previously was.</p>
<p>Is there a way to rewind the changes to the state or can anyone advise on the best way to implement one?</p>
<p>Many thanks.</p>
]]></description><link>http://discuss.colyseus.io/topic/296/how-best-to-implement-an-undo-rewind-on-the-server</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 21:59:56 GMT</lastBuildDate><atom:link href="http://discuss.colyseus.io/topic/296.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 06 Nov 2019 12:23:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How best to implement an undo&#x2F;rewind on the server? on Invalid Date]]></title><description><![CDATA[<p>Hi,<br />
I'm trying to work out the best way of implementing an undo or rewind function on the server. I realise the JSONPatch stuff gives a delta of what changed but not what it previously was.</p>
<p>Is there a way to rewind the changes to the state or can anyone advise on the best way to implement one?</p>
<p>Many thanks.</p>
]]></description><link>http://discuss.colyseus.io/post/1003</link><guid isPermaLink="true">http://discuss.colyseus.io/post/1003</guid><dc:creator><![CDATA[saiph]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to How best to implement an undo&#x2F;rewind on the server? on Invalid Date]]></title><description><![CDATA[<p>Disclaimer: I didn't implement such a thing by myself. since it is a little time consuming. so maybe someone has a better solution.</p>
<p>My understanding how to achieve time traveling.</p>
<p>You start with your base state:</p>
<pre><code class="language-json">{
 players: {}
}
</code></pre>
<p>Now you add a player on server tick 1145<br />
state looks like this after that</p>
<pre><code class="language-json">{
 players: {
  &quot;420&quot;: {
   name: &quot;Player 1&quot;
  }
 }
}
</code></pre>
<p>Now you add a player on server tick 5000<br />
state looks like this after that</p>
<pre><code class="language-json">{
 players: {
  &quot;420&quot;: {
   name: &quot;Player 1&quot;
  },
  &quot;125&quot;: {
   name: &quot;Player 2&quot;
  }
 }
}
</code></pre>
<p>so what you have to do is: save somewhere the base state. and every action you do on the state.<br />
so example:</p>
<pre><code class="language-json">{
 baseState: {
  players: {}
 },
 actions: [
  {
   type: &quot;AddPlayer&quot;,
   data: {
   id: &quot;420&quot;,
   name: &quot;Player 1&quot;
   tick: 1145
  },
  {
   type: &quot;AddPlayer&quot;,
   data: {
    id: &quot;125&quot;,
    name: &quot;Player 2&quot;
   }
   tick: 5000
  }
 ]
}
</code></pre>
<p>So when the functions which mutate the state of the room are <a href="https://en.wikipedia.org/wiki/Pure_function" rel="nofollow">pure functions</a>.<br />
You can just recalculate the state with all actions to a specific tick.</p>
<p>if we are on tick 12'000 and we want to rewind 10 seconds. we calculate 12'000 - 10'000 so we get 2'000<br />
now we just recalculate all actions in the correct order again which happen to the tick 2'000<br />
so we would get this state:</p>
<pre><code class="language-json">{
 players: {
  &quot;420&quot;: {
   name: &quot;Player 1&quot;
  }
 }
}
</code></pre>
<p>this is the basic stuff. their can be alot of performance optimisation.<br />
like after 2 hours of game time you probably dont want to recalculate the hole state from tick 0.<br />
so you could save a snapshot of the state all 100'000 ticks</p>
<p>hope this helps.</p>
]]></description><link>http://discuss.colyseus.io/post/1004</link><guid isPermaLink="true">http://discuss.colyseus.io/post/1004</guid><dc:creator><![CDATA[Wenish]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to How best to implement an undo&#x2F;rewind on the server? on Invalid Date]]></title><description><![CDATA[<p>Thanks for the detailed answer you've given here, I hadn't though about using the server ticks to wind back the state. I had made a start by saving the JSONPatch on each state change from the current state to the previous state to a stack and then popping them to rewind the changes. My problem now is when i do an undo it triggers the broadcast patch and then I save a new undo action which would undo the undo i just performed! Struggling to find a way to not save the change made by the undo, but the ticks might help me here.<br />
Many thanks.</p>
]]></description><link>http://discuss.colyseus.io/post/1005</link><guid isPermaLink="true">http://discuss.colyseus.io/post/1005</guid><dc:creator><![CDATA[saiph]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>