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

February 12, 2020

Beginner Programming

Creating a Character Stat System

Accessing Variables in Our System Part 1


Beginner Programming: Unity Game Dev Courses

Unity Learn Course – Beginner Programming

Accessing Variables in Our System Part 1

Equipping Weapons

They mostly just created the scriptable object, ItemPickUp_SO, for now so that the base of the EquipWeapon method would work. This method simply adds a designated amount to the character’s base stats.

Equipping Armor

Here they created two separate enums within ItemPickUp_SO to define options for the types of items and options for the types of armor. These enums on the scriptable objects were actually very nice in the editor as they provided drop down lists easily accessible to the designer.

The EquipArmor method in CharacterStats_SO used a switch statement, which goes well with enums. They however performed the same calculations with every single case (adding the different values to the player’s resistances), so I think it would be cleaner to just perform this calculation directly after the switch statement.

Un-Equipping Weapons

Using bool return type methods is useful for equipment systems since you normally want two different events to occur depending on if the player can and successfully equips the item, or if they are unable to equip the item. In their case, they just check if a weapon pickup is the same as the current weapon with the bool field within the bool method UnequipWeapon method. It also checks if there was previously a weapon, and it will destroy the current weapon, set it to null, and return the currentDamage to baseDamage.

Un-Equipping Armor

The UnequipArmor method followed the same idea as the UnequipWeapon method, where it had a return type of bool and returned a value based on whether the item passed in (the item being unequipped in this case) matched the item currently equipped to the player. Since this dealt with armor, they used a switch case again to account for all the various armor types so they could direct the actions at a specific armor slot on the character. This check was similar to what was done in UnequipWeapon again, as it checked if the current slot matched the item being unequipped and then adjusted the current stats (resistance in this case) to the base value and set that item slot to null.

This was repeated for every armor type in the switch case, with the slight difference that they had to change the item slot (specific ItemPickUp field on this particular CharacterStats_SO object). I found it easier to just create a helper method that took in an extra ItemPickUp parameter to determine which ItemPickUp slot was the one being tested and have that method do all the work for that specific slot. This way I only needed that block of code once in the script and only needed a single line for each case in the switch statement.

This ensures everything is uniform (which it currently is according to the tutorial) and keeps the code more organized and cleaner. This may have to be edited if different events are needed for different armor types in the future on unequip, but it’s much better currently.

SUMMARY

  • Enums are very nice on the editor side for scriptable objects since they provide an organized list of options to work with
  • Using bool return type methods can be useful with equip systems
  • Identify repeated code in switch statements to clean/organize code