Is there any way to store AllPlayers[] includig NPC?

Hello!
I want to know there is any way to store all the NPCs active in level right now?
When I use playspace , they only provide Players not NPCs .
Thank you in advance!

Playspace: fort_playspace = GetPlayspace()
AllPlayers: []player = Playspace.GetPlayers()

Sure thing. You can utilize the NPC Spawner’s Spawned Event by subscribing to it with a function that will add your agents to an array of agents.

You can set up a new device called agent_tracker, or make this code live in your game manager or another device that will exist for the entire game instance.

You will need to get a hold of all of your NPC spawners. I suggest using a tag to get a hold of them. You can create the tag in global space, outside of any class by outdenting the tag as such:

Once you have the tag created, compile your verse in the editor. You can add this tag by adding a component to your npc spawner devices called ‘Verse Tag Markup’ and selecting your newly programmed tag from the dropdown list:

Back in the code, In the On Begin function of this device, get all of your tagged npc spawners and subscribe to their spawned events (the function returns an array of interfaces, so you have to iterate through it and cast them to your npc spawner device type):

Then we will need an array to hold all of the agents, and we code the function OnNPCSpawned that adds the agents to the array :

This way, the code is fired every time an npc is spawned and the agent is added to the array.

If you want all of the players who join to also be in this array, you can get them all on OnBegin and add them manually. You can debug all of this with a print command in the OnNPCSpawned function:

Completed Code :

Be sure to compile everything, and after assigning your tags to your devices, do a full ‘Push Changes’ (rather than just a Push Verse Changes), as I’ve found that when working with Tags it always needs a the full client update for things to register.

Hopefully this works and helps, let me know if you need any more help and good luck with your project :slight_smile:

2 Likes

Thank you soo much for all the details and kindness.
I understand now completely.
Wishing you a great day!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.