Forum rules - please read before posting.

Override "Use" Syntax

edited September 2018 in Technical Q&A
Hi
Is there a way to override syntax based on a hotspot rather than an inventory object?

example:
I have a "slice of bread" inventory item.
I have a "plate" inventory item.
I have a "toaster" hotspot.

Syntax should be:
Use slice of bread in toaster
Use slice of bread on plate

Kinda feels like  you could have "override syntax" tickbox on the "Use" action within the Hotspot component.

I've attached a snapshot of a recommended location for it.

Thanks
Kev
https://drive.google.com/open?id=1GloRQEDb29t-SN7cYLNznZpYi5RgycB0
«1

Comments

  • edited September 2018
    [deleted]
  • edited September 2018
    I appreciate the need, but having having an override syntax per-Hotspot makes the UI overly-complex in my opinion.  This can, however, be done by hooking into the OnHotspotSelect custom event and updating the syntax depending on the Hotspot, i.e.:

    http://pasteall.org/1219037/csharp
  • That's an elegant solution thank you. I may tweak it to allow specifying the text to use as the replacement.
  • I tried to do this myself without pestering anybody again but after nearly three hours i think I need to ask for assistance..

    The code below works brilliantly, except for an error on Line 17 about accessing a null object.


    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using AC;

    public class OverrideItemPrefix : MonoBehaviour
    {

        public string hotspotToOverride;
        public string newSyntax;
        public string invItem;
        string selectedItem="";
        string hotspotName = "";

        private void Update()
        {
            if (KickStarter.runtimeInventory.SelectedItem.GetFullLabel()==null)
            {
                selectedItem = "";
            } else { 
                selectedItem = KickStarter.runtimeInventory.SelectedItem.GetFullLabel();
            }
        }

        private void OnEnable()
        {
            EventManager.OnHotspotSelect += OnHotspotSelect;
        }


        private void OnDisable()
        {
            EventManager.OnHotspotSelect -= OnHotspotSelect;
        }

        

        private void OnHotspotSelect(Hotspot hotspot)
        {
            hotspotName = hotspot.name;
            if ((hotspotName == hotspotToOverride)&&(selectedItem == invItem))
            {
                KickStarter.cursorManager.hotspotPrefix2.label = newSyntax;
            }
            else
            {
                KickStarter.cursorManager.hotspotPrefix2.label = "on";
            }
        }

    }

    Also, I spent a while going through documentation and trying to understand the scripts, but I just can't figure out the last two things that would make this perfect.

    1) set the hotspot to (this), the hotspot that the script is attached to.
    2) a dropdown list of all active inventory items in place of "public string invItem;"


    If anybody wants to make use of this script, be sure to untick "Override use syntax" from the Inventory Manager menu,  as that overrides this script.

    thanks again
    Kev


  • Though I'm not totally sure on your intentions with the script, the error is occuring because you're checking for data within the "SelectedItem" variable, which is null when the player doesn't have an item selected.  You need to wrap a null check around it, i.e.:

    if (KickStarter.runtimeInventory.SelectedItem != null) { // Do stuff }

    To set an internal Hotspot variable to the one it's attached to, use GetComponent in your Awake method.  See, for example, the way _camera is assigned in the BackgroundCamera script.

    I don't know what you mean by a list of all active inventory items, since only one can be selected at a time.
  • That's great info thank you Chris. I'll have a look and cobble something together. Regards the inventory, in the "use inventory actions" UI, you can click a drop down list to choose an item from all available inventory items to specify which item the use action references. All I want is to have the same list available in my script so rather than typing the name of the item, I just choose it from a drop-down.
  • image
    I've tidied up the script a little and incorporated your suggestions - it works perfectly thank you :)

    If I could change the "Inventory Item" to a drop down... that would be the cherry on top ;)

    Kev
  • You can grab and adapt the code that generates that drop down from HotspotEditor.cs, lines 337-381
  • Awesome thanks :)
  • Ok... so showing my lack of C# here... :(  I really wanted to do this on my own, but after having spent HOURS hacking around, trying various code snippets etc - I just cant get the inventory dropdown to work :(

    I have attached the entire script here: https://codepad.co/snippet/yXKKIfVy

    I'm hoping you take pity on me and can fix the couple of lines of code needed to get this working.... Or in fact anybody else on the forum?

    Fingers crossed...

    KevRev

  • edited September 2018
    Ok.. ignore all the above, i've changed my tactic and ALMOST have it exactly as I need it!


    I know its core files that'll be overwritten when I update AC, but thats my lookout (unless you like it enough to integrate it... ;)  )

    1 line added to "Button.cs" to add the "syntax" element to a button:
            
            public string syntax; 

    1 line added to "HotspotEditor.cs" to allow you to type the new syntax:

             invButton.syntax = EditorGUILayout.TextField("Override 'Use' syntax:", invButton.syntax);

    Then (because I didnt know how to integrate it) i've still had to add the following script as a component:

    https://codepad.co/snippet/S2jUbXFq

    So i think all I need is a little tweak in the above code to reference (InvButton.syntax) and to apply it. 

    I really think this could be an excellent addon to Adventure Creator.

    Use Can of Paint to spray on wall
    Use water to fill watering can
    Use scissors to cut paper

    It opens up a whole lot of context sensitive commands that you could add to a game. :)

    Any help greatly appreciated. 

    Ta
    Kev




  • There's no need to modify AC's scripts to allow for it - it can all be done within the same custom script to allow for a per-Hotspot, per-item option.

    You don't need to read the hotspot's own list of items, just the item that the player currently has selected and compare it with an array of IDs, each with their own prefix:


  • edited September 2018
    Hi everybody.
    I finally have this working EXACTLY as I wanted. :)

    DESCRIPTION:
    You can now override the "Use" syntax on a per inventory/hotspot interaction basis.
    For instance, if you want to use a slice of bread with a toaster OR a plate, you'll want to:

    Use slice of bread on plate
    Use slice of bread in toaster

    You may even want to:

    Use sponge to mop up puddle


    VIDEO DEMO:
    https://youtu.be/CbxMvPmTyBk 


    ADD TO YOUR OWN PROJECT:
    There are three script files:

    ItemOverride.cs
    https://codepad.co/snippet/oEfT8Wtw 

    InvManager.cs
    https://codepad.co/snippet/gyag7smQ 

    And in an "EDITOR" folder beneath the main script, InvManagerEditor.cs:
    https://codepad.co/snippet/Dk9WIyyx


    Massive thanks to @ChrisIceBox ;for his excellent support and advice.
    I've spent HOURS and HOURS working on this (being a N00b and all). I've learnt basic C# and a LOT about Unity and AC, which will help with my projects.

  • edited January 2019

    Hi everybody. First of all thanks @KevRev for sharing the code.
    Now, could InvManager be modified to override the "Use" part of the sentence as well?

    Drop slice of bread in toaster.

    If it's possible to do it OnSpotSelect, it might be a good way to add custom syntax per hotspot.

    I know @Selzier has a tutorial that covers getting the text to say "Equip" instead of "Use" on specific occasions, but I don't know how to combine both.

    Thanks!

  • Now, could InvManager be modified to override the "Use" part of the sentence as well?

    The Inventory Manager already allows for this without modification. Checking Override 'Use' syntax? in an item's list of properties allows you to change both the 'Use' and 'on' words in the sentence.

  • Thanks @ChrisIceBox !

    I am using that already, but I find myself wanting to define different Verbs for different hotspots with the same item.

    I would also love to be able to change the verb that appears when Using, Looking at or Talking to a hotspot (without an item).
    I am aware I can create another cursor, but since I am using Click on Hotspot then Interaction with a custom Unity UI, I am having trouble linking interaction buttons to 'new' cursors.

  • I am using that already, but I find myself wanting to define different Verbs for different hotspots with the same item.

    I see. @KevRev's scripts only seem to override the "on" word, but I'd imagine it could be adapted to incorporate "Use" as well with some tweaks. You may like to PM him directly.

    I would also love to be able to change the verb that appears when Using, Looking at or Talking to a hotspot (without an item). I am aware I can create another cursor, but since I am using Click on Hotspot then Interaction with a custom Unity UI, I am having trouble linking interaction buttons to 'new' cursors.

    You can rename "Use", "Look at", etc - but creating new cursors is definitely the way to go.

    To link a cursor to a menu, you need to create an Interaction element and select the correct "Cursor" field from the drop-down. Since custom Unity UI canvases do add another layer of complexity, you can temporarily switch your Menu's Source to Adventure Creator and getting it working in that mode before incorporating Unity UI.

  • I adapted @KevRev's script to include the verb too. You only need very minimal C# knowledge. I don't have access to my project right now, but all you need to do is add an extra parameter to ItemOverride, and then do everything he did for hotspotPrefix2 but for hotspotPrefix1 this time (and then add a field to the editor script to account for that).

    @ChrisIceBox how would I go about scanning these fields when I click "Gather text"? They would need to be localised.

  • Have your script implement the ITranslatable interface - see the Manual's "Custom translatables" chapter for more.

  • Thank you! I'll check it out.

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.