Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Headache

Discussion in 'Scripting' started by EatUrDemon, Jun 9, 2014.

  1. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Code (csharp):
    1.  
    2. using System.Collections.Generic;
    3.  
    4. public class ModifiedStat : BaseStat {
    5.    
    6.     private List<ModifyingAttribute> _mods;    // A list of Attributes that modify the stat
    7.     private int _modValue;    // The amount added to the baseValue from the modifiers
    8.    
    9.     public ModifiedStat()
    10.     {
    11.         _mods = new List<ModifyingAttribute>();
    12.         _modValue = 0;
    13.     }
    14.    
    15.     public void AddModifier(ModifyingAttribute mod)
    16.     {
    17.         _mods.Add(mod);
    18.     }
    19.    
    20.     private void CalculateModValue()
    21.     {
    22.         _modValue = 0;
    23.        
    24.         if(_mods.Count > 0)
    25.             foreach(ModifyingAttribute att in _mods)
    26.                 _modValue += (int)(att.attribute.AdjustedValue * att.ratio);
    27.     }
    28. }
    29.  
    30. public struct ModifyingAttribute
    31. {
    32.     public Attribute attribute;
    33.     public float ratio;
    34. }
    35.  
    On line 25, I'm getting an error that says "Operator '*' cannot be applied to operands of type 'method group' and 'float'. What's going on here?! It's been typecasted and everything...
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    What is in the Attribute class?

    I'm guessing that it should be att.attribute.AdjustedValue ()
     
  3. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Code (csharp):
    1.  
    2. public class Attribute : BaseStat {
    3.    
    4.     public Attribute()
    5.     {
    6.         ExpToLevel = 50;
    7.         LevelModifier = 1.05f;
    8.     }
    9. }
    10.  
    11. public enum AttributeName
    12. {
    13.     Strength,
    14.     Constitution,
    15.     Dexterity,
    16.     Intelligence,
    17.     Wisdom,
    18.     Charisma
    19. }
    20.  
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Alright, now what is in BaseState?

    I'm looking for what AdjustedValue is.
     
  5. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Code (csharp):
    1.  
    2. public class BaseStat {
    3.     private int _baseValue;    // The base value of the stat
    4.     private int _buffValue;    // The amount of the buff to the stat
    5.     private int _expToLevel;    // The total amount of exp needed to raise the skill
    6.     private float _levelModifier;    // The modifier applied to the exp needed to raise the skill
    7.    
    8.     public BaseStat()
    9.     {
    10.         _baseValue = 0;
    11.         _buffValue = 0;
    12.         _levelModifier = 1.1f;
    13.         _expToLevel = 100;
    14.     }
    15.    
    16. #region Basic Setters and Getters
    17.     // Basic Setters and Getters
    18.     public int BaseValue
    19.     {
    20.         get{ return _baseValue;}
    21.         set{ _baseValue = value;}
    22.     }
    23.    
    24.     public int BuffValue
    25.     {
    26.         get{ return _buffValue;}
    27.         set{ _buffValue = value;}
    28.     }
    29.    
    30.     public int ExpToLevel
    31.     {
    32.         get{ return _expToLevel;}
    33.         set{ _expToLevel = value;}
    34.     }
    35.    
    36.     public float LevelModifier
    37.     {
    38.         get{ return _levelModifier;}
    39.         set{ _levelModifier = value;}
    40.     }
    41. #endregion
    42.    
    43.     public int CalculateExpToLevel()
    44.     {
    45.         return (int)(_expToLevel * _levelModifier);
    46.     }
    47.    
    48.     public void LevelUp()
    49.     {
    50.         _expToLevel = CalculateExpToLevel();
    51.         _baseValue++;
    52.     }
    53.    
    54.     public int AdjustedValue()
    55.     {
    56.         return _baseValue + _buffValue;
    57.     }
    58. }
    59.  
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I was right.

     
  7. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    I never would've guessed that. o_O

    Thanks! Knowing me, I'm probably going to have a few more problems throughout the day. *sigh*
     
  8. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    You do know why you needed to add () ?
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Either he doesn't know, or someone else is going to come along and search for this thread someday and need the answer.

    Basically, this:
    Code (csharp):
    1.  
    2. float foo = SomeFunctionThatReturnsSix() * 5f;
    3. //foo is now 30
    4.  
    Tells the compiler "run SomeFunctionThatReturnsSix, then multiply its result by 5".

    While this (the equivalent of the problem code first posted):
    Code (csharp):
    1.  
    2. float foo = SomeFunctionThatReturnsSix * 5f;
    3.  
    tells the compiler "Multiply the function SomeFunctionThatReturnsSix by 5". You can't multiply a block of code by a number.

    There are situations where you DO want to do stuff with the function itself, rather than its result. Callbacks are the most common scenario. Here's a good one: http://docs.unity3d.com/ScriptReference/Application.RegisterLogCallback.html.
     
  10. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Makes sense.
     
  11. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    I'm done coding for today. Mainly because I've been going at it all day. But before I go, I've got one last issue that needs to be straightened out.

    BaseCharacter
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;    // Added to access the enum class
    5.  
    6. public class BaseCharacter : MonoBehaviour {
    7.     private string _name;
    8.     private int _level;
    9.     private uint _freeExp;
    10.    
    11.     private Attribute[] _primaryAttribute;
    12.     private Vital[] _vital;
    13.     private Skill[] _skill;
    14.        
    15.     public void Awake()
    16.     {
    17.         _name = string.Empty;
    18.         _level = 0;
    19.         _freeExp = 0;
    20.        
    21.         _primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
    22.         _vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
    23.         _skill = new Skill[Enum.GetValues(typeof(SkillName)).Length];
    24.        
    25.         SetupPrimaryAttributes();
    26.         SetupVitals();
    27.         SetupSkills();
    28.     }
    29.        
    30.     public string Name
    31.     {
    32.         get{ return _name; }
    33.         set{ _name = value; }
    34.     }
    35.    
    36.     public int Level
    37.     {
    38.         get{ return _level; }
    39.         set{ _level = value; }
    40.     }
    41.    
    42.     public uint FreeExp
    43.     {
    44.         get{ return _freeExp; }
    45.         set{ _freeExp = value; }
    46.     }
    47.    
    48.     public void AddExp(uint exp)
    49.     {
    50.         _freeExp += exp;
    51.        
    52.         CalculateLevel();
    53.     }
    54.    
    55.     // Take the average of all of the player's skills and assign that as the player level.
    56.     public void CalculateLevel()
    57.     {
    58.        
    59.     }
    60.    
    61.     private  void SetupPrimaryAttributes()
    62.     {
    63.         for(int cnt = 0; cnt < _primaryAttribute.Length; cnt++)
    64.         {
    65.             _primaryAttribute[cnt] = new Attribute();
    66.         }
    67.     }
    68.    
    69.     private void SetupVitals()
    70.     {
    71.         for(int cnt = 0; cnt < _vital.Length; cnt++)
    72.         {
    73.             _vital[cnt] = new Attribute();
    74.         }
    75.     }
    76.    
    77.     private void SetupSkills()
    78.     {
    79.         for(int cnt = 0; cnt < _skill.Length; cnt++)
    80.         {
    81.             _skill[cnt] = new Attribute();
    82.         }
    83.     }
    84.    
    85.     public Attribute GetPrimaryAttribute(int index)
    86.     {
    87.         return _primaryAttribute[index];
    88.     }
    89.    
    90.     public Vital GetVital(int index)
    91.     {
    92.         return _vital[index];
    93.     }
    94.    
    95.     public Skill GetSkill(int index)
    96.     {
    97.         return _skill[index];
    98.     }
    99.    
    100.     private void SetupVitalModifiers()
    101.     {
    102.         // Health
    103.         GetVital ((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 0.5f));
    104.        
    105.         // Mana
    106.         GetVital ((int)VitalName.Mana).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Intelligence), 0.5f));
    107.     }
    108.    
    109.     private void SetupSkillModifiers()
    110.     {
    111.         // Melee Offense
    112.         GetSkill ((int)SkillName.Melee_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Strength), 0.33f));
    113.         GetSkill ((int)SkillName.Melee_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Dexterity), 0.33f));
    114.         // Melee Defense
    115.         GetSkill ((int)SkillName.Melee_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), 0.33f));
    116.         GetSkill ((int)SkillName.Melee_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), 0.33f));
    117.         // Ranged Offense
    118.         GetSkill ((int)SkillName.Ranged_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Dexterity), 0.33f));
    119.         GetSkill ((int)SkillName.Ranged_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), 0.33f));
    120.         // Ranged Defense
    121.         GetSkill ((int)SkillName.Ranged_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Strength), 0.33f));
    122.         GetSkill ((int)SkillName.Ranged_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Intelligence), 0.33f));
    123.         // Magick Offense
    124.         GetSkill ((int)SkillName.Magick_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), 0.33f));
    125.         GetSkill ((int)SkillName.Magick_Offense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Intelligence), 0.33f));
    126.         // Magick Defense
    127.         GetSkill ((int)SkillName.Magick_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Dexterity), 0.33f));
    128.         GetSkill ((int)SkillName.Magick_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Charisma), 0.33f));
    129.     }
    130.    
    131.     public void StatUpdate()
    132.     {
    133.         for(int cnt = 0; cnt < _vital.Length; cnt++)
    134.             _vital[cnt].Update();
    135.         for(int cnt = 0; cnt < _skill.Length; cnt++)
    136.             _skill[cnt].Update();
    137.     }
    138. }
    139.  
    Line 72: Cannot implicitly convert type 'Attribute' to 'Vital'
    Line 80: Cannot implicitly convert type 'Attribute' to 'Skill'

    ModifyingAttribute
    Code (csharp):
    1.  
    2. using System.Collections.Generic;
    3.  
    4. public class ModifiedStat : BaseStat {
    5.    
    6.     private List<ModifyingAttribute> _mods;    // A list of Attributes that modify the stat
    7.     private int _modValue;    // The amount added to the baseValue from the modifiers
    8.    
    9.     public ModifiedStat()
    10.     {
    11.         _mods = new List<ModifyingAttribute>();
    12.         _modValue = 0;
    13.     }
    14.    
    15.     public void AddModifier(ModifyingAttribute mod)
    16.     {
    17.         _mods.Add(mod);
    18.     }
    19.    
    20.     private void CalculateModValue()
    21.     {
    22.         _modValue = 0;
    23.        
    24.         if(_mods.Count > 0)
    25.             foreach(ModifyingAttribute att in _mods)
    26.                 _modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);
    27.     }
    28.    
    29.     public new int AdjustedBaseValue
    30.     {
    31.         get {return BaseValue + BuffValue + _modValue; }
    32.     }
    33.    
    34.     public void Update()
    35.     {
    36.         CalculateModValue();
    37.     }
    38. }
    39.  
    40. public struct ModifyingAttribute
    41. {
    42.     public Attribute attribute;
    43.     public float ratio;
    44.    
    45.     public ModifyingAttribute(Attribute att, float rat)
    46.     {
    47.         attribute = rat;
    48.         ratio = rat;
    49.     }
    50. }
    51.  
    Line 46: Cannot implicitly convert type 'float' to 'Attribute'
     
  12. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    You are trying to store something as something else. Like at the bottom you are trying to save a number as an object. What are you trying to do here? It should probably be att not rat on line 47 (46 in your editor)
     
  13. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Fixed the att/rat issue. But it didn't change anything.

    See, I'm following the BurgZerg Arcade Hack and Slash tutorial series. I'm not very good with C#, and I figure this is the best way to learn... I've already been able to make a few small custom C# scripts on my own.

    Anyway.

    I can't seem to figure out where these errors are coming from. I've gone over the code multiple times. Maybe it's just a case of being used to that particular block of code, like when a writer needs someone else to proofread. Whatever it is, I can't find the problems, no matter how long I search. ):
     
  14. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    Error 1: You are trying to save an Atribute in a variable defined as a Vital on line 72
    Error 2: You are trying to save an Atribute in a variable defined as a Skill on line 80
    Error 3: You are trying to save a float in a variable defined as an Atribute on line 46

    Either change the variables, or make a function to convert them.