![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Rikri |
I have a dictionary with a few hundred keys in it, and a function which searches every key in the dictionary to find one with a value that lies in a particular range. This function is then run around 100 times on certain frames. During these frames, my game lags substantially: a single frame sometimes takes an upwards of an entire second (and I wouldn’t consider my specs particularly bad, either). Does running this many comparisons tend to cause Godot to lag a lot, or did I do something else inefficiently?
The following is an excerpt from my code:
var object_dict = {
0: {"Type": "FGTile", "Position": [0,0]}, # this dict has ~300ish objects in it when the game is run
}
func point_is_between(p0, p1, p2):
return (p1 >= p0 && p0 >= p2) || (p2 >= p0 && p0 >= p1)
func overlap_between_ranges(x1, y1, x2, y2, x3, y3, x4, y4):
if point_is_between(x1, x3, x4) || point_is_between(x2, x3, x4) || point_is_between(x3, x1, x2):
if point_is_between(y1, y3, y4) || point_is_between(y2, y3, y4) || point_is_between(y3, y1, y2):
return true
return false
func is_range_empty(x1, y1, x2, y2): # this is the function which is run ~100 times
for i in range(0, object_dict.size()):
if object_dict[i]["Type"] == "FGTile":
if overlap_between_ranges(x1, y1, x2, y2, object_dict[i]["Position"][0]-2, object_dict[i]["Position"][1]-2, object_dict[i]["Position"][0]+1, object_dict[i]["Position"][1]+1):
return false
return true