In theory how could I make an undo/redo for a drawing system?

I am not sure if there is a misunderstanding but it doesn’t seem to be what I am looking for, when the user reaches the max undos “saving” which is 20, so they can’t undo something over 20 actions back for example, when a new folder is created and it is over the max, it will remove the oldest and shift all the names down so then I only have to look for one name.

okay, I see now.

I’ll give you an example of it, but you will have to make it yourself

local oldestcachenumber = 1

--when max undos reached, do
cachefolder:FindFirstChild(tostring(oldestcachenumber)):Destroy()
oldestcachenumber += 1

But don’t -= the cache number or the oldest cache number.

I see thanks a lot! I think I got it working, have a great day!

Hey, I the main thing is done, but I didn’t feel like a new topic was necessary for this, I am trying to create the actual undo and redo to cycle through the cache.

I thought I could just subtract and add depending on if they undid, or redid but from the messy picture below trying to show I have an issue, which index do I save the one that was just there?

Blue line: the original frame
Red line: the frame found from the cache history replacing the original frame
The numbers: showing the indexes

I hope that makes sense, if not let me know

Just make a table and add the frames inside the table after you could just cycle through with using the following; getframe = table[number] and when undoing just go back to that frame and delete the one that is latest one and when redoing do not delete them but just get the latest one that was undone with another table like so getundoneframe = undonetable[number]

I mean the system that @regexman told me is semi working, do you think your way will work better? I will be just getting the children of the frame for the table, with that way I can also check if there is the max cap and if so then, table.remove(table, 1) which is handy.

Hey!

A system such as this in practice is not that complex. You can simply use an array and traverse the indexes based on keycodes. I would recommend using a custom enumerate system to keep track because strings generally take up more memory than numbers.

@rottendogDkR gave a good example.

I attempted to code something up, however, I will need some more time because Lua has some restrictive functionality compared to C++.

Yes there might be a max cap then you will need to remove the first one of the table and then add the new scribble/frame in using table.insert(table, item) but you can combine the two with what @samjay22 said.

Just for your own learning purposes (because everyone here seems to understand the principal of how this is accomplished with arrays), you could call it a stack. A stack is a LIFO data structure that uses push/pop (-> only the top of the stack can actually be altered at any time). You may not necessarily need all of the benefits that an actual stack structure can provide.

image

1 Like

I was attempting to write using that method, however, lua has limitations that I am trying to bypass. I may have to write my own stack system and judging by the question OP wont understand the concepts used.

I see, that makes sense, so should I try to do this or will this not work from the limitations that @samjay22 mentioned?

The obvious limitation as far as undo/redo is concerned is that you need to traverse the array in two directions. You could create two stacks, possibly wrote into a dictionary. Otherwise the ideal solution would be traversing a singular array where if you go too far in any one direction the final index on the opposing end gets removed.

Two questions, one how do I know which index I need to get? Number 2, where do I put the frame that was there before the undo?

You would know which index to get by just creating a variable and connecting it to something such as a key bind and just subtract once every time the key was pressed until the limit was hit which then you will need to wait until another item has been added or been redone. You do not need to put the frame anywhere before undoing but only until after undoing you will need to do something with that frame such as putting it into another table only for redoing and hiding that frame.

Could you maybe reexplain? I don’t think I understand, it kind of works, but not well, plus the redo will not work. Thanks

Basically to answer your questions just use a variable to switch indexes for example by pressing a key it the variable goes down to undo, and to answer your second question you will need to insert the frame inside a table every time you make one and when undoing just take it off from the table and add it to another table that is only for redoing.

Can I do this automatically? like do I just whenever they hit undo I go a head an index?

You probably can but it is better to use a variable local Number = 0 to see where exactly you are in the indexes.

So, when do I need to subtract this and when do i add?

You go down “subtract” when you undo because you want to go the “first” one that was made, and you go up “add” when you redo because you want to go the latest one that was added which always is further from 0.