I apologise if this isn’t the adequate place for this question.
So I’ve just finished reading main.gd
in the “Multiple Resolutions and Aspect Ratios Demo” Godot Asset (project link | Git Hub), because I want to make a mobile app that uses the entire viewport area. Reading its comments and using the official Godot Docs, I was able to understand most of it, except one thing:
func update_container():
# The code within this function needs to be run deferred to work around an issue with containers
# having a 1-frame delay with updates.
# Otherwise, `panel.size` returns a value of the previous frame, which results in incorrect
# sizing of the inner AspectRatioContainer when using the Fit to Window setting.
for _i in 2:
if is_equal_approx(gui_aspect_ratio, -1.0):
# Fit to Window. Tell the AspectRatioContainer to use the same aspect ratio as the window,
# making the AspectRatioContainer not have any visible effect.
arc.ratio = panel.size.aspect()
panel.offset_top = gui_margin
panel.offset_bottom = -gui_margin
else:
# Constrained aspect ratio.
arc.ratio = min(panel.size.aspect(), gui_aspect_ratio)
panel.offset_top = gui_margin / gui_aspect_ratio
panel.offset_bottom = -gui_margin / gui_aspect_ratio
panel.offset_left = gui_margin
panel.offset_right = -gui_margin
(I’ve removed some comments that I didn’t think were relevant to this question)
From what I understand, this is the main function in the code, where everything gets handled at the beginning and when the viewport is resized. After searching in the reference guide (since I’m new to GDScript) I found out that for i in 2
is equivalent to for i in range(2)
. What I don’t get is: why does this function have to run everything twice?
Since _i
is never used in the for
loop and I don’t see why do everything twice, I’ve removed the loop itself and tested the code; as far as I can tell, everything still works.