FyroxEngine/Fyrox · error
Cast to failed!
Error message
Cast to {} failed! What it means
This panic comes from a macro-generated node accessor (e.g. component_of/as_ref pattern) that calls Node::cast::<T>() and panics when the node cannot be downcast to the requested type. The message interpolates the type name via stringify!($kind).
Solutions
- Use the non-panicking cast::<T>() which returns Option and handle the None case
- Check node kind (is_light/is_mesh, etc.) or query components_of before casting
- Fix the handle/source data so it actually contains the expected node type
Example fix
// before
let light = node.as_light_ref(); // panics if not a Light
// after
if let Some(light) = node.cast::<Light>() {
// use light
} Defensive patterns
Strategy: type-guard
Validate before calling
if node.cast::<Light>().is_some() { /* safe to use typed accessors */ } Type guard
fn as_light(node: &Node) -> Option<&Light> { node.cast::<Light>() } Try / catch
let light = std::panic::catch_unwind(|| node.as_light_ref()).ok(); // prefer cast::<Light>()
Prevention
- Prefer cast::<T>() (Option) over panicking typed accessors
- Verify node type before calling as_*_ref/as_*_mut
- Re-verify handles after scene data or format changes
When it happens
Trigger: Calling a typed accessor such as node.as_light_ref() / component_of::<Mesh>() on a node whose underlying component is a different type (e.g. casting a Camera node to Light).
Common situations: Grabbing nodes by handle from a scene file whose node types changed; assuming a handle points to a specific kind after script/refactor changes; iterating pool nodes and applying type-specific calls unchecked.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cast to failed!
- Type mismatch!
- Animation pool must be empty on load!
- An object at index must be returned to a pool it was taken…
- Attempt to spawn an object at pool record with payload!…
AI-assisted analysis of FyroxEngine/Fyrox@76c91aad8e (2026-09-10).
Data as JSON: /api/errors/a28fe83bed7b9cc6.
Report an issue: GitHub.
Appendix: source
Thrown at fyrox-impl/src/scene/node/mod.rs:476
}
}
/// Defines as_(variant), as_mut_(variant) and is_(variant) methods.
#[macro_export]
macro_rules! define_is_as {
($typ:ty => fn $is:ident, fn $as_ref:ident, fn $as_mut:ident) => {
/// Returns true if node is instance of given type.
#[inline]
pub fn $is(&self) -> bool {
self.cast::<$typ>().is_some()
}
/// Tries to cast shared reference to a node to given type, panics if
/// cast is not possible.
#[inline]
pub fn $as_ref(&self) -> &$typ {
self.cast::<$typ>()
.unwrap_or_else(|| panic!("Cast to {} failed!", stringify!($kind)))
}
/// Tries to cast mutable reference to a node to given type, panics if
/// cast is not possible.
#[inline]
pub fn $as_mut(&mut self) -> &mut $typ {
self.cast_mut::<$typ>()
.unwrap_or_else(|| panic!("Cast to {} failed!", stringify!($kind)))
}
};
}
impl Node {
/// Creates a new node instance from any type that implements [`NodeTrait`].
#[inline]
pub fn new<T: NodeTrait>(node: T) -> Self {
Self(Box::new(node))
}View on GitHub (pinned to 76c91aad8e)