How can I make a function act differently for different scenarios without a ton of extra scripting?

Hi, I’m making a platformer game and I want the player to be able to talk to people by clicking them. I obviously don’t want to make a unique script for each NPC, is there an easier way to do this? A component perhaps? Something to put in the player script?

Hello @JerBear264 !

If you are looking for a component for this, i don’t think it exists. What you need is a good script that can “think” a little to give different reactions/questions/conversation, with some random.

Create some properties that initializes by random, and depending on this properties the npc chose what to say.

@tormentoarmagedoom

To go into more detail, I have a player that can interact with NPCs via clicking, I want it so when you click, a speech bubble appears from the NPC that you clicked. I want to avoid the following:

  • Individual scripts for each NPC
  • What you listed (if statements or switch statements corresponding to each NPC)
    What is an easy alternative. Is there a component I can add to each this effect? (Changing reaction depending on what I click on)

You could try adding a tag to every npc which would be quicker than scripting for each one then on the player script check for the tag and if it equals whatever tag you put in then continue the actions. For example:

//Clicked being the npc clicked on and example will be the npc tag
if (clicked.tag == "example")
{
      //What you want to happen
}

Note that you will need to have multiple ifs for every NPC in the main script all within a update function.

Hope this helps you!