Description
Creating actors from a script's OnStart (same for OnAwake and OnEnable) can crash the engine when entering play mode.
Scene::Play loops over m_actors with iterators while calling these callbacks. If a script calls Scene:CreateActor there, m_actors can be reallocated and the loop keeps reading the old buffer, which ends in an access violation in Actor::IsActive.
It only crashes when the actor holding the script isn't the last one in the scene, because the loop stops right after the last actor. Scene::Update, FixedUpdate and LateUpdate don't have this problem since they iterate over a copy of m_actors. Doing the same in Scene::Play should fix it.
To Reproduce
Steps to reproduce the behavior:
- Create a new scene (File > New Scene)
- Create a script with the following content:
local Spawner = {}
function Spawner:OnStart()
for i = 1, 10 do
Scenes.GetCurrentScene():CreateActor("Spawned " .. i, "")
end
end
return Spawner
- Add the script to
Main Camera, which is the first actor of the scene
- Press Play
- See the crash (access violation in
Actor::IsActive)
Expected behavior
The actors are created and play mode starts normally, the same way it works when CreateActor is called from OnUpdate.
Screenshots
N/A
Description
Creating actors from a script's
OnStart(same forOnAwakeandOnEnable) can crash the engine when entering play mode.Scene::Playloops overm_actorswith iterators while calling these callbacks. If a script callsScene:CreateActorthere,m_actorscan be reallocated and the loop keeps reading the old buffer, which ends in an access violation inActor::IsActive.It only crashes when the actor holding the script isn't the last one in the scene, because the loop stops right after the last actor.
Scene::Update,FixedUpdateandLateUpdatedon't have this problem since they iterate over a copy ofm_actors. Doing the same inScene::Playshould fix it.To Reproduce
Steps to reproduce the behavior:
Main Camera, which is the first actor of the sceneActor::IsActive)Expected behavior
The actors are created and play mode starts normally, the same way it works when
CreateActoris called fromOnUpdate.Screenshots
N/A