Cell Attachment Analysis
This more of a self reminder for myself but I hope it proves useful to others.
It's been proven that cell attachment/detachment is the result of the engine checking a bool on the objectreference as to whether or not its cell is/was attached/detached. See OnCellAttach - ObjectReference talk page.
However, I'm going to extend the basic info about these two things a bit.
It's all about the objectreferences in the cell with the player/reference that has scripts attached them containing these events.
My research is mainly for interiors, so here goes.
These are for more reliable then any other load event associated with Cells. They can be used for a large range of tracking, provided that you understand how they work.
OnCellAttach will always fire for an objectreference when another reference(like the player) enters its parent cell. This is expected behavior. OnCellDetach will also always fire for an objectreference when another reference(again like the player) leaves its parent cell. Notice that these events weren't firing as the result of the objectreference itself changing cells.
In short, the steps are:
- OnCellAttach
- OnCellDetach
To reduce confusion, I'll refer to the object with the parent cell as the objectreference and the object in it's parent cell as the player.
Now let's talk about the objectreference changing cells. There is where OnAttachedToCell - ObjectReference and OnDetachedFromCell - ObjectReference come in. The above still applies but with some new additions. If the objectreference moves to another cell, OnCellDetach will fire but for the player that left the objectreference's parent cell BEFORE the objectreference was moved. Yes, OnCellDetach will never fire as the result of the objectreference changing cells. The objectreference hasn't moved yet. When the objectreference completes is movement to the next cell, OnAttachedToCell will fire. As its name implies, the new cell is attached to the objectreference thereby becoming its new parent cell and OnCellDetach can fire as normal if the player leaves this cell. Now, if the objectreference moves back to the previous cell, but the player stays put, OnDetachedFromCell is fired and this is because, as its name implies, it's moving from an attached cell to a detached one. To clarify, the objectreference's new parent cell is a detached one, while the player is in an attached one. OnCellAttach can fire again as normal if the player re-enters the previous cell.
In short, the steps are:
- OnCellAttach
- OnCellDetach
- OnAttachedToCell
- =>OnCellDetach
- OnDetachedFromCell
- =>OnCellAttach