How to use the UndoRedo not with a plugin

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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 =

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.level = main_board.to_string()
		undo_redo.add_do_property(main_board, 'level', main_board.level)
	if event.scancode == KEY_Z and Input.is_key_pressed(KEY_CONTROL):
func update_main_board():

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:
debug console
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

:bust_in_silhouette: Reply From: clemens.tolboom

In this example undo works on custom class and Button

extends Node2D

class Board:
	var level:int
	var car:String = 'CAR' setget set_car
	func set_car(v:String):
		car = v

var undo_redo:UndoRedo =

onready var board:Board =
onready var button:Button =

func _ready():

func _input(event):
	if Input.is_key_pressed(KEY_D):
		undo_redo.create_action('Move button right')

		undo_redo.add_undo_property(button, 'rect_position', button.rect_position)
		button.rect_position.x += 10
		undo_redo.add_do_property(button, 'rect_position', button.rect_position)

		undo_redo.add_undo_property(board, 'level', board.level)
		board.level += 13
		undo_redo.add_do_property(board, 'level', board.level)


	if Input.is_key_pressed(KEY_Z) and Input.is_key_pressed(KEY_CONTROL) and Input.is_key_pressed(KEY_SHIFT):
		if undo_redo.has_redo():
			printt('Redo',  undo_redo.get_version(), button.rect_position)
	elif Input.is_key_pressed(KEY_Z) and Input.is_key_pressed(KEY_CONTROL):
		printt('Undo',  undo_redo.get_version(), button.rect_position)

Thanks alot for providing the code example, and sorry I didn’t explain the board it’s because it was a question raised from another answer… I changed my code to be like in the example and it still didn’t work so then I thought maybe there’s a problem with the board drawing… then it turned out that I was dumb when making the update board function, it works by finding the difference between the board level given and the board level property and moving the cars accordingly, if not successful drawing them all over… and somewhy I set it to compare with the level property instead of the to_string() of the board, and since the default value of the function is the level it found no difference when redrawing and did nothing. thankfully it’s a multiplayer game so I saw the car moved as intended on the other client .-.

Have a nice day!

Nahumus | 2021-03-31 15:01