[내일 배움 캠프 Unity 4기] W3D5 05.03 TIL

김용준·2024년 5월 3일
0

내일배움캠프

목록 보기
8/47

Goals

  • Team project
    - Attach Quest class to the Player
    - Add potion in the Item class

Quest class on Player

To enhance the pleasure, the quest system is considered to implement on the team project. Before to access the script, we planned the connection of to the other class. Firstly, the we discussed that the player accept the quest on the quest board. And, the limit of maximum number of acceptable quest is dicided to be 3. Also the progress could be seen on the quest board. Then, we made the class and method to include all the features.

public class Quest
{
  int fID;
  int fType;
  int fTargetID;
  int fGoalNum;
  int fCurrNum;
  bool fIsCompleted;
  (Item, int, int) fRewards; // reward Item, Gold, Exp
  
  public Quest()
  {}
  
  public Quest(string[] quest_dat)
  {
    ... // assign the string data to private variables
  }
  
  public string GetStatus(int ID)
  {
    ... 
    return $"Current: {fCurrNum}, Goal: {fGoalNum}";
  }
  
  public bool IsAcceptable(int ID)
  {
    Player player = Player.instance;
    ... // compare the condition to accep the quest
        // also check the number of accepted quest and compared with 3
  }
  
  public bool Complete()
  {
   ... // when the player satisfy the goal condition, return true
  }
}

Potion in Item

Adding the Use type on the enum ItemType, I thought how to minimize the effort to implement potion. Then, I modified some method for the potion. Since the main actor of drink potion is the player, the use method is written on the player class.

public class Item
{
  ... // private variables and Constructor
}

public class Player
{
  public void UsePotion()
  {
    ... // Find the index of potion on the inventory
    if(nPotion > 0)
    {
      nPotion--;
      fHP += Player.Inventory.GetItem(idx).GetBHP();
    }
  }
}

Q&A

Q. When I use the foreach loop rather than for, the element can not be modified, but the value under the class can be modified. What is the difference and How do I solve the error?

A. Before you write the script, you have to recognize the authority of elements. The element offoreach sets to be 'ReadOnly' and the for is able to 'Read and Write'.
The difference between value and class is the address of element. Even the foreach do not allow the modification of element which the loop takes, including the address of value. When you access the class, However, the address would not be changed. Since the value under the address is changed, this method is possible

profile
꿈이큰개발자지망생

0개의 댓글