Forum rules - please read before posting.

Suggestion: Action to change a character's speech menu placement child

edited December 2021 in Technical Q&A

It would be great to have an action for changing this to another Transform.

I have times when the characters should change speaking pos depending on whether they are standing/laying/sitting and depending on which way they are facing.
I think having an action that can assign another Transform would be better than changing the actual pos of the existing one, since there would be no need to store away the original pos when setting it back. It would also be great to be able to say "release" to return it to the original/previous position.

Thanks

Comments

  • The Transform can be set through script by updating the Char script's speechMenuPlacement variable.

    Sample Action:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionSpeechTransform : Action
        {
    
            public AC.Char character;
            public bool isPlayer;
            public Transform newTransform;
    
    
            public override ActionCategory Category { get { return ActionCategory.Character; }}
            public override string Title { get { return "Change speech placement"; }}
    
    
            public override float Run ()
            {
                if (newTransform == null)
                {
                    if (isPlayer && KickStarter.player)
                    {
                        KickStarter.player.speechMenuPlacement = newTransform;
                    }
                    else if (!isPlayer && character)
                    {
                        character.speechMenuPlacement = newTransform;
                    }
                }
                return 0f;
            }
    
    
            public override void Skip ()
            {
                Run ();
            }
    
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {
                isPlayer = EditorGUILayout.Toggle ("Is player?", isPlayer);
                if (!isPlayer)
                {
                    character = (AC.Char) EditorGUILayout.ObjectField ("Character:", character, typeof (AC.Char), true);
                }
                newTransform = (TrackData) EditorGUILayout.ObjectField ("New transform:", newTransform, typeof (Transform), true);
            }
    
            #endif
    
        }
    
    }
    

    I'd still recommend updating the position of a single transform, and this can be done through animation. A sub-layer in your character's Animator could have animations for each of the positions the transform could appear at, and this state would be saved in the Remember Animator component as well.

  • edited December 2021
    Thanks, but there are two problems I'm trying to solve:
    * saving the pos
    * being able to change back to the original pos

    If I use this action it will be easy to change back to the original pos, but the fact that the transform was changed won't be persisted in saves.

    If I change the pos of the existing transform, it will be persisted in saves but there's no easy way to return to the original pos.
  • Attaching the Remember Transform component will save any change in position.

    To save a change in transform, you just need a custom Remember component - see this tutorial.

    using UnityEngine;
    
    namespace AC
    {
    
        public class RememberSpeechChild : Remember
        {
    
            public override string SaveData ()
            {
                SpeechChildData data = new SpeechChildData();
                data.objectID = constantID;
                data.savePrevented = savePrevented;
    
                AC.Char character = GetComponent<AC.Char> ();
                if (character)
                {
                    ConstantID cidComponent = character.speechMenuPlacement.GetComponent<ConstantID> ();
                    if (cidComponent)
                    {
                        data.transformID = cidComponent.constantID;
                    }
                }
    
    
                return Serializer.SaveScriptData <SpeechChildData> (data);
            }
    
    
            public override void LoadData (string stringData)
            {
                SpeechChildData data = Serializer.LoadScriptData <SpeechChildData> (stringData);
                if (data == null) return;
                SavePrevented = data.savePrevented; if (savePrevented) return;
    
                if (data.transformID != 0)
                {
                    ConstantID cidComponent = Serializer.GetGameObjectComponent (data.transformID, gameObject);
                    if (cidComponent)
                    {
                        GetComponent<AC.Char> ().speechMenuPlacement = cidComponent.transform;
                    }
                }
            }
    
        }
    
    
        [System.Serializable]
        public class SpeechChildData : RememberData
        {
    
            public int transformID;
    
            public SpeechChildData () { }
    
        }
    
    }
    
  • Ok, 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.