Forum rules - please read before posting.

Randomizing NPC Names

Hey!

Im currently making a bouncer game where the bouncer has to check npc's identification to allow entry. Is there any way to assign random names to NPC's? Maybe if i have a text file with just a bunch of full names and the action list editor can pull a random name out of that list and assign it. Can you tell me the best way to tackle this?

Thanks

Comments

  • Welcome to the community, @JohnnySins.

    This'll require custom coding. Two scripts: first is RandomNames.cs:

    using UnityEngine;
    using AC;
    
    [CreateAssetMenu (menuName = "Random names")]
    public class RandomNames : ScriptableObject
    {
    
        public string[] names;
    
        public string GetRandomName ()
        {
            int randomIndex = Random.Range (0, names.Length);
            string randomName = names[randomIndex];
            return randomName;
        }
    
    }
    

    This allows for an asset file named "Random names" to be created from the Project window's Create menu. Use it to create a new asset and populate it with names.

    Second script is AssignRandomName.cs:

    using UnityEngine;
    using AC;
    
    public class AssignRandomName : MonoBehaviour
    {
    
        public RandomNames randomNames;
        public NPC npc;
        public bool assignOnStart;
    
        public void Start ()
        {
            if (assignOnStart)
            {
                AssignRandomName ();
            }
        }
    
        public void Assign ()
        {   
            npc.SetName (randomNames.GetRandomName (), -1);
        }
    
    }
    

    Attach to your NPC and assign the "Random names" asset and "NPC" Inspector fields. Either check "Assign On Start" to randomly assign a name when the scene begins, or use AC's Object: Send message or Object: Call event Actions to call the component's "Assign" function.

  • Legend Chris! Thanks for the fast response

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.