Note that there are some explanatory texts on larger screens.

plurals
  1. POStuck at creating undo functionality in C#/XNA Game
    primarykey
    data
    text
    <p>So I want a undo functionality for a MapEditor I am working on in my game. I got it partly working. When I click on the map, it stores both the old and the new tile in seperate lists. When I press Ctrl + Z, it will undo the last action and so on.</p> <p>My problem is when you undid some actions, and then add a new action to the lists. What should happen then? Should I just add the new action at the end of the lists, or should I remove everything from the current position in the lists till the end, and then add the new action to the list.</p> <p>My problem is that I can't wrap my head around this. I've tried multiple things, but they all were kind of broken when this situation occurred.</p> <p>So again, I need to know how to proceed when adding the new actions to the undo list.</p> <p><strong>My current code when adding to the undo list:</strong></p> <pre><code>private void UpdateCorrectedTiles(Dictionary&lt;TileSide, Tile&gt; correctedTiles, bool saveEditedTiles) { List&lt;Tile&gt; tmpUndoTilesList = new List&lt;Tile&gt;(); List&lt;Tile&gt; tmpRedoTilesList = new List&lt;Tile&gt;(); foreach (Tile tile in tiles) { foreach (KeyValuePair&lt;TileSide, Tile&gt; correctedTile in correctedTiles) { if (tile.GetTilePosition() == correctedTile.Value.GetTilePosition()) { if (correctedTile.Key == TileSide.Clicked &amp;&amp; saveEditedTiles &amp;&amp; Tile.IsTileChanged(previousClickedTile, correctedTile.Value)) { Tile undoTile = Tile.CreateCloneTile(previousClickedTile); Tile redoTile = correctedTile.Value; tmpUndoTilesList.Add(undoTile); tmpRedoTilesList.Add(redoTile); } TileInfo info = correctedTile.Value.GetTileInfo(); Vector2 frames = correctedTile.Value.GetCurrentFrame(); tile.SetTileInfo(info); tile.SetCurrentFrame(frames); } } } if (saveEditedTiles &amp;&amp; tmpUndoTilesList.Count &gt; 0 &amp;&amp; tmpRedoTilesList.Count &gt; 0) { undoTilesList.Add(tmpUndoTilesList); redoTilesList.Add(tmpRedoTilesList); currentUndoRedoIndex = undoTilesList.Count - 1; } } </code></pre> <p>What this code does is in the Foreach, it will loop through all the tiles that have been corrected. If the corrected tile is the clicked tile and it has actually changed, it will add it to the Undo and Redo List. You should ignore the Redo list for now, since the redo functionality is not inplemented yet. i want to get the undo function working first.</p> <p>So in the last part of the function, I add the new actions to the lists, but again, I think I need to do something else then adding when you are somewhere in the list and not at the end.</p> <p>Hope you understand what I want.<br> Thanks!</p>
    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.
 

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