Forum rules - please read before posting.

Sending messages to the parent object/hotspot that just ran the action list

Hi all,

A confusing thing to explain but here goes.

I want to re-use an action called TALKTO_ACTION for 15 different "guests" in the game, but don't want to duplicate the action 15 times (one for each guest, as i might be adding more, and it seems inefficient)

The custom function (setQuestPic) I need to run is contained in a "guest" script, attached to each guest gameobject. This is the same gameobject in the scene that has the AC hotspot attached that just called the TALKTO_ACTION.

The issue is, If I use the existing Object>send message action, I have to select the specific gameobject (in the Object to Affect field).. which unfortunately then ties every TALKTO_ACTION to the same individual guest game object.

In the screenshot below, In theory, I'd like to set the field Object to affect = "The parent gameobject of the hotspot that just called this action" . This would make the same action re-usable for all guests, calling the custom guest script each time.

Any help.. or something simpler would be greatly appreciated.
thanks

Comments

  • edited November 2020

    You can use a Parameter of type GameObject to dynamically alter the "Object to affect" field value.

    I'm a little confused about the hierarchy though. You mention that your custom "guest" script is on the same GameObject as the Hotspot - yet you want to assign the parent of the Hotspot. Is the Hotspot not on the root?

    In any case, you can likely get what you need by also attaching the Set Interaction Parameters component. This component allows you to set an Interaction ActionList's parameter values - which are then assigned at the time the Interaction is triggered.

    If the GameObject parameter values you assign here all reference GameObjects in the object's hierarchy, you can also use this technique to prefab your game logic - since such references will be retained in prefabs. More on this workflow can be found in this tutorial.

    It's also good to be aware that parameter values can be set through script. If, for exampe, you hook into the OnHotspotInteract custom event, you can extract both the Hotspot being interacted with, and the ActionList being called, and use it to update that list's parameters before the list itself is run. That's essentially how the above component works, though a custom solution is there if you need something more specific.

    A typical script for that method:

    using UnityEngine;
    using AC;
    
    public class CustomInteractionParameter : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnHotspotInteract += OnHotspotInteract; }
        private void OnDisable () { EventManager.OnHotspotInteract -= OnHotspotInteract; }
    
        private void OnHotspotInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot == GetComponent <Hotspot>())
            {
                switch (hotspot.interactionSource)
                {
                    case InteractionSource.InScene:
                        button.interaction.GetParameter (0).stringValue = "Hello";
                        break;
    
                    case InteractionSource.AssetFile:
                        button.assetFile.GetParameter (0).stringValue = "Hello";
                        break;
    
                    default: break;
                }
            }
        }
    
    }
    
  • Chris, this is excellent and well explained as always.

    The set interaction parameters component, is something I'd totally missed, looks very useful, for prefabing so much logic.

    The "parent" bit I mentioned was confusing, your solution addresses what I was struggling with. I was wanting to run code, from a custom script attached to the same gameobject as the hotspot, and not be tied to a constant ID. (so I can duplicate the same behaviour many times)

    Your solution has given me great new knowledge which will allow this behaviour using the prefab approach in the tutorial.

    Perfect, thanks!

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.