Skip to content

Crash when creating actors from OnStart #844

Description

@Gopmyc

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:

  1. Create a new scene (File > New Scene)
  2. 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
  1. Add the script to Main Camera, which is the first actor of the scene
  2. Press Play
  3. 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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Good First IssueAn easy issue for new contributors

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions