Variables Methods

v4.0

Each GameItemObject has a List<ItemObjectVariable> which contains all the variables, and the current values.

// GameItemObject.cs
public List<ItemObjectVariable> variables

// Returns the ItemObjectVariable with the variableName provided
public ItemObjectVariable Variable(string variableName)

// Note: The Variable() method will return a value that is cached in a Dictionary
// that is populated at runtime. This is more optimized for values that are called
// often. However, depending on your project, you may wish to cache the
// ItemObjectVariable in another location where it is often accessed, perhaps in
// another class.

When the Value changes, if variableAttributes are in use, then the system will automatically check to see if the existing GameItemAttribute needs to be updated. If so, the system will remove all GameItemAttributes of the ObjectType from the list, and add the new one based on the current RangeValue.

SetStatsDirt() will then be called on the GameItemObject, triggering stats to recompute their values.

Methods

Get Value & RangeValue

var durability = gameItemObject.Variable("Durability");

// Get the current value
var durabilityValue = durability.value;

// Get the current RangeValue
var durabilityRangeValue = durability.RangeValue;

Example

You can use the value of durability to modify the "price" of a GameItemObject.

// Get the price, perhaps it is stored using the Dictionaries module
var price = sword.dictionaries.Value<int>("Price");
var valuePrice = price * durability.RangeValue;

if (valuePrice > 0)
    Merchant.Speak($"I'll give you {valuePrice} for that {sword.FullName()}. Fair price.");
else
    Merchant.Speak($"That hunk of junk isn't worth a single ounce of my sweat!");

Add Value

Use the AddValue() method to add value to the Variable. Add a negative value to subtract. The final Value will be clamped between Min and Max, and RangeValue will be updated.

Returns the RangeValue of the Variable.

// Decrease Durability
var durability = gameItemObject.Variable("Durability");
var durabilityRangeValue = durability.AddValue(-1f); // Removes 1 from the value

Set Value

Use the SetValue() method to set the value to a specific amount. Add a negative value to subtract. The final Value will be clamped between Min and Max, and RangeValue will be updated.

Returns the RangeValue of the Variable.

// Set to a specific value
var durability = gameItemObject.Variable("Durability");
var durabilityRangeValue = durability.SetValue(0.8f);

// Set to Min or Max values (Returns the RangeValue)
durability.SetToMax();
durability.SetToMin();

You can change the values of Min and Max at runtime! While they will always start at the values set in the ItemObject scriptable object, you can modify them later for even more game mechanic opportunities.

Set to a specific RangeValue

RangeValue is always between 0f and 1f, between the Min and Max values and modified by the curve, if in use. Rather than setting the specific Value, you can opt to set a RangeValue, and the Value will be computed based on that.

Returns the Value of the Variable.

// Set to 0.5, and get the new Value
var durability = gameItemObject.Variable("Durability");
var newDurabilityValue = durability.SetToRangeValue(0.5);

Last updated