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
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.
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);
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.
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.