Forum rules - please read before posting.

What's the correct way to get a global variable?

edited July 2023 in Technical Q&A

I have a Close Up menu that comes up when you interact with something that needs to be...close up. Like a hand crank that requires you to stop being in fps mode (no first person movement, unlocked cursor). I have an exit button that runs an action list that returns you to normal mode (re-enables first person movement, locks cursor) and hides itself.

To enter this mode/menu, the player interacts with a hotbox that I hide. I want to unhide that hotbox when I exit. To accomplish this I pass the value of that hotbox to a global variable. I want to get that variable so I know which object to re-enable on exiting.

I see how I can check a global variable and see if it matches a condition. I want to just get the identity of the object housed in this variable. I tried copying the global to a component variable parameter in the action list. But I don't have a component variable assigned to the menu (can you even do that?). Either way it doesn't seem to grab the identity.

If this is the wrong way to accomplish this, what's the right way?

https://imgur.com/a/Ygt2fdx

Comments

  • edited July 2023

    image of my action list...doesn't seem to want to show up. Oh there it is.

  • I've also tried copying its value to a global variable parameter. My stumbling block is that I can't access a parameter in the hotspot enable/disable action.

  • What is the "Variable type" of the Global Variable?

    Using a Variable, however, shouldn't strictly be necessary - you can use parameters instead.

    An ActionList asset's parameters can be set independently of it being run. Both the ActionList: Set parameter and ActionList: Run Action both support the ability to set parameter values without causing the ActionList itself to run.

    If you declared a GameObject parameter in your ActionList, you could set that to the Hotspot in question, and then use that to override the field in the Hotspot: Enable or disable Action.

    One other thing that might be useful: a Hotspot's interactivity can be limited to a specific camera in their Inspector. If your close-up involves switching camera, you may be able to prevent the Hotspot from being interactive automatically by assigning the "regular" camera in this field.

  • It would be a game object type variable.

    But I had no idea you could just change a parameter on the sky like that. That’s a good trick.

    The problem I was having was that a bunch of actions done use parameters. The send message one, for example. That’s how I imagined I would turn it back on. Shouldn’t all actions that take game objects use parameters?

    Buuut you are absolutely correct. Telling the hotspot to ignore my new camera is the simplest solution in this case! I’ll do that.

    As always, thanks!
  • oh, how do I make it limit to the player's camera if the player is a prefab? I tried dragging the prefab into the scene and dragging the camera into that slot, but it didn't stick.

  • edited July 2023

    Shouldn’t all actions that take game objects use parameters?

    It's not automatic - I have to hard-code it, so it's possible I've missed some fields. Which Action/field is missing a parameter? The Object: Send message Action's Object to affect field should accept a GameObject parameter, however.

    how do I make it limit to the player's camera if the player is a prefab?

    You'd either need to place the Player's prefab within the scene file, or use a script to create the link at runtime - something like:

    using UnityEngine;
    using AC;
    
    public class LimitToPlayerCamera : MonoBehaviour
    {
    
        public Hotspot hotspot;
    
        private void OnEnable () { EventManager.OnSetPlayer += OnSetPlayer; }
        private void OnDisable () { EventManager.OnSetPlayer -= OnSetPlayer; }
    
        private void OnSetPlayer (Player player)
        {
            _Camera camera = player.GetComponentInChildren<_Camera> ();
            hotspot.limitToCamera = camera;
        }
    
    }
    
  • would a viable solution be to make the camera a global variable and find a way to stick it into a parameter?

    putting the character prefab in each scene seems inefficient. I'm currently using the player start to spawn the player.

  • I tried the script and I'm getting this error:

    Error CS0029 Cannot implicitly convert type 'AC._Camera[]' to 'AC._Camera'

    I'm also currently using AC version1.75.5, if that helps.

  • Sorry, small typo - I've corrected it now.

  • Perfect! Thank you. It works!

    So maybe I'm working with an incorrect mental model of how AC works. I'm working with the assumption that I'm building the world out of re-usable prefabs, and the character is also a spawned prefab using the player start). This makes things a lot more modular, but I have had a few stumbling blocks using this method. Setting up actionlists to work with parameters is a bit wonkier, requires more paperwork up front. I don't know what I'm saying here, other than it would be great to streamline this method as it seems like the 'correct' way to build videogames to me. But maybe I'm approaching AC from a sideways perspective.

    Once small example would be this script you wrote for me. If it were 'normal' to build the game my way, actionlists would be happier to deal with spawned characters.

    Curious what you think about this. And thanks again.

  • Indeed, making logic more modular with prefabs is best for making your workflow more efficient, and I encourage the use of it when possible.

    The main kink when it comes to AC is that ActionLists can't (by default, see below) exist in prefabs - hence the need for ActionList assets and parameters.

    Parameter values can be set in a number of ways:

    • Actions
    • Dedicated components (Set Interaction Parameters etc)
    • Events (Event Runner / Events Editor) in v1.78

    A tutorial on working with parameters and prefabs can be found here. It's quite involved, but may offer the kind of workflow you're looking for.

    Regarding ActionLists and prefabs: this is something I've been looking to allow for, which technically has been possible ever since Unity introduced their [SerializeReference] keyword.

    It is actually possible to have ActionList prefabs working - but a word of caution: it breaks all existing scene-based ActionLists (hence it being a hidden feature). To try it out, just add AC_ActionListPrefabs as a Scripting Define Symbol - but be sure to only do so in a new project!

  • Woah, what a revelation. Will I be brave enough to try this? Thanks for the info. Definitely something to consider.

    Events are also something I hadn't considered. That's a great tool for the toolbox.

  • Will I be brave enough to try this?

    If you give in a spin, let me know how it goes - I'd welcome any feedback.

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.