Forum rules - please read before posting.

[QUESTION] Object: send message and Prefabs

Hello there,
I've been fiddling around with my ActionLists, and I tried to call a method in my Inventory menu prefab: My inventory menu has a character image in the bottom left corner, and the character image must be chosen within a list. At the moment I've made a scene property with the character unique name and I extrapolate the value from a dictionary. For the time being, I'm calling the method within the Start() method of the script I attached to the menu prefab root gameobject. So far, so good.

What I've been trying to achieve was calling this method within the ActionList called on TurnOn of the Inventory menu (as the character may change within the same scene, and also as a simple test), but apparently I can't tell AC to send a message to an instance of a prefab, even though I've referenced the gameobject with the Constant ID. Is this an error on my side (i.e. I've made a mistake somewhere), or sending a message is not meant to be used on prefabs? I couldn't find any clarification on this so far...

Any help, as usual, is deeply appreciated!

Comments

  • edited December 2020

    It can work with prefab instances, yes. If an Action field references a prefab, at runtime it will rely on the prefab's scene instance - provided both the prefab and the instance share the same Constant ID value.

    This is typically just a case of checking Retain in prefab? in the prefab's component.

    However, the object in the scene must be active at the time - which won't be the case if the Menu it's a part of is "off". If the scene instance is disabled, it cannot be found, and so the original prefab will be referred to instead.

    The best way to run custom code when turning on a Menu is to hook into the OnMenuTurnOn custom event. If attached to a Unity UI menu prefab, you can rely on its Canvas to determine if its Menu was turned on:

    using UnityEngine;
    using AC;
    
    public class MenuEventExample : MonoBehaviour
    {
    
        public Canvas canvas;
    
        private void OnEnable () { EventManager.OnMenuTurnOn += OnMenuTurnOn; }
        private void OnDisable() { EventManager.OnMenuTurnOn -= OnMenuTurnOn; }
    
        private void OnMenuTurnOn (AC.Menu _menu, bool isInstant)
        {
            if (_menu == KickStarter.playerMenus.GetMenuWithCanvas (canvas))
            {
                // This menu was turned on
            }
        }
    
    }
    
  • edited December 2020

    Oh, ok! Thanks Chris for your explanation. The error I got is probably due to the fact that when the TurnOn ActionList is running the UI Prefab hasn't been activated yet (otherwise it should behave like you said, since the Retain in Prefab option is checked)

    I like your example code better, though, because it gave me another piece of information I'll surely use somewhere in my project.

    As a side note, I just noticed that the GetMenuWIthCanvas method doesn't show in my Intellisense although it's public... Any idea why this is happening? I'm using Visual Studio 2019 (not Code) with the latest update (v.16.8.3)

    EDIT: forget it, I was using the PlayerMenus class name instead of referencing the instance in Kickstarter. My bad!

    Keep it up!

  • Hey Chris, I'm sorry if I recicle this thread for asking you something else, but I was wondering: does a click on an interaction always close the associated menu?

    Because, even though I've selected the option to close interactions as "Via Script Only" and also checked the option "Unity UI blocks interaction and movement", whenever I choose the "look at" interaction of the hotspot (which should change a global variable value) the interaction menu fades out, which is not what I want. I'd like to close the hotspot menu only when I choose one of the other available interactions or with the right click of the mouse, but I don't understand why it closes by itself. Any thoughts?

  • It's something I've been looking into myself recently.

    The Interaction menu will close once gameplay is blocked - no matter where that block comes from (Interaction, Cutscene, Trigger etc).

    However, it should be the case that it should remain open (at least optionally so) if an Interaction run from it doesn't block gameplay - which is a case of setting the Interaction's When running property to Run In Background.

    I shall be looking to include such an option in the next AC release - though if you'd like to test it beforehand, PM me and we can discuss it in private.

  • Hi Chris,
    I'm not sure I'm expert enough with AC to be able to give you proper feedback while testing, but if you need help, sure!

    However, it should be the case that it should remain open (at least optionally so) if an Interaction run from it doesn't block gameplay - which is a case of setting the Interaction's When running property to Run In Background.

    I've just tried this solution, by unchecking the "Pause Game When Enabled" option of the interaction menu and by making the "look At" actionlist run in background... But to no avail. Just to be sure I've also brought forth the AC Status menu, to check if the game was paused somewhere along the test, but it doesn't seem like that (unless it's so fast that I didn't notice). Still, the interaction menu fades out while updating the text label.

  • Perhaps my wording could have been better. By "should be the case", I mean that I would like for this to be the case. This behaviour will be optional as part of the net update.

  • oh, ok, sorry. Anyway, I'm willing to test it beforehand if you think I can be of help (I'm rather new with AC)

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.