egametang/ET · error · Exception
btenv not found key: {key} {typeof(T).FullName}
Error message
btenv not found key: {key} {typeof(T).FullName} What it means
Thrown by BTEnv.GetCollection<T> when the requested key is absent from the env's internal dict. BTEnv is a typed blackboard shared across behavior-tree nodes; GetCollection retrieves an IEnumerable previously added via AddCollection. The exception surfaces both the missing key and the expected element type so you can tell which collection accessor failed.
Source
Thrown at Packages/cn.etetet.behaviortree/Scripts/Model/Share/BTEnv.cs:87
env.RunPath.AddRange(this.RunPath);
env.EntityId = this.EntityId;
env.TreeId = this.TreeId;
#endif
return env;
}
public void SetTreeId(long treeId)
{
#if UNITY_EDITOR
this.TreeId = treeId;
#endif
}
public T GetCollection<T>(string key) where T: IEnumerable
{
if (!this.dict.TryGetValue(key, out object value))
{
throw new Exception($"btenv not found key: {key} {typeof(T).FullName}");
}
return (T)value;
}
public bool TryGetStruct<T>(string key, out T t) where T: struct
{
t = default;
if (key == null)
{
return false;
}
if (!this.dict.TryGetValue(key, out object value))
{
return false;
}View on GitHub (pinned to 5cab01f7a8)
Solutions
- Verify the key string matches exactly between the AddCollection call and the GetCollection call (check spelling and casing).
- Ensure the node that calls AddCollection runs before the node that calls GetCollection in the tree order.
- Guard the read with env.ContainKey(key) before calling GetCollection, or use a default value.
- If using pooled BTEnv, confirm the env was not cleared/recycled between write and read.
Example fix
// before
var targets = env.GetCollection<List<long>>("targets"); // throws [40] if never set
// after
if (env.ContainKey("targets"))
{
var targets = env.GetCollection<List<long>>("targets");
} Defensive patterns
Strategy: validation
Validate before calling
if (!env.ContainKey("targets")) { /* return default or skip */ }
var targets = env.GetCollection<List<long>>("targets"); Try / catch
try { var targets = env.GetCollection<List<long>>("targets"); }
catch (Exception e) when (e.Message.Contains("btenv not found key")) { /* key absent */ } Prevention
- Centralize key strings as named constants shared by writer and reader nodes.
- Ensure the AddCollection node runs before the GetCollection node in tree order.
- Prefer ContainKey checks for optional data.
When it happens
Trigger: Calling env.GetCollection<List<long>>("targets") (or any T:IEnumerable) when no AddCollection call ever wrote that key, or when the key was written under a different name/string literal.
Common situations: A BT node handler reads a collection that an upstream node was supposed to populate but the upstream branch never ran; a typo between the key used at AddCollection and at GetCollection; the tree was re-entered on a pooled BTEnv whose IPool.Clear wiped the dict.
Related errors
- 不能把{value.GetType()}转换为{typeof (T)}
- condition root child count error: {node.Children.Count}
- unknown condition compare op: {op}
- condition number parse error: {text}
- condition token error at {this.index}: {c}
AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13).
Data as JSON: /api/errors/f2380f70e7223790.
Report an issue: GitHub.