Certain lines seem to ignore if statement after the first pass

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

Im sorry if I have already asked this question already because I cannot find any record of doing so.
I have a singleton that contains my global variables, but I have confirmed that this error remains even when it isn’t one. In it, I want to detect a change in the array “overlapping_tasks” by making a copy of it and testing for when they are different. The overlapping_tasks.append(0) is just for demonstration purposes.

This works the first time, but then after that the code in the if statement never runs again. In fact, it seems like the line overlap_c = overlapping_tasks is running every single frame. What’s even weirder is that when I had a version of this that ran every second instead of every frame, each second paused before running the line overlap_c = overlapping_tasks functions completely normally (I even had a counter to make sure and the counter was updating).
Does anyone know what is causing this and how I can avoid/fix this? My full code for the node is down below

var overlapping_tasks = []
var overlap_c = []

func _process(delta):
	if overlap_c != overlapping_tasks:
		overlap_c = overlapping_tasks
		print("YAY")
	overlapping_tasks.append(0)
	print(overlapping_tasks == overlap_c)

I’ve done some testing at it seems like the problem lies within the line overlap_c = overlapping tasks. It seems like there’s is an issue where after running that the two variables seem linked together, where no matter what I do to one the other receives the same change.
I’ve managed to get it working though, as I have replaced that line with

overlap_c.clear()
overlap_c.append_array(overlapping_tasks)

That doesn’t seem like the most efficient way to solve the problem but it works for now

Goodman599 | 2022-08-04 23:21

:bust_in_silhouette: Reply From: godot_dev_

I assume your problem is the condition overlap_c != overlapping_tasks. You want to compare both arrays’ content, and if they share the same elements, you want the if statement to be true. If I am not mistaken, the overlap_c != overlapping_tasks comparison is checking whether overlap_c and overlapping_tasks reference the same array. They don’t in your example code, which would explain why your if statement is always true.

What you should do instead is check both array’s sizes, when they are different, the arrays clearly are different. When they share the same size, iterate over each element of both arrays to compare the elements together. Now it is up to you if by design, arrays are considered equal if they contain he same tasks in different order or if the arrays have to strictly contain the same elements in the same order to be equal