Forum rules - please read before posting.

Converting an InvVar to a Bool - GetProperty custom Action

edited September 2017 in Technical Q&A
Hey!

So I've managed to get myself into a pickle with the InvVars. I'm trying to convert an InvVar bool to an global variable bool and it's not letting me do that.

For a little bit of background, Basically I'm planning on having most of my inventory solutions being handled by the unhandled inventory scripting function and getting the script to check certain properties to see if the object can be used for that purpose - Rather then writing a million and one handled inventory scripts.

My item was to set up an action that checks one particular property on the selected item and then Set it onto a global variable that I can then use the standard variable action on. While I would have liked to be able to script a flexible custom action that basically checks all the different types of variable types... my scripting isn't nearly that good, but getting it to set onto one variable that I can always check straight after setting the variable seemed a decent idea to me. Anyway, I'll show my workings and hopefully you can school me on how to go about fixing this hot mess.

        // Declare variables here
        public bool CurrentInvPropVal;
        private InvItem myItem;
            
        public ActionInvPropertyLong()
{
this.isDisplayed = true;
category = ActionCategory.Custom;
title = "Long?";
description = "Sets current inventory long property as CurrentInvPropVal.";
}
override public float Run ()
        {
            AC.InvItem myItem = AC.KickStarter.runtimeInventory.SelectedItem; 
            CurrentInvPropVal = myItem.GetProperty(3);
         }

Hopefully you can see where I've gone wrong... 

Thank you for all your help!

Comments

  • To transfer CurrentInvPropVal to a Global Variable, use:

    AC.GlobalVariables.SetBooleanValue (2, CurrentInvPropVal);

    (Where "2" is the ID number of the variable you wish to update, as listed in the Variables Manager).

    For more on setting/getting variables through script, see the front page of the Scripting Guide.
  • Hi Chris, Thanks - That should help me once I've worked out the main issue... Converting the InvVar to a Bool.

    I'm getting a "Cannot implicity convert type 'AC.InvVar' to bool" error. Considering InvVar's cover all variables is there something else I need to do to get it to recognize that the InvVar in question is set as bool? There's nothing on the front page that specifically talks about the InvVars.
  • Oh, sorry - misread that part.

    The InvVar class is a subclass of GVar, which is the base class for all variables (global and local). The front page doesn't describe that, but you can search for InvVar at the top.

    It's value is stored in it's val integer property.  If the type is boolean this value is '1' if True, and '0' if False, so the correct code would be:

    CurrentInvPropVal = (myItem.GetProperty(3).val == 1);
  • And now I'm getting another error - Not all code paths return a value. Considering that above line would possibly only work if the value was true, I tried forming it into a if statement instead as:

    if (myItem.GetProperty(3).val == 1)
                {
                    CurrentInvPropVal = true;
                }
                if (myItem.GetProperty(3).val == 0)
                {
                    CurrentInvPropVal = false;
                }

    But I'm still getting the same error. Is it perhaps trying to ask what it needs to do if the property isn't a bool?
  • No, your code and mine are equivalent.

    The Run function returns a float.  If the Action runs instantly, you need to end it with:

    return 0f;


    See the provided ActionTemplate file, which has comments that explain how each function should be used when creating new Actions.
  • Thanks again! The action finally implemented...

    However when it runs it breaks the game. The console comes back with "Object reference not set to an instance of an object" - Sorry that you've had to hold my hand through this.

    It seems to be tripping up the moment myItem.GetPropety is getting involved in the script.

    Here's the log of what in Unity's log under that error:

    NullReferenceException: Object reference not set to an instance of an object
    AC.ActionLong.Run () (at Assets/LB1.1/Scripts/Actions/ActionLong.cs:56)
    AC.ActionList+<RunAction>c__Iterator2.MoveNext () (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:342)
    UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
    UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
    AC.ActionList:ProcessAction(Int32) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:306)
    AC.ActionList:ProcessActionEnd(ActionEnd, Int32, Boolean) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:507)
    AC.ActionList:EndAction(Action) (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:420)
    AC.<RunAction>c__Iterator2:MoveNext() (at Assets/AdventureCreator/Scripts/ActionList/ActionList.cs:389)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
  • And what is on line 56?  Presumably:

    AC.InvItem myItem = AC.KickStarter.runtimeInventory.SelectedItem;

    If so, you'll get a NullReferenceException error if the Action runs when no item is currently selected.  A null check can evade the error afterwards:

    if (myItem == null) return 0f;

    Bear in mind that if the Action blocks gameplay, it may be that the inventory item is being automatically deselected before the Action itself is run.  You can get around that by setting the ActionList's When running field to Run In Background.
  • Actually it was tripping on 
    CurrentInvPropVal = (myItem.GetProperty(3).val == 1); 

    But I suppose if myItem is null, then it's not going to be able to get anything out of myItem.GetProperty to work.

    I've already got the script running in the background because our narration UI runs in the background. I'm wondering if it's dropping the inventory even earlier - I noticed that clicking anywhere, even not on a hot spot seems to drop the cursor icon. 

    AHHHHH. I WORKED IT OUT. Left-click deselects active item was set in the settings. Now it works! 

    *FIREWORKS!* Thanks so much for all your help, Chris, Little did you know if, but this was the best birthday present ever. ;p
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.