Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Nahumus |
I’ve seen people use the UndoRedo only in Plugins, I’ve tried using it for my game but it didn’t update to the older values somewhy, I think I didn’t understand it well although I tried…
Here’s a brief of my code with some simplifications (read it like a pseudocode it’s not actually my code I just didn’t wanna spam you with alot of lines):
var undo_redo = UndoRedo.new()
func _input(event):
if can_move:
undo_redo.create_action('Move car')
undo_redo.add_undo_property(main_board, 'level', main_board.level)
undo_redo.add_undo_method(self, 'update_main_board')
main_board.selected_car.move(forward)
main_board.level = main_board.to_string()
print(main_board.level)
undo_redo.add_do_property(main_board, 'level', main_board.level)
undo_redo.commit_action()
if event.scancode == KEY_Z and Input.is_key_pressed(KEY_CONTROL):
print(main_board.level)
print(undo_redo.get_version())
undo_redo.undo()
func update_main_board():
main_board.update_board_from_string()
I have the level property in the board object which I want to change back when you press ctrl + z, it changes from the main_board.to_string(), to visually update my board I use the update_board_from_string() so it’s not really important in this case
It all seems to be working, the update_main_board function is called, the versions are looking good but the level property doesn’t change when I press ctrl + z, it prints the same one.
Here’s an example of the debug console when I do 12 moves then press ctrl + z multiple times:
As you can see the level changes before doing the ctrl + z’s (the 10th and 12th characters switch) and when I undo it doesn’t change to what it was
It is always better to provide a working code snippet. I’ve created an answer based on your input. Your mainboard
is a blackbox so I created a custom class. Hope it helps. If so please mark my answer appropriate.
clemens.tolboom | 2021-03-31 14:21