How to make a variable that identifies self-change and does a certain action

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lucasfazzi

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

:bust_in_silhouette: Reply From: estebanmolca

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	
            other_function()
            b=4
        elif abs(a-b) == 2:
            print("Be careful here, if the difference jumps from 1 to 5, this will never be 
            printed as expected")	
        else:
             pass
                    
  func some_action():
      pass	
   func other_function():
       pass
:bust_in_silhouette: Reply From: jandrewlong

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
        pass
    if b - a == x + 1:
        # do something else
        pass

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

anyway thank you guys both were very helpful

lucasfazzi | 2019-11-27 01:42