Forum rules - please read before posting.

Create marker at runtime associated with spawned NPC prefab

edited January 2022 in Technical Q&A

I'm looking for some advice on how to achieve the below scenario

  • I'm dynamically generating the scene at runtime using Dungeon Architect (DA). This means I cannot place AC configuration objects like markers in the scene at design time and cannot refer to scene-based objects when setting up AC because they are created at runtime.
  • What I'm trying to achieve... When the player enters the DA-generated room, all NPCs in that room will start following the player around the room, then when the player leaves the room I would like each NPC to move back to the position it was at when the scene was generated. The purpose of this is to build in a time factor for the player to get through the room.

What I have achieved so far...

  • The scene is generating using DA, a single player prefab is spawned and multiple/random NPC prefabs are spawned in each DA-generated room. Inventory and decoration prefab objects are also spawned.
  • I can attach hotspots to the prefabs and interact at runtime with the spawned objects like doors, NPCs, inventory and decorations.

To get the NPCs to follow/unfollow the Player....

  • A trigger object prefab is spawned in the doorway between rooms with "On enter" and "On exit" trigger components calling ActionList Assets.
  • The ActionLists use Object/SendMessage to call EnemyMovement script methods "StartFollowingPlayer" (on exiting trigger) or "StopFollowingPlayer" (on entering trigger).
  • Within the script I have successfully used DA API to identify which NPC objects are in the previous/next room (as the Player moves through the trigger).

What's next...

  • I believe I can get the NPC to start following the Player using AC APIs but haven't yet tried.
  • I don't know how to identify the starting position (at the time DA generated the scene) of each NPC in the previous room. If I have this data I might be able to use AC API to instruct the NPC to move to that position. I'm waiting on advice from DA support to see if this data is available. Is there a way in AC to capture the starting position when the NPC prefab is spawned into the scene?

I was thinking the role of DA in this scenario is to generate the scene and spawn objects while the role of AC is to build the gameplay logic... so NPC movement logic would be setup using AC functionality... hence asking the question in this forum.

Does what I have described above sound like a reasonable start?
How would you approach this?

Cheers, Randall.

Comments

  • To clarify: a DA-generated scene has multiple rooms?

    You should be able to record any object's starting position by attaching a script that records it in its Start function, i.e.:

    Vector3 startPosition;
    void Start ()
    {
        startPosition = transform.position;
    }
    

    To have an NPC snap back to this position, an additional function can be used inside this same script:

    public void ReturnToStart ()
    {
        GetComponent<AC.NPC> ().Teleport (startPosition);
    }
    
  • Thanks for the quick response Chris. Yes, the number of rooms in a DA-generated scene is variable based on the design-time configuration, then the actual layout is randomly generated at runtime with spawned objects placed randomly within configured limits. Quite cool functionality for generating the scene and seems to fit quite well with AC gameplay logic attached to prefabs.

    I'm using a ceiling trapdoor with ladder prefab as the spawned DA "goal" with an AC actionlist attached which restarts the scene, to given the impression of moving up through multiple levels of a dungeon. Once I've got the base character movements working, I will play with using a Global AC variable to hold the "level" number so I can perhaps spawn different objects or different number of objects such as enemies or maybe change the DA layout configuration limits if that's possible as the levels increase.

    I'll use your suggestion but look at using MoveToPoint() so the NPC's walk back to the start position not teleport. In case the player turns around and looks through the door opening, it would be a bit weird to have the NPCs snap back.

    Cheers!

  • Follow up newb question if I may. How would I call ReturnToStart() from a different script when I am looping through a list of NPC gameobjects in the room the player is about to leave?

  • If you have a list of NPCs already defined, you can iterate through it and reference the above script with GetComponent:

    foreach (NPC npc in myNPCList)
    {
        npc.GetComponent<StartPositionScript>().ReturnToStart ();
    }
    

    Where "StartPositionScript" is renamed to the name of the script you placed the function in.

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.