0 votes

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 mainboard.tostring(), to visually update my board I use the updateboardfrom_string() so it's not really important in this case

It all seems to be working, the updatemainboard 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

Godot version 3.2.1
in Engine by (28 points)
edited by

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.

1 Answer

+1 vote
Best answer

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 = UndoRedo.new()

onready var board:Board = Board.new()
onready var button:Button = Button.new()

func _ready():
    add_child(button)

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)

        undo_redo.commit_action()

    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)
            undo_redo.redo()
    elif Input.is_key_pressed(KEY_Z) and Input.is_key_pressed(KEY_CONTROL):
        printt('Undo',  undo_redo.get_version(), button.rect_position)
        undo_redo.undo()
by (638 points)
selected by

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.