0 votes

It's probably an easy question, but I'm so stuck I can't think of alternatives anymore;

I have a VAR A that changes over time; basically it starts with value 1 and then turns 2 and then 3 and so on.

so I need to do an action that checks each change from VAR A and determines another action for another VAR B; for example, "when the difference from VAR A to VAR B is X then do this; when the difference from VAR A to VAR B is X + 1 then do that" and so on ever.

I already have the answer in my head, but I can't elaborate on code

in Engine by (170 points)

2 Answers

+2 votes
Best answer

Use a setter function, and put the logic check in it.


var a setget _set_a
var b

func _set_a(newA):
    a = newA
    if b - a == x:
        # do something
    if b - a == x + 1:
        # do something else
by (342 points)
selected by

I tested them both and it came closer than I imagined.

anyway thank you guys both were very helpful

+1 vote

For the difference you can use abs (), abs (a-b) is the same as abs (b-a), then you can do:

    var a = 2
    var b = 5
    var x=0
    var compare =true

    func _process(delta):
        if abs(a-b) >= x and compare:
            some_action() #continue call the function while abs (a-b) =>x is true
            compare = false#we avoid the above
        elif abs(a-b) > x and (a > 4) or (b > 4): #others rules 
        elif abs(a-b) == 2:
            print("Be careful here, if the difference jumps from 1 to 5, this will never be 
            printed as expected")   

  func some_action():
   func other_function():
by (2,060 points)
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.