Forum rules - please read before posting.

Hotspot: Change Interaction. Disabling last used use interaction through script.

edited December 2021 in Technical Q&A

What I want to happen is every time a player interacts with a hotspot, that will run a script which plays a message, levels up a particular stat, then disables that particular interaction.

So if I look at a sign it will play the sign message, level up the Knowledge stat, then disable being able to look at the sign again. The player can still 'use hand' the sign or or 'talk to' sign or use an item on the sign etc.

Here is the interaction script for looking at the sign (as an example). This uses the Hotspot: change interaction to disable looking at the sign again, but I want to do this automatically by script rather than having to set this every time I play a message.
https://www.dropbox.com/t/5BxU5n8Ud3SIIXYW

That then sets the parameters of this actionlist asset:
https://www.dropbox.com/t/Fdxw03Hdn6rsmdFs

Which then sends a message to this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AC;

public class Code_To_Stat : MonoBehaviour
{

GVar MP = GlobalVariables.GetVariable(103);
GVar MaxMP = GlobalVariables.GetVariable(104);

GVar increaseAmount = GlobalVariables.GetVariable(122);
GVar knowledge = GlobalVariables.GetVariable(22);
GVar climbing = GlobalVariables.GetVariable(8);
GVar topbarLeftText = GlobalVariables.GetVariable(46);
//GVar currentAction = GlobalVariables.GetVariable(2);

private void Start()
{
    climbing.IntegerValue = 3;
}

public void Increase(int StatToIncrease)
{
    //Knowledge
    if (StatToIncrease == 11)
    {
        if (MP.IntegerValue < increaseAmount.IntegerValue)
        {
            StartCoroutine(HeadHurts());
        }
        else
        {
            HPSPMP(3);
            knowledge.IntegerValue += increaseAmount.IntegerValue;
            topbarLeftText.SetStringValue("Knowledge " + knowledge.IntegerValue);
            StartCoroutine(Dialogue());
        }
    }



}

void HPSPMP (int num)
{
    //Decrease MP, Increase MaxMP
    if (num == 3) {
        MP.IntegerValue -= increaseAmount.IntegerValue;
        MaxMP.IntegerValue += increaseAmount.IntegerValue;
    }
}




private IEnumerator HeadHurts()
{

    KickStarter.stateHandler.EnforceCutsceneMode = true;
    Speech speech = AC.KickStarter.dialog.StartDialog(null, "You're too mentally exhausted to continue doing that.");

    while (speech.isAlive)
    {

        yield return null;

    }

    KickStarter.stateHandler.EnforceCutsceneMode = false;

    StopCoroutine(HeadHurts());
}

private IEnumerator Dialogue()
{
    KickStarter.stateHandler.EnforceCutsceneMode = true;
    Speech speech = AC.KickStarter.dialog.StartDialog(null, GlobalVariables.GetStringValue(3));
    while (speech.isAlive)
    {

        yield return null;

    }

    KickStarter.stateHandler.EnforceCutsceneMode = false;
    StopCoroutine(Dialogue());
}

private void TurnOffHotSpotInteraction()
{
    Hotspot hotspot = KickStarter.playerInteraction.GetLastOrActiveHotspot();
    //hotspot.GetPreviousInteraction();

    //Button interactionToClose = hotspot.GetUseButton(hotspot.GetPreviousInteraction());
    //interactionToClose.isDisabled = true;

}

}

The commented parts are where I have tried and failed to get the script to turn off the particular interaction just used.

Unity 2021.2.4
AC 1.74.4

Comments

  • You can hook into the OnHotspotInteract custom event to disable a Look interaction at the time it's run:

    using UnityEngine;
    using AC;
    
    public class DisableLookInteractions : MonoBehaviour
    {
    
        private int lookIconID = 1; // Change this to your Examine cursor ID
    
        private void OnEnable () { EventManager.OnHotspotInteract += OnHotspotInteract; }
        private void OnDisable () { EventManager.OnHotspotInteract -= OnHotspotInteract; }
    
        private void OnHotspotInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot.GetButtonInteractionType (button) == HotspotInteractionType.Examine ||
                (hotspot.GetButtonInteractionType (button) == HotspotInteractionType.Use && button.iconID == lookIconID))
            {
                hotspot.SetButtonState (button, false);
            }
        }
    
    }
    

    When a single instance of this script is placed in a scene, it'll disable the Look interaction for any Hotspot when run.

    Alternatively, you could adapt it to be placed only on specific Hotspots you want it to affect. This approach would also let you perform whatever additional changes you wanted, e.g. call your Code_To_State script, directly:

    using UnityEngine;
    using AC;
    
    public class DisableLookInteractions : MonoBehaviour
    {
    
        public int statToIncrease;
        private int lookIconID = 1; // Change this to your Examine cursor ID
    
        private void OnEnable () { EventManager.OnHotspotInteract += OnHotspotInteract; }
        private void OnDisable () { EventManager.OnHotspotInteract -= OnHotspotInteract; }
    
        private void OnHotspotInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot.gameObject != gameObject)
            {
                return;
            }
    
            if (hotspot.GetButtonInteractionType (button) == HotspotInteractionType.Examine ||
                (hotspot.GetButtonInteractionType (button) == HotspotInteractionType.Use && button.iconID == lookIconID))
            {
                hotspot.SetButtonState (button, false);
                KickStarter.player.GetComponent<Code_To_Stat> ().Increase (statToIncrease);
            }
        }
    
    }
    
  • Thanks so much (again).

    I ended up adding your:

    private void OnEnable() { EventManager.OnHotspotInteract += OnHotspotInteract; }
    private void OnDisable() { EventManager.OnHotspotInteract -= OnHotspotInteract; }

    private void OnHotspotInteract(Hotspot hotspotPar, AC.Button buttonPar)
    {
    // Set the variables from the parameters
        hotspot = hotspotPar;
        button = buttonPar;
    
    }
    

    to my Code_To_Stat script on the player, so that I could use the:

    hotspot.SetButtonState(button, false);

    to disable interactions as needs be.

  • It seems that you made this protected in AC 1.75.2
    "error CS0122: 'Hotspot.useButton' is inaccessible due to its protection level"

    My script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using AC;

    public class RemoveInteractionFromHotspot : MonoBehaviour
    {

    public Hotspot hotspot;
    

    public void RemoveInteraction()
    {
    //Not working in latest AC
    hotspot.useButton.interaction.enabled = false;
    }
    }

  • edited May 2022

    useButton was protected as it's a legacy variable - it hasn't been used in years.

    Your code also attempts to disable the Interaction component - this is separate from the enabled state of the Use button itself.

    To disable the first-found Use interaction, call:

    hotspot.SetButtonState (hotspot.GetFirstUseButton (), false);
    
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.