Revision Difference
ents.Iterator#561402
<function name="Iterator" parent="ents" type="libraryfunc">
<description>
Returns a [Stateless Iterator](https://www.lua.org/pil/7.3.html) for all entities.
Intended for use in [Generic For Loops](https://www.lua.org/pil/4.3.5.html).
See <page>player.Iterator</page> for a similar function for all players.
Internally, this function uses cached values that exist entirely within lua, as opposed to <page>ents.GetAll</page>, which is a C++ function.
Because switching from lua to C++ (and vice versa) incurs a performance cost, this function will be somewhat more efficient than <page>ents.GetAll</page>.
<note>The <page>GM:OnEntityCreated</page> and <page>GM:EntityRemoved</page> hooks are used internally to invalidate this function's cache. Using this function inside those hooks is not guaranteed to use an up-to-date cache because hooks are currently executed in an arbitrary order.</note>⤶
<warning>An error being thrown inside the <page>GM:OnEntityCreated</page> or <page>GM:EntityRemoved</page> hooks is likely to break this function. Make it certain that no addons are causing any errors in those hooks.</warning>⤶
</description>
<added>2023.10.13</added>
<realm>Shared</realm>
<file line="L33-L39">lua/includes/extensions/ents.lua</file>
<rets>
<ret name="" type="function">The Iterator Function from <page text="ipairs">Global.ipairs</page></ret>
<ret name="" type="table">Table of all existing <page text="Entities">Entity</page>. This is a cached copy of <page>ents.GetAll</page></ret>
<ret name="" type="number">
The starting index for the table of players.
This is always `0` and is returned for the benefit of [Generic For Loops](https://www.lua.org/pil/4.3.5.html)
</ret>
</rets>
</function>
<example>
<description>Example usage of this function.</description>
<code>
for _, ent in ents.Iterator() do
if ( ent:GetClass() == "prop_physics" ) then
ent:Remove()
end
end
</code>
<output>All physics props will be deleted.</output>
</example>