UnityLearn – Beginner Programming – Creating a Character Stat System – Pt. 04 – Accessing Variables in Our System Part 2

March 5, 2020

Beginner Programming

Creating a Character Stat System

Accessing Variables in Our System Part 2


Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Accessing Variables in Our System Part 2

Writing Your Character Stats MonoBehaviour

They simply created the base CharacterStats class (non-scriptable object version). They gave it a constructor that created a reference to the CharacterInventory and had a Start method which initialized all the stat variables automatically if the “setManually” option was not checked.

Using Your Scriptable Objects Methods

They begin to fill the CharacterStats class they just created with many methods that simply call methods from the referenced CharacterStats_SO scriptable object.

Here they also use the UnEquipWeapon method, which has a return type of bool. They use it in the ChangeWeapon method, and have it in the if statement condition. This is interesting as the method here will run as the if statement checks the condition (regardless of the outcome being true or false), then based on the result of this method, decides if it will continue through the if statement block.

The method can be seen here:
public void ChangeWeapon(ItemPickUp weaponPickUp)
{
if(!characterDefinition.UnequipWeapon(weaponPickUp, charInv, characterWeaponSlot))
{
characterDefinition.EquipWeapon(weaponPickUp, charInv, characterWeaponSlot);
}
}

Here, characterDefinition.UnequipWeapon will be performed during the if statement condition check, regardless of the result. The result is determined through running that method, which will then determine if the enclosed if statement block is run or not.

Finally they cover their reporter methods, which are simply methods which when called report back information about a specific variable within CharacterStats_SO. For example, this can just read what the currentHealth of the CharacterStats_SO is.

SUMMARY

  • Scriptable objects can be created as a basis for other classes to use as long as those other classes have a reference to the scriptable object
  • Methods can be called and run from within if statement conditions
  • Reporter methods can be useful for passing read-only information