0 votes

Hi everyone,

I have a Label showing a couple of numbers, which change occasionally. Each time a change takes place, I'd like to check which of the digits actually changed and what digit replaced the old one.

For example, when the Label shows „10836“ and then changes to „10796“, I'd like to trigger the Signals „position 2 changed to 9“ and „position 3 changed to 7“ (approaching from the right because there might be shorter or longer numbers).

I'm thinking of maybe extracting each digit, storing it on its own and then compare on the next change... but maybe there's a simpler way?
Any suggestions are much appreciated.

Godot version 3.3
in Engine by (492 points)

1 Answer

0 votes
Best answer

Something like this?

var old: int
var new: int
# populate your ints
var pos = 0
while(old or new):
    if old % 10 != new % 10:
        print("Digit %s changed from %s to %s" % [pos, old % 10, new % 10])
    old /= 10
    new /= 10
    pos += 1
by (1,305 points)
selected by

Something like this!

I put this in its own function which is called right after the "new"-Label changed. Right after that an "old"-Label is filled with the numbers, so the comparison works on the next ping...

Very elegant, thank you very much!

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 Frequently asked questions and 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 [email protected] with your username.