Forum rules - please read before posting.

Question about Component Variables

edited November 2019 in Technical Q&A

Hi Chris

Unity 2019.2.4f1 AC 1.69.5

What I almost always need to do in my AC projects is to store information about Actors. As a couple of examples I keep a track of which Scene they should currently appear in, and what their actual name is (which may differ from the label that initially gets set in their Prefab)

Clearly all this information COULD be stored in Global variables, but that gets very messy very quickly as the number of Actors, and the different amounts of information I want to store about them, increases during game development

In the past I've used various creaky roll-your-own methods to deal with the situation, but I was rather hoping that I'd be able to use the new (to me) Component Variables functionality to store this stuff

A quick play with the feature suggests that it wouldn't be suitable, however. It seems great for storing static Actor-related data that doesn't change during the course of the game, but as far as I can tell, if you try and update a Component Variable data item, what happens is:

  • If the Actor Prefab does NOT exist in the Scene, then the data is updated in the Actor's base Prefab

  • But, if the Actor Prefab DOES exist in the Scene as a cloned copy, then the data is updated in the Scene copy, not in the base Prefab

Have I got that right? If so, then the Component Variables won't be any use for my purposes, because most of the Actor data I want to maintain needs to be game-wide, not local to a Scene; and when updating data about Actors they almost always DO appear in the Scene where the update needs to take place, so it would be their local copy that gets updated not their base Prefab

Perhaps there's some way round this that I'm not seeing though?

Comments

  • Component variables certainly are designed to be changed over the course of the game, but the behaviour you're experiencing is as intended. While Component variables can be prefabbed, each scene instance of such a prefab is treated as its own set.

    As AC provide event hooks for the manipulation of variables, it would be possible to write a custom script that updates a prefab's Variables when a scene instance of it is updated, but I'd advise against updating prefabs at runtime.

    At the moment, I'd say the best way to store such data would be to rely on an array of a custom class, i.e.:

    [SerializeField] ActorData[] allActorData;
    
    [System.Serializable] class ActorData
    {
        public int actorID;
        public string actorName;
        public int currentScene;
    }
    

    Which you can then update/read from as necessary. When it's time to save/load the game, you could then serialize/deserialize this data to a single String Global variable. See, for example, the way the Remember Animator component stores layer weights into the layerWeightData string.

  • edited November 2019

    Thanks Chris. I'd already been doing something similar to your suggestion for storing Actor-related data in my older projects, and I guess I'll just have to bang on with that method for my latest projects too

    The potential big advantage of the Component Variable approach for me would have been that the data could have then been naturally integrated into Actionlists without any extra fussing about, so it seemed worth checking to see whether or not Component Variables could be used in the way that I'd envisaged (answer: not!)

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.