- Unity Game Development Scripting
- Kyle D'Aoust
- 632字
- 2021-08-05 16:52:52
The self item class
The first item class we'll create is for an item that affects the player upon usage. Items that players use typically affects their various stats either by adding or removing them or buffing/debuffing them for a certain amount of time. Now let's start scripting; create a new script and name it itemSelf
.
Adding our variables
Our first set of variables will actually be added outside of our class as they are enum variables:
public enum SelfAction {BuffDebuff, ChangeHP, ChangeArmor, None}; public enum SelfType {Armor, Potion, None};
The first enum we created will be used to pick what the item does. We've got a few options for our items, but this can be expanded and customized to your liking. The second enum we use will determine of what type the item is; for now, we're just checking to see whether it's a potion or armor. Now let's add the rest of our variables:
public GameObject Player; public int Amount, Value, ArmorAmount; public float Weight; public string Name, Stat; public SelfAction selfAction = SelfAction.None; public SelfType selfType = SelfType.None;
We add a GameObject so that we have a player reference to adjust stats. The rest of the variables we added are for the item stats. Finally, we add our two enums to our list of variables. We make these variables public so that anyone can just drag-and-drop the scripts for easy item creation.
Buff or debuff stats
The first function we'll add to our item script will allow us to add or subtract player stats. Add the following code to your script:
void BuffDebuffStat() { Player.SendMessage("BuffDebuffStat", new KeyValuePair<string, int>(Stat, Amount)); }
When we call this function, we send a message to our player, which will call a function in a script on the player that will add or subtract the stat we specify. In this message, we tell this function which function to call as well as send a KeyValuePair
variable. We use a KeyValuePair
variable to send both the stat we want to modify as well as the amount that we want to modify it by.
The health changer
Our next function to be added will be one that will affect the player's health. Add the following code to your script:
void ChangeHealth() { Player.SendMessage("ChangeHealth", Amount); }
When we call ChangeHealth
, we send a message to the player to call a function known as ChangeHealth
, and we send Amount
as well. As you can see, we use Amount
often. Since changing stats is all about amounts, we use a single variable to make it easier for us.
The armor changer
The next and final stat modifying function we'll add will allow us to adjust the armor of our player. Add this function to your script:
void ChangeArmorAmount() { Player.SendMessage("ChangeArmorAmount", ArmorAmount); }
This function is similar to the ChangeHealth
function. We send the player a message to call a function that will change the player's armor amount. Then, we also send it the amount we want to change it by.
The item activator
This last function that we add will be called by other classes to activate the item. Add this last function to your script:
void Activate() { switch(selfAction) { case SelfAction.BuffDebuff: BuffDebuffStat(); break; case SelfAction.ChangeHP: ChangeHealth(); break; case SelfAction.ChangeArmor: ChangeArmorAmount(); break; } if(selfType == SelfType.Potion) Destroy(gameObject); }
When this function is called, we use a switch
statement to check the selfAction
variable. This is an easy way to see what the item should do when the player uses it. At the end of the Activate
function, we check to see what type of item it is. If it is a potion, we destroy the GameObject. Not all items get destroyed upon use, such as armor, so we use the selfType
variable to determine what type of item it is.
- Mastering Ext JS(Second Edition)
- LabVIEW程序設計基礎與應用
- Dynamics 365 Application Development
- Java高手真經(高級編程卷):Java Web高級開發技術
- 算法基礎:打開程序設計之門
- Visual Basic程序設計教程
- 從程序員到架構師:大數據量、緩存、高并發、微服務、多團隊協同等核心場景實戰
- The React Workshop
- Hands-On JavaScript High Performance
- Scratch真好玩:教小孩學編程
- Mastering macOS Programming
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- Mastering Unity 2D Game Development(Second Edition)
- MINECRAFT編程:使用Python語言玩轉我的世界
- C++ System Programming Cookbook