Forum rules - please read before posting.

Override "Use" Syntax

2»

Comments

  • edited January 2020

    Here are @KevRev 's scripts to include both the verb and the preposition.

    InvManager.cs

    using UnityEngine;
    using System.Collections.Generic;
    using AC;
    
    [AddComponentMenu("Adventure Creator/Hotspots/Inventory Action Manager")]
    [ExecuteInEditMode]
    [RequireComponent(typeof(Hotspot))]
    //[RequireComponent(typeof(InvItem))]
    public class InvManager : MonoBehaviour
    {
        public List<ItemOverride> itemOverrides = new List<ItemOverride>();
        private Hotspot thisHotspot;
        public List<string> invButtons = new List<string>();
        private bool buttonFound;
        private bool hotSpotMatch;
        private InventoryManager inventoryManager;
    
        private void OnEnable()
        {
            EventManager.OnHotspotSelect += OnHotspotSelect;
            EventManager.OnHotspotDeselect += OnHotspotDeselect;
        }
    
        private void OnDisable()
        {
            EventManager.OnHotspotSelect -= OnHotspotSelect;
            EventManager.OnHotspotDeselect -= OnHotspotDeselect;
        }
    
        private void OnHotspotSelect(Hotspot hotspot)
        {
            hotSpotMatch = false;
            foreach (ItemOverride itemOverride in itemOverrides)
            {
                if (hotspot.name == itemOverride.hotspot)
                {
                    hotSpotMatch = true;
                }
            }
    
            if (hotSpotMatch==true)    
            {
                if (KickStarter.runtimeInventory.SelectedItem != null)
                {
                    foreach (ItemOverride itemOverride in itemOverrides)
                    {
                        if (itemOverride.invitem == KickStarter.runtimeInventory.SelectedItem.label)
                        {
                            KickStarter.cursorManager.hotspotPrefix1.label = itemOverride.overrideSyntax1;
                            KickStarter.cursorManager.hotspotPrefix2.label = itemOverride.overrideSyntax2;
                            // Use this to set an ID value for the Editor
                        }
                    }
                }
            }
            hotSpotMatch = false;
        }
        private void OnHotspotDeselect(Hotspot hotspot)
        {
            KickStarter.cursorManager.hotspotPrefix1.label = "Use";
            KickStarter.cursorManager.hotspotPrefix2.label = "on";
        }
    
        private void Update()
        {
            GameObject g = GameObject.Find(this.name);
            Hotspot thisHotspot = g.GetComponent<Hotspot>();
            inventoryManager = AdvGame.GetReferences().inventoryManager;
    
            foreach (Button invButton in thisHotspot.invButtons)
            {
                bool invFound = false;
                var j = 0;
                foreach (ItemOverride itemOverride in itemOverrides)
                {
                    if (inventoryManager.items[invButton.invID].label.ToString() == itemOverride.invitem) invFound = true;
                    j++;
                }
                if (invFound == false)
                {
                    itemOverrides.Add(new ItemOverride(inventoryManager.items[invButton.invID].label.ToString(), "Use", "On", thisHotspot.name));
                }
            }
            for (int j = 0; j < itemOverrides.Count; j++)
            {
                buttonFound = false;
                foreach (Button invButton in thisHotspot.invButtons)
                {
                    if (inventoryManager.items[invButton.invID].label.ToString() == itemOverrides[j].invitem.ToString())
                    {
                        buttonFound = true;
                    }
                }
                if (buttonFound == false)
                {
                    itemOverrides.RemoveAt(j);
                }
            }                       
        }
    }
    
  • ItemOverride.cs

    using System;
    
    [Serializable]
    public struct ItemOverride
    {
        public string hotspot;
        public string invitem;
        public string overrideSyntax1;
        public string overrideSyntax2;
    
        public ItemOverride(string newInvID, string newSyntax1, string newSyntax2, string newHotspot)
        {
            invitem = newInvID;
            overrideSyntax1 = newSyntax1;
            overrideSyntax2 = newSyntax2;
            hotspot = newHotspot;
        }
    
    }
    
  • InvManagerEditor.cs

    using UnityEngine;
    using UnityEditor;
    using UnityEditorInternal;
    using AC;
    
    [CustomEditor(typeof(InvManager))]
    public class InvManagerEditor : Editor
    
    {
        private ReorderableList list;
    
        private void OnEnable()
        {
            list = new ReorderableList(serializedObject,
                serializedObject.FindProperty("itemOverrides"),
                true, true, true, true);
            list.drawHeaderCallback = (Rect rect) => {
                EditorGUI.LabelField(rect, "Inventory Action Syntax Overrides");
            };
    
            list.onAddCallback = (ReorderableList l) => {
                var index = l.serializedProperty.arraySize;
                l.serializedProperty.arraySize++;
                l.index = index;
                var element = l.serializedProperty.GetArrayElementAtIndex(index);
                element.FindPropertyRelative("hotspot").stringValue = Selection.activeGameObject.name;
                element.FindPropertyRelative("invitem").stringValue = "Item";
                element.FindPropertyRelative("overrideSyntax1").stringValue = "Use";
                element.FindPropertyRelative("overrideSyntax2").stringValue = "On";
            };
    
            list.onCanRemoveCallback = (ReorderableList l) => {
                return l.count > 1;
            };
    
            list.onRemoveCallback = (ReorderableList l) => {
                if (EditorUtility.DisplayDialog("Warning!",
                    "Are you sure you want to delete the override?", "Yes", "No"))
                {
                    ReorderableList.defaultBehaviours.DoRemoveButton(l);
                }
            };
    
            list.drawElementCallback =
                (Rect rect, int index, bool isActive, bool isFocused) => {
                var element = list.serializedProperty.GetArrayElementAtIndex(index);
                rect.y += 2;
                EditorGUI.PropertyField (new Rect(rect.x, rect.y, 70, EditorGUIUtility.singleLineHeight),element.FindPropertyRelative("overrideSyntax1"), GUIContent.none);
                EditorGUI.LabelField    (new Rect(rect.x + 80, rect.y, 150, EditorGUIUtility.singleLineHeight),element.FindPropertyRelative("invitem").stringValue);
                EditorGUI.PropertyField (new Rect(rect.x + 200, rect.y, 60, EditorGUIUtility.singleLineHeight),element.FindPropertyRelative("overrideSyntax2"), GUIContent.none);
                EditorGUI.LabelField    (new Rect(rect.x + rect.width - 140, rect.y, 140, EditorGUIUtility.singleLineHeight),element.FindPropertyRelative("hotspot").stringValue);
            };
        }
    
        public override void OnInspectorGUI()
        {
    
            serializedObject.Update();
            list.DoLayoutList();
            serializedObject.ApplyModifiedProperties();
    
    
        }
    
    
    
    }
    
  • I'm not entirely sure how to implement the ITranslatable interface here though. If anyone who actually knows C# could give me a hand, I'd be really grateful.

  • MonoBevahiour scripts that implement ITranslatable will be picked up by the Speech Manager provided that it an instance of it is in one of your scenes.

    It looks like your InvManager script is what needs to implement this - though bear in mind you could also store the text in Global String Variables instead. These can be translated automatically, and you can then read their values in the game's current language through script - see the Manual's "Variable scripting" chapter for more.

  • How would I go adapting the script so that it can be used to affect Use interactions instead of Inventory Interactions? The goal is to be able to, depending on the hotspot, read "Look at" or "Marvel at" or "Walk to" or "Shiver at" without the need to create a cursor for each of those, just replacing the text to display when choosing a classic "use" interaction.

    Besides that one, anoter of the reasons I am asking is because I have 8 Arrow cursors (named Arrow Up, Arrow UpRight, etc). I need the names to be different for backend purposes, but on the front end I would like all of them to display "Walk to scene", instead of "Arrow DR scene" as it displays right now. That's why I believe it's a good idea to be able to control the text displayed depending on the Hotspot.

    I reckon the script can't be that different from the one we have right now, but I have no clue where to start adapting it.

    Thanks!!!

  • For what backend purposes do you need the cursor names to be different?

    You shouldn't need a script so complicated as the above to deal just with Hotspots - the following, attached to each Hotspot you want to override, should do it:

    using UnityEngine;
    using AC;
    
    public class SetCustomIconLabel : MonoBehaviour
    {
    
        public int iconID;
        public string newIconLabel;
        private string originalLabel;
    
    
        private void Start ()
        {
            CursorIcon icon = KickStarter.cursorManager.GetCursorIconFromID (iconID);
            originalLabel = icon.label;
        }
    
    
        private void OnEnable ()
        {
            EventManager.OnHotspotSelect += OnSelect;
            EventManager.OnHotspotDeselect += OnDeselect;
        }
    
        private void OnDisable ()
        {
            EventManager.OnHotspotSelect -= OnSelect;
            EventManager.OnHotspotDeselect -= OnDeselect;
        }
    
        private void OnSelect (Hotspot hotspot)
        {
            if (hotspot == GetComponent <Hotspot>())
            {
                CursorIcon icon = KickStarter.cursorManager.GetCursorIconFromID (iconID);
                icon.label = newIconLabel;
            }
        }
    
        private void OnDeselect(Hotspot hotspot)
        {
            if (hotspot == GetComponent <Hotspot>())
            {
                CursorIcon icon = KickStarter.cursorManager.GetCursorIconFromID (iconID);
                icon.label = originalLabel;
            }
        }
    
    }
    
  • Thanks Chris, that did the job beautifully.

    The reason I need different names is so I can select each of them when needed (instead of choosing from a list where everything is named "walk to"). The script will also help me make a few hotspot dependant jokes that I was initially renouncing on because I wasn't going to create a cursor for a one-Time thing.

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.