Forum rules - please read before posting.

Action script from a mouse/cursor Roll Over

I think I’m being stupid, but how do I trigger an action script from a mouse/cursor Roll Over? I have regular hotspots that have mouse click activated scripts but occasionally I want to run something as the mouse just passes over it. Can’t think how to do it in AC?

«1

Comments

  • You'd have to rely on custom scripting, but not much.

    To invoke an ActionList, call its Interact() method.

    To run code when a Hotspot is selected (i.e. mouse-overed), hook into the OnHotspotSelect custom event.

    Sample script (MouseOverInteraction.cs), that gets attached to the Hotspot:

    using UnityEngine;
    using AC;
    
    public class MouseOverInteraction : MonoBehaviour
    {
    
        public ActionList actionList;
    
        private void OnEnable ()
        {
            EventManager.OnHotspotSelect += MySelect;
        }
    
        private void OnDisable ()
        {
            EventManager.OnHotspotSelect += MySelect;
        }
    
        private void MySelect (Hotspot hotspot)
        {
            if (GetComponent <Hotspot>() == hotspot)
            {
                actionList.Interact ();
            }
        }
    
    }
    
  • Thank you, I'll give that a try :smile:

  • Hey @ChrisIceBox in OnDisable() it should unregister the MySelect right?!
    private void OnDisable () { EventManager.OnHotspotSelect -= MySelect; }

  • Yes - that was a typo.

  • The script works very well to trigger an assigned AC action list on mouse rollover – So I wondered if it possible to extend it to also create the action of On Mouse Leave?

    What I’m trying to achieve is when a mouse cursor enters the region of the hotspot, one action list runs (this it does), but when the play move the mouse off of the hotspot a different action list runs also.

    But... I’m not sure how to extend the code. This is seeming creating a hybrid of Trigger and Hotspot.

  • edited July 2019

    Triggers are not involved here - they involve physical objects in the scene being detected, not the mouse.

    The OnHotspotDeselect can similarly be used to run an ActionList when the mouse leaves a Hotspot.

    using UnityEngine;
    using AC;
    
    public class MouseOverInteraction : MonoBehaviour
    {
    
        public ActionList actionListOnEnter;
        public ActionList actionListOnExit;
    
        private void OnEnable ()
        {
            EventManager.OnHotspotSelect += MySelect;
            EventManager.OnHotspotDeselect += MyDeselect;
        }
    
        private void OnDisable ()
        {
            EventManager.OnHotspotSelect -= MySelect;
            EventManager.OnHotspotDeselect -= MyDeselect;
        }
    
        private void MySelect (Hotspot hotspot)
        {
            if (GetComponent <Hotspot>() == hotspot)
            {
                actionListOnEnter.Interact ();
            }
        }
    
        private void MyDeselect (Hotspot hotspot)
        {
            if (GetComponent <Hotspot>() == hotspot)
            {
                actionListOnExit.Interact ();
            }
        }
    
    }   
    
  • Thanks for the code. Unfortunately I seem to get my exit hotspot AC action list running when entering a hotspot with the mouse (as well as the action list on entering). Do I need to set my action lists to any specific setting? I tried alternating between run in background and pause.

  • No, it was my fault for coding too early in the morning. I've updated the above post - give it another go.

  • Works like a charm! Thank you :) <3 This is going to be such a useful extension to the Adventure Creator toolkit for my projects.

  • Hi, This script is working great for me until I return to the scene that I'm using it in. Then I get this error:

    MissingReferenceException: The object of type 'MouseOverInteraction' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.

    Any suggestions on how to keep the script available when I return to the scene?

  • Where did you place the script, and what is the error message in full?

  • Hi,

    Just FYI I'm using the script at the top of this thread, not the second one.

    I've put the script in Assets-Scripts folder of my project. The error occurs when I exit the scene which has the script attached to a hotspot, as soon as I rollover a "regular" hotspot (no scripts attached) in the new scene.

    Here's the log from the console:

    MissingReferenceException: The object of type 'MouseOverInteraction' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.Component.GetComponent[T] () (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/Component.bindings.cs:42)
    MouseOverInteraction.MySelect (AC.Hotspot hotspot) (at Assets/Scripts/MouseOverInteraction.cs:21)
    AC.EventManager.Call_OnChangeHotspot (AC.Hotspot hotspot, System.Boolean wasSelected) (at Assets/AdventureCreator/Scripts/Managers/EventManager.cs:386)
    AC.Hotspot.Select () (at Assets/AdventureCreator/Scripts/Logic/Hotspot.cs:773)
    AC.PlayerInteraction.ContextSensitiveClick_Process (System.Boolean doubleTap, AC.Hotspot newHotspot) (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:646)
    AC.PlayerInteraction.ContextSensitiveClick () (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:598)
    AC.PlayerInteraction.HandleInteractionMenu () (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:206)
    AC.PlayerInteraction.UpdateInteraction () (at Assets/AdventureCreator/Scripts/Controls/PlayerInteraction.cs:133)
    AC.StateHandler.Update () (at Assets/AdventureCreator/Scripts/Game engine/StateHandler.cs:214)

  • I'm using the script at the top of this thread, not the second one.

    Did you apply the correction discussed beneath it?

  • That fixed the errors. I should have read the entire thread throughly. Sorry about that but thanks for the quick response and your patience. The script works perfectly now.

  • Hi Chris, could be that I'm just dumb and not enough programmer but I could not make this work, I need the 2. type here. And maybe I just don't fully understand what does "To run code when a Hotspot is selected (i.e. mouse-overed), hook into the OnHotspotSelect custom event." mean.
    What I did was to create a new script named MouseOverInteraction.cs with the 2. type of code above but then I got 5 red error messages. What do I miss? Please help.

  • And maybe I just don't fully understand what does "To run code when a Hotspot is selected (i.e. mouse-overed), hook into the OnHotspotSelect custom event." mean.

    Custom events are a way of running your own additional code when AC performs common tasks - in this case, when a Hotspot is selected.

    See the tutorial I link to above in my first reply, as well as the Manual's "Custom events" chapter for details.

    What I did was to create a new script named MouseOverInteraction.cs with the 2. type of code above but then I got 5 red error messages. What do I miss? Please help.

    The steps sound right. What are the messages, exactly?

  • Thanks for the fast reply! I will check this tutorial.

    So when I create a new C#script and name it MouseOverInteraction.cs, without doing anything else, I already get all this which you see on this picture:

  • And when I copy paste the 2. code part above, I get this with 5 error msges.

  • Your screenshots are too small for me to make out the messages - can you copy/paste them from the Console?

    Though, it looks like a case of you already having a script named "MouseOverInteraction". If you're trying out the scripts separately, you'll need to name them differently. Rename both the filename and the classname to something different, e.g. MouseOverInteraction2.

  • Yes! I already had a script, but with this code but I named "mouse". Now I corrected, added to the hotspot game object and works perfectly! Thank you!

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.