Forum rules - please read before posting.

Random conversation option - is it possible?

Unity Ver: 2019.2.4f1
AC Ver: 1.71.3

Hello!

Is there a way to have a conversation option be random? In the sense of each time the conversation is displayed, the last choice would be random?

P

Comments

  • In what way? The same option with different label, or chosen at random from a selection of separate options? And only the last option?

    It's possible to modify which dialogue options are enabled through script, so you could randomly choose an option from a given set of indices. Probably best to do this when the OnStartConversation event is called:

    using UnityEngine;
    using AC;
    
    public class RandomConvOption : MonoBehaviour
    {
    
        public int[] randomOptionIDs;
    
        private void OnEnable () { EventManager.OnStartConversation += OnStartConversation; }
        private void OnDisable () { EventManager.OnStartConversation -= OnStartConversation; }
    
        private void OnStartConversation (Conversation conversation)
        {
            if (conversation == GetComponent <Conversation>() && randomOptionIDs.Length > 0)
            {
                int chosenIndex = Random.Range (0, randomOptionIDs.Length-1);
                int chosenID = randomOptionIDs[chosenIndex];
    
                foreach (int optionID in randomOptionIDs)
                {
                    ButtonDialog option = conversation.GetOptionWithID (optionID);
                    option.isOn = (option.ID == chosenID);
                }
            }
        }
    
    }
    
  • Hey Chris,

    Yeah, so my desire is to have this conversation option change every time the conversation is displayed. It would be a pool of options and each time it chooses one.

    Is there any facility to do this within Action Lists or is it definitely a code job?

    Thanks!

  • So, the label only. Not the result of clicking it?

    It's a code job if randomness is involved, but you can change the label of an option easily enough:

    option.label = "My new label";
    

    If you want it to literally be a random option (i.e. both label and result different), you'll need to define all available options in the Conversation itself and use/adapt the code earlier.

  • @ChrisIceBox Thanks so much! I've got this working with the code snippet above and attaching it to the conversation gameobject. Took me a while just to understand the flow and logic of it but got there!

    Also this way of setting a bool is brilliant;

    option.isOn = (option.ID == chosenID);

    Just isn't a structure I've ever used. Love it.

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.