Skip to content

Coerce lookahead_time to float above the vehicle loop#522

Open
LXMedia1 wants to merge 1 commit into
ETS2LA:mainfrom
LXMedia1:fix/coerce-lookahead-time-to-float
Open

Coerce lookahead_time to float above the vehicle loop#522
LXMedia1 wants to merge 1 commit into
ETS2LA:mainfrom
LXMedia1:fix/coerce-lookahead-time-to-float

Conversation

@LXMedia1
Copy link
Copy Markdown
Contributor

Description

CollisionAvoidance.get_intersecting_vehicles passes settings.lookahead_time directly into Vehicle.get_path_for(...). The setting is declared as float in Plugins/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_for does:

for i in range(0, int(seconds * points_per_second)):

with seconds = "2.5" and points_per_second = 10. "2.5" * 10 is Python string repetition, so the expression becomes "2.52.52.52.52.52.52.52.52.52.5", and int(...) raises:

ValueError: invalid literal for int() with base 10: '2.52.52.52.52.52.52.52.52.52.5'

Plugin crashes, Collision Avoidance stops running until a restart.

The fix is a one-line hoist: coerce to float once at the top of get_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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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.
Copilot AI review requested due to automatic review settings April 18, 2026 02:36
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_time to a local lookahead_time: float at the top of get_intersecting_vehicles.
  • Pass the coerced lookahead_time into vehicle.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)
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
lookahead_time = float(settings.lookahead_time)
try:
lookahead_time = float(settings.lookahead_time)
except (TypeError, ValueError):
lookahead_time = 3.0

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants