Problem with +=

Godot Version

4.2.2.stable

Question

Hi! I’m having problem with +=, I don’t know why it’s not working with an Int var. Another bool var is working so the code is not wrong. Here is the code:

var a: bool; var b: int
func abc():
	a = !a
	b += 1

the a var works perfectly, but the b var stays at 0. Why is that?
The b var is not a string, because I tried ’ b += “1” ’ (with quotes) and it says that I’m trying to add string to an Int, so nothing wrong there. There is no export var, array, nothing. I don’t know why it’s not working :face_exhaling:

did you call the func yet??

Yes. All other variables are working except the ones that add with +=.
In fact, I’ve always used

if b == 0: b = 1
elif b == 1: b = 2
#etc...

because for some reason adding to a Int is not working here. I don’t know if it’s a configuration or a bug with my system.

can you provide the full code

Sorry, the code is thousands of lines long. I’ll provide the function in question and the declared vars:

extends Node2D

var f1: bool; var f2: bool; var f3: int; var f4: bool; var f5: int #... goes until f30, either bool or int

func _on_rot_1_pressed():
	if not $bg/acc/ac1/t1.texture == null:
		if $bg/acc/ac1/r1.is_playing() == false:
			$rot.play()
			if r1 == 0: $bg/acc/ac1/r1.play("rotate"); r1 = 1
			elif r1 == 1: $bg/acc/ac1/r1.play("rotate2"); r1 = 2
			elif r1 == 2: $bg/acc/ac1/r1.play("rotate3"); r1 = 3
			else: $bg/acc/ac1/r1.play("rotate4"); r1 = 0
			if $bg/acc/ac1.modulate == l:
				if $bg/acc/ac1/t1.texture == Global.i1: f1 = !f1
				elif $bg/acc/ac1/t1.texture == Global.i2: f2 = !f2
				elif $bg/acc/ac1/t1.texture == Global.i3: f3 += 1; if f3 == 4: f3 = 0
				elif $bg/acc/ac1/t1.texture == Global.i4: f4 = !f4
				elif $bg/acc/ac1/t1.texture == Global.i5: f5 += 1; if f5 == 4: f5 = 0
				...
				#... it goes until Global.i30 then the function ends

the booleans vars are working ok. The int vars don’t add up as I said before, but they work if I use = instead of += ; the problem is that using = I have to do it manually until the maximum number I want (4 in this case).

It might be because the default variable = null
And null + 1 can’t be defined (NaN)
Try putting a default value for the int variable like f3 = 0 and see if it works

1 Like

lmao it worked! I didn’t even knew this was a thing; I thought the default values of Int vars were 0.

Thanks a lot for your help bro!! :grin:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.