Learning Agents: Trained in One Map, Fails in Another Map - Error Occurred

RERocowolf
Sat, 18 Oct 2025 13:46:47 +0000
Programming & Scripting

Hello everyone,

I’m encountering a generalization issue with the Learning Agents plugin in Unreal Engine 5.6.1 and would appreciate your help. Here is my detailed setup and the problem:

Environment:

Unreal Engine: 5.6.1 (Source Build)

Project Template: VehicleTemplate (5.6.1 version)

Learning Agents Tutorial: Followed the 5.5 version tutorial.

Training Map: VehicleTemplate (the default map from the Racing Template).

Target/Inference Map: Variant_Timetrial (another map from the same template).

Steps I Took:

1. Successful Training: I successfully trained my agents in the VehicleTemplate map. Everything works as expected during training.

2. Setup in New Map: To run inference in the new map Variant_Timetrial, I did the following:

Placed BP_SportsCarManager and BP_SportsCarTrackSpline into the map.

Redrew the spline to match the new track layout.

Added the same number of agents as in the training map.

Checked the Run Inference box to run the pre-trained model.

3. The Error: At this point, the editor throws an error and the inference fails.

Code Modifications (Deviations from Tutorial):

Compared to the standard tutorial, I made several key modifications to the BluePrint to address issues in the VehicleTemplate map:

ResetToRandomPointOnSpline Function:

Original: Looped through and reset all agents, with an additional loop to prevent agents from spawning too close together.

My Modification: It now only resets the current agent and I removed the anti-clustering loop. This was necessary because the small size of VehicleTemplate caused infinite loop errors when using 32 agents.

BP_SportsCarTrainingEnv Blueprint:

I have overridden both ResetAgentEpisode and ResetAgentEpisodes.

In my overridden ResetAgentEpisodes, I manually implement a ForEach loop to reset all agents.

The Critical Clue:

Following the tutorial, I have a logic node that cleans up the network. The bool input for this node is defined as: NOT (Continue Train OR Run Inference).

If I check Continue Train (and leave Run Inference unchecked), the editor does NOT crash. This suggests the agent and manager setup is fundamentally valid.

The error only occurs when I check the Run Inference box (and leave Continue Train unchecked).

My Question:

Why would my trained model work perfectly in the training map but fail with an error when switching to a new map for inference? Most importantly, why does the GatherAgentObservation function in BP_SportsCarInteractor sometimes receive an invalid ObsActor during inference in the new map, leading to a crash? Could this be related to my custom reset logic or the way agents are managed when switching from training to inference mode in a different environment?

Why does the Run Inference flag trigger the error while Continue Train does not? Given my customizations, could the issue lie in the interaction between my modified ResetToRandomPointOnSpline function, the overridden ResetAgentEpisodes node, and the different spline/data in the new map during inference?

What I’ve Considered:
Through debugging, I discovered that within the GatherAgentObservation function of BP_SportsCarInteractor, the GetAgent call sometimes returns an invalid ObsActor. This is the direct cause of the error, as subsequent operations (like printing the actor’s location) fail. (I have attached screenshots of the debug print and the error).

Any insights into the root cause or steps to debug this would be immensely helpful. Thank you for your time and expertise!

2 posts - 1 participant