Forum rules - please read before posting.

Pressing menu buttons through actionlist/scripting commands?

Hi there! Sorry to pop in with another question. I did a lot of research on this and am formally stuck and am wondering if it's even possible.

I have a piano minigame set up with an adventurecreator menu as a base, using variables to affect the outcome of the minigame. What I'm struggling with however is the "watch" aspect of the minigame.

Essentially the minigame is supposed to work like this:

1) The NPC plays keys on the piano in a specific sequence
2) You (the player) follow the keys in that sequence--variables are used to check whether the key order is correct or not
3) You win the minigame if you press in the correct sequence

I am able to get 2 and 3 to work, but I cannot for the life of me figure out how to simulate a button being pressed for the sake of the first part of the game. I've already set up both Unity input keys and adventurecreator input keys that are functioning (that is, the piano key buttons are pressed when I input the key on my keyboard), but I cannot figure out how to trigger them automatically through the script.

Help appreciated!

Thanks again.

Comments

  • When you say "simulate a button being pressed", are you referring to a Unity input, or a menu button that visually appears in the menu?

    Input buttons can be simulated through script:

    AC.KickStarter.playerInput.SimulateInputButton ("MyInputButtonName");
    

    If you're talking about menu buttons, is this with the intent of showing the player which keys are being pressed? If it's an AC menu, then you can invoke the MenuButton class's ShowClick function, which shows it's "Click texture" and fades it out over time. Something like:

    (AC.PlayerMenus.GetElementWithName ("MyMenu", "MyButton") as AC.MenuButton).ShowClick ();
    

    To do either of the above over time would be a bit tricky, but possible using e.g. coroutines. Best way I think would be to first switch over to Unity UI for your menu's display (if not already), so that you can then animate the button's appearance using a standard Animator workflow. You can then control playback of the animation using the regular Object: Animate Action.

  • edited August 2019

    Hi Chris,

    Yes, the intent is showing the player which keys to press in what order as a sort of "tutorial" and then you repeat what you are shown. It is indeed through an AC menu. Will the ShowClick function also play the sound associated with the button click? I have the piano keys' appropriate notes linked to the on click sound through the AC menu editor. If it's useful, I can record a quick video showing how the minigame is currently functioning (right now I just have a wait command in place of the "tutorial" aspect I can't figure out, but the rest is 100% functioning.)

    For additional clarity, what is the appropriate way to call this function? I assume I'm supposed to add a component to something with a script that calls this function? I'm sort of learning Unity as I plug away at this game so sorry if this is an obvious question haha.

    Thanks again for your help!

  • If it's useful, I can record a quick video showing how the minigame is currently functioning

    Thanks, but I think I follow you.

    Will the ShowClick function also play the sound associated with the button click?

    No, ShowClick is visual only. However, you can extract the button's audio clip and play it manually with:

    AudioClip clickSound = (AC.PlayerMenus.GetElementWithName ("MyMenu", "MyButton") as AC.MenuButton).clickSound;
    AC.KickStarter.sceneSettings.PlayDefaultSound (clickSound, false);
    

    For additional clarity, what is the appropriate way to call this function?

    While it may be worth considering the use of Timeline to handle this (though the menu would need to be a Unity UI one), you could feasibly place the above code into a custom Action. Try something like this would probably do it:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionMenuButtonSimulate : Action
        {
    
            public string menuName;
            public string elementName;
    
    
            public ActionMenuButtonSimulate ()
            {
                this.isDisplayed = true;
                category = ActionCategory.Menu;
                title = "Simulate button click";
            }
    
    
            override public float Run ()
            {
                if (menuName != "" && elementName != "")
                {
                    MenuButton myButton = AC.PlayerMenus.GetElementWithName (menuName, elementName) as AC.MenuButton;
                    myButton.ShowClick ();
                    KickStarter.sceneSettings.PlayDefaultSound (myButton.clickSound, false);
                }
    
                return 0f;
            }
    
    
            #if UNITY_EDITOR
    
            override public void ShowGUI (List<ActionParameter> parameters)
            {
                menuName = EditorGUILayout.TextField ("Menu name:", menuName);
                elementName = EditorGUILayout.TextField ("Button element name:", elementName);
    
                AfterRunningOption ();
            }
    
            #endif
    
        }
    
    }
    

    Place this in a script named "ActionMenuButtonSimulate.cs" and place it in a custom Actions folder as described by this tutorial. This new Menu: Simulate button click should then let you fake button clicks from an ActionList.

  • Hey Chris,

    This works perfectly! Thank you very much for spending time whipping that code up for me, it's a life saver!

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.