Forum rules - please read before posting.

Spawning an addressable NPC

edited February 2023 in Technical Q&A

I have "Save asset references with Addressables" checked and I'm trying to spawn an NPC and have him persisted during scene changes.

After marking the NPC as an addressable, I can successfully spawn him using a "Object: Add or remove" action (entering the addressable name). But he isn't saved to the scene, so I added a RememberTransform (with "Save scene presence" checked and the name of the addressabe is entered). But this resulted in the following warning when running the "Object: Add or remove" action:
"Agent(Clone) cannot be instantiated, as it is already present in the scene. To allow for multiple instances to be spawned, assign a non-zero value in its Remember Transform component's 'Linked prefab ConstantID' field."

I don't really understand what the warning means, and I don't see any 'Linked prefab ConstantID' field on the RememberTransform.

AC: 1.76.3
Unity: 2021.3.19

Thanks!

Comments

  • edited February 2023

    Three things I noticed when digging deeper into this:

    1. If I comment out line 326-333 in ActionInstantiate, the spawning does actually work, so there's something there you might want to have a look at
    2. While the NPCs transform data is now persisted across scene changes, its facing direction is lost (even though it does have a RememberNPC component)
    3. When the character is spawned, the name of the gameobject is "Agent(Clone)", and when he is re-instantiated due to a scene switch, the name is just "Agent"
  • Thanks for the details. To be clear: the original warning appeared regardless of whether or not the NPC was actually present?

    While the NPCs transform data is now persisted across scene changes, its facing direction is lost (even though it does have a RememberNPC component)

    That sounds like a conflict between RememberTransform and RememberNPC, which both deal with the attached GameObject's Transform data.

    I'll look into this, but an alternative approach to this would be to enable Player-switching, convert the NPC to a Player, and use the Player: Teleport inactive Action to spawn them. When dealing with such Players, AC handles their scene presence and position automatically.

  • edited February 2023

    Thanks for the details. To be clear: the original warning appeared regardless of whether or not the NPC was actually present?

    Yes, it happens no matter what.

    That sounds like a conflict between RememberTransform and RememberNPC, which both deal with the attached GameObject's Transform data.

    Yes, I was surprised to see that RememberTransform was needed in order to have objects remembered in saves, perhaps a separate RememberScenePresence would make more sense.

    BTW: I tried removing the RememberNPC component, but I have the same problems remain.

    I'll look into this, but an alternative approach to this would be to enable Player-switching, convert the NPC to a Player, and use the Player: Teleport inactive Action to spawn them. When dealing with such Players, AC handles their scene presence and position automatically.

    Thanks for the workaround, but I already use player switching and I'd rather not mix up players and npcs.

    Thanks for looking into this, if you find it can be made to work with a few code modifications (or an updated package), please get back to me 👍

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.