Thanks Crazy Programmer!
Unfortunately I do need a bit more data than that, ideally every function the editor can perform is able to be undone/redone, but the two most important are definitely creating sprites and deleting them.
I realized that part of the "bug" wasn't actually what I thought - the undo/redo is doing it in correct order, it just for some reason will not re-delete a previously deleted sprite, but it will set the gridspace to be usable again, and send out a print statement.
I will post my much-improved code:
// This file is for undoing and redoing actions in the editor
// First set up a type which will store the data
Type undoData
actionType as integer
tileID as integer
tileX as integer
tileY as integer
tileType as integer
lastX as integer
lastY as integer
EndType
// Create the undo and redo list
Global UndoStack as undoData[]
Global RedoStack as undoData[]
Global copyData as undoData
// Define some constants
#Constant TILE_PLACED = 1
#Constant TILE_DELETED = 2
// Add the last performed action to the Undo Stack
Function AddToUndoStack(actionType as integer)
// Create a local undo object
local undo as undoData
// Store the action type
undo.actionType = actionType
// Store the id#
undo.tileID = copyData.tileID
// store the position
undo.tileX = copyData.tileX
undo.tileY = copyData.tileY
undo.tileType = copyData.tileType
undo.lastX = copyData.lastX
undo.lastY = copyData.lastY
// insert object into undo stack
UndoStack.insert(undo)
EndFunction
// Add the last undone action to the Redo Stack
Function AddToRedoStack()
// create a local redo object
local Redo as undoData
// Get the last action that was performed
i = UndoStack.length
// copy the values from the undo stack
Redo.actionType = UndoStack[i].actionType
Redo.tileID = UndoStack[i].tileID
Redo.tileType = UndoStack[i].tileType
Redo.tileX = UndoStack[i].tileX
Redo.tileY = UndoStack[i].tileY
Redo.lastX = UndoStack[i].lastX
Redo.lastY = UndoStack[i].lastY
// insert into redo stack
RedoStack.insert(Redo)
EndFunction
// Undo function
Function Undo()
// First determine what the last action taken was
i = UndoStack.length
// undo the last action
Select UndoStack[i].actionType
Case TILE_PLACED:
// delete the sprite
DeleteSprite(UndoStack[i].tileID)
// Clear the map tileExists flag at this point
map[UndoStack[i].lastX, UndoStack[i].lastY].tileExists = 0
EndCase
Case TILE_DELETED:
// Recreate the sprite
img = UndoStack[i].tileType
map[UndoStack[i].lastX, UndoStack[i].lastY].tileExists = 1
map[UndoStack[i].lastX, UndoStack[i].lastY].tileID = CreateSprite(img)
SetSpritePosition(map[UndoStack[i].lastX, UndoStack[i].lastY].tileID, UndoStack[i].tileX, UndoStack[i].tileY)
EndCase
EndSelect
// Copy over to the redo stack
AddToRedoStack()
// Delete this element of the array
UndoStack.remove(i)
EndFunction
// Redo Function
Function Redo()
// determine what to do
i = RedoStack.length
Select RedoStack[i].actionType
Case TILE_PLACED:
// recreate the sprite
img = RedoStack[i].tileType
map[RedoStack[i].lastX, RedoStack[i].lastY].tileExists = 1
map[RedoStack[i].lastX, RedoStack[i].lastY].tileID = CreateSprite(img)
SetSpritePosition(map[RedoStack[i].lastX,RedoStack[i].lastY].tileID, RedoStack[i].tileX, RedoStack[i].tileY)
EndCase
Case TILE_DELETED:
// delete the sprite
DeleteSprite(RedoStack[i].tileID)
// Clear the map tileExists flag at this point
map[RedoStack[i].lastX, RedoStack[i].lastY].tileExists = 0
Print("Am I Gone?")
EndCase
EndSelect
// Copy this info over to our copyData object
copyData.actionType = RedoStack[i].actionType
copyData.tileID = map[RedoStack[i].lastX, RedoStack[i].lastY].tileID
copyData.tileX = map[RedoStack[i].lastX, RedoStack[i].lastY].tileX
copyData.tileY = map[RedoStack[i].lastX, RedoStack[i].lastY].tileY
copyData.tileType = map[RedoStack[i].lastX, RedoStack[i].lastY].tileType
copyData.lastX = RedoStack[i].lastX
copyData.lastY = RedoStack[i].lastY
// Add to the undo stack
AddToUndoStack(copyData.actionType)
// Delete this element of the array
RedoStack.remove(i)
EndFunction
Function NewAction()
// Delete any elements in the redo list
If RedoStack.length > -1
RedoStack.length = -1
EndIf
EndFunction
I made a video which will explain it more in-depth, I will post just as soon as it finishes uploading!
EDIT: Here is the video, however don't bother watching it (unless you want to see some of the other stuff I've added to the editor) as I JUST SOLVED THE PROBLEM!
So, I was using this line of code:
DeleteSprite(RedoStack[i].tileID)
It SHOULD have been:
DeleteSprite(map[RedoStack[i].lastX, RedoStack[i].lastY].tileID)
It wasn't deleting the sprite because it was trying to delete a sprite from a 2D array using the index of a 1D array... Ugh TWO DAYS ON THIS AND I JUST NOW REALIZED WHAT IT WAS!!!!

Sign up for NaGaCreMo!
