1

So I’m running into an issue while implementing an ‘undo’ mechanic in Godot Engine v4.4.1 using GDScript. I’m trying to limit the size of the undo queue, and cut off the end once it exceeds the limit.

The code snippet looks as such, where Command is a custom class, and executed commands get instantiated as nodes in the tree (hence the queue_free()):

const QUEUE_LIMIT: int = 5
var undo_queue: Array[ Command ] = []
if undo_queue.size() > QUEUE_LIMIT:
    print("too big")
    undo_queue.pop_back().queue_free()

The issue is that the if statement never triggers.

If I run the following:

print(type_string(typeof(undo_queue.size())))

and

print(type_string(typeof(QUEUE_LIMIT)))

both correctly print ‘int’. However, if I run this:

print(type_string(typeof(undo_queue.size()>QUEUE_LIMIT)))

it prints ‘Object’, when the operation should be giving me a boolean.

In fact, even this:

var testvar: int = 1
var testvar2: int = 2
print(type_string(typeof(testvar2>testvar)))

prints ‘Object’, while this:

print(type_string(typeof(2>1)))

fortunately prints ‘bool’.

Does anyone know what’s going on and how to get around it? Assigning the comparison to a variable in advance doesn’t seem to work either. Thanks in advance.

3
  • 1
    It prints bool for me: gdscript-online.github.io/… Commented Aug 17 at 5:36
  • print bool for me also Commented Aug 17 at 8:22
  • I tried the testvar thing again today and now it's also printing bool, so I'm not sure what was happening there before. Nevertheless, the issue with undo_queue.size() didn't change but I just posted how that got fixed. Commented Aug 17 at 9:17

1 Answer 1

1

I got an answer from the Godot forum (thanks hexgrid!). It turns out the program is trying to evaluate the comparison before it evaluates undo_queue.size(), so making it a little wordier as such:

var slots = undo_queue.size()
if slots > QUEUE_LIMIT:
    ...
    ...

fixed the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.