Coerce lookahead_time to float above the vehicle loop#522
Conversation
settings.lookahead_time is declared as float, but the UI slider can
persist it as the string "2.5". When that happens, the scalar ends
up in Traffic/classes.py:get_path_for, which does
range(0, int(seconds * points_per_second))
"2.5" * 10 is Python string repetition ("2.52.52.52.52.52.52.52.52.52.5"),
so int(...) raises ValueError and the plugin crashes.
Hoisting float(settings.lookahead_time) to a local above the loop
fixes the crash (any stringy value is coerced up front) and also
avoids a per-iteration attribute lookup plus float() call in the hot
path. No behaviour change when the setting is already a float:
float(x) returns x unchanged.
There was a problem hiding this comment.
Pull request overview
Fixes a crash in the Collision Avoidance plugin when settings.lookahead_time is persisted as a string (e.g., "2.5") by coercing it to a float once before iterating over vehicles, ensuring Vehicle.get_path_for(...) receives a numeric value.
Changes:
- Hoist/cast
settings.lookahead_timeto a locallookahead_time: floatat the top ofget_intersecting_vehicles. - Pass the coerced
lookahead_timeintovehicle.get_path_for(...)instead of the raw settings value.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| sensitivity = settings.sensitivity | ||
| intersecting_vehicles = [] | ||
| impacts = [] | ||
| lookahead_time = float(settings.lookahead_time) |
There was a problem hiding this comment.
lookahead_time = float(settings.lookahead_time) fixes the common "2.5" string case, but the underlying reason the setting can load as a string is that ETS2LASettings casts DB values using type(default) (not the annotation). In this plugin, lookahead_time: float = 3 uses an int default, so int("2.5") fails and the raw string is kept. Consider changing the defaults in Plugins/CollisionAvoidance/settings.py to real floats (e.g., 3.0) so values are consistently loaded as floats and you may not need defensive casting here long-term.
| lookahead_time = float(settings.lookahead_time) | |
| try: | |
| lookahead_time = float(settings.lookahead_time) | |
| except (TypeError, ValueError): | |
| lookahead_time = 3.0 |
Description
CollisionAvoidance.get_intersecting_vehiclespassessettings.lookahead_timedirectly intoVehicle.get_path_for(...). The setting is declared asfloatinPlugins/CollisionAvoidance/settings.py(lookahead_time: float = 3), but in practice the slider can persist it as a string — I was hitting this with"2.5".When the stored value is a string,
Traffic/classes.py:get_path_fordoes:with
seconds = "2.5"andpoints_per_second = 10."2.5" * 10is Python string repetition, so the expression becomes"2.52.52.52.52.52.52.52.52.52.5", andint(...)raises:Plugin crashes, Collision Avoidance stops running until a restart.
The fix is a one-line hoist: coerce to
floatonce at the top ofget_intersecting_vehicles, then pass the local into the loop. When the setting is already numeric,float(x)is a no-op. As a side effect we also stop re-doing the attribute lookup + coercion for every tracked vehicle on every tick.No behaviour change when the setting is numeric.
Type of change