Search Unity

2 Classes for 1 Field?

Discussion in 'Scripting' started by Andreas12345, Jan 18, 2019.

  1. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    I get values from 2 Devices "only one" is active at the same time.
    Both Classes should have the field bpm:
    Code (CSharp):
    1.  public HeartRateDisplay + FitnessEquipmentDisplay bpm;
    is there a way to do this in c#?
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Seems like pretty basic inheritance with accessors to me.
    Code (csharp):
    1.  
    2. public abstract class Device
    3. {
    4.     public abstract float bpm { get; }
    5. }
    6.  
    7. public class HeartRateDisplay : Device
    8. {
    9.     public override float bpm
    10.     {
    11.         get
    12.         {
    13.             return 2.5f;
    14.         }
    15.     }
    16. }
    17.  
    18. public class FitnessEquipmentDisplay : Device
    19. {
    20.     public override float bpm
    21.     {
    22.         get
    23.         {
    24.             return 3.5f;
    25.         }
    26.     }
    27. }
    28.  
    29. Device device = new HeartRateDisplay();
    30. Debug.Log(device.bpm);
    31. device = new FitnessEquipmentDisplay();
    32. Debug.Log(device.bpm);
    33.  
    Should output:
    Code (csharp):
    1.  
    2. 2.5
    3. 3.5
    4.  
     
    Last edited: Jan 21, 2019
    Andreas12345 likes this.
  3. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    2 times device = new?
    I try this out, but i think i need to work on this, thanks for the tip:
    My Script so far is:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5.  
    6. public class DemoUIDisplay : MonoBehaviour
    7. {
    8.   //  public ProfileSessionManager profileSessionManager;
    9.     public BicyclePowerSim bicycleSim;
    10.     public PowerMeterDisplay powerMeter;
    11.  
    12.     public HeartRateDisplay bpm;
    13.    // public FitnessEquipmentDisplay bpm2;  we need bpm for it :)
    14.  
    15.    // public FitnessEquipmentDisplay bpm;
    16.     //
    17.     public SpeedDisplay speed2;
    18.     public CadenceDisplay cadence2;
    19.     public FitnessEquipmentDisplay fed;
    20.     public RoundCounter roundCounter;
    21.  
    22.     public GameObject MiniMap;
    23.     public GameObject SaveExit;
    24.     public CircularBlur radialBlur;
    25.     public Text envText;
    26.     public Text riderText;
    27.     public Text speedText;
    28.    
    29.  
    30.  
    31.     public Text stridesText;
    32.     // public Text StrideCadenceText;
    33.     // public Text StrideDistanceText;
    34.     // public Text StrideSpeedText;
    35.  
    36.     public float minutes;  //= Mathf.Floor(timer / 60);
    37.     public float seconds;  //= timer % 60;
    38.     public float timer;
    39.     private bool gamePaused = true;
    40.     public float power;
    41.     public int rounds;
    42.  
    43.     public float distance;
    44.     public float ftp;
    45.     public float bpms; //Heartrate
    46.    // public Text km1Text;
    47.     public float sec = 3f;
    48.     public float climb;
    49.      
    50.  
    51.  
    52.     void Update()
    53.     {
    54.  
    55.  
    56.         //cumulatedDistance += ((transform.position - previous position).magnitude);
    57.         float totalF = Mathf.Abs(bicycleSim.Fgravity) + Mathf.Abs(bicycleSim.FrollingResitance) + Mathf.Abs(bicycleSim.FaeroDrag);
    58.         float fpercent = 0;
    59.         float rpercent = 0;
    60.         float apercent = 0;
    61.  
    62.         if (totalF != 0)
    63.         {
    64.             fpercent = Mathf.Abs(bicycleSim.Fgravity) / totalF * 100;
    65.             rpercent = Mathf.Abs(bicycleSim.FrollingResitance) / totalF * 100;
    66.             apercent = Mathf.Abs(bicycleSim.FaeroDrag) / totalF * 100;
    67.  
    68.         }
    69.         //  distance = powerMeter.distanceTravelled;  //3.3f
    70.         distance = bicycleSim.distanceTravelled / 1000;
    71.  
    72.              
    73.         ftp = powerMeter.instantaneousPower / bicycleSim.weight;
    74.       //    ftp = powerMeter.instantaneousPower / ProfileSessionManager.curPlayerWeight;
    75.  
    76.         bpms = bpm.heartRate;
    77.         rounds = GameObject.Find("Bicycle/target").GetComponent<RoundCounter>().count;
    78.         //rounds = roundCounter.count + 1;
    79.         climb = bicycleSim.totalClimb;
    80.  
    81.         //deactivate MiniMap in UI
    82.         if (Input.GetKeyUp(KeyCode.M))
    83.         {
    84.             MiniMap.SetActive(!MiniMap.activeSelf);
    85.  
    86.         }
    87.  
    88.  
    89.         if (distance >= 1000)
    90.         {
    91.      //       km1Text.text = "Reached 1000Meter!";
    92.         }
    93.  
    94.         if (bicycleSim.speed <= 10)
    95.         {
    96.             SaveExit.SetActive(true);
    97.         }
    98.         else
    99.         {
    100.             SaveExit.SetActive(false);
    101.         }
    102.  
    103.  
    104.         float minSpeed = 40;
    105.         float maxSpeed = 100;
    106.  
    107.         if (bicycleSim.speed >= minSpeed)
    108.         {
    109.             radialBlur.strength = Mathf.InverseLerp(minSpeed, maxSpeed, bicycleSim.speed);
    110.             radialBlur.strength = .1f + .9f * radialBlur.strength;
    111.         }
    112.        
    113.         if (bicycleSim.speed >= minSpeed)
    114.         {
    115.             radialBlur.strength = 0.5f;
    116.         }
    117.         else
    118.         {
    119.             radialBlur.strength = 0f;
    120.         }
    121.        
    122.  
    123.  
    124.         if (gamePaused)
    125.         {
    126.             power = powerMeter.instantaneousPower;
    127.             if (power >= 20)
    128.             {
    129.                 timer += Time.deltaTime;
    130.             }
    131.  
    132.         }
    133.  
    134.         int minutes = Mathf.FloorToInt(timer / 60F);
    135.         int seconds = Mathf.FloorToInt(timer - minutes * 60);
    136.         string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
    137.  
    138.         envText.text = /*"\n" +*/ "Slope: " + bicycleSim.slopeGrade.ToString("F1") + " %" + "\n"
    139.  
    140.                        // + "CadenceOnly: " + cadence2.cadence.ToString("F0") + "RPM" + "\n"
    141.                        // + "SpeedOnly: " + speed2.speed.ToString("F2") + "Km/h" + "\n"
    142.                        //   + "FitnessDevice: " + fed.SetTrainerResistance.ToString("F0") + "\n" // Keine Ahnung!? How that works !?
    143.  
    144.                        // + "Gravity Force: " + bicycleSim.Fgravity.ToString("F1") + " n" + " (" + fpercent.ToString("F0") + "%)" + "\n"
    145.                        //  + "Roll Resist Force: " + bicycleSim.FrollingResitance.ToString("F1") + " n" + " (" + rpercent.ToString("F0") + "%)" + "\n"
    146.                        + "Aero Drag Force: " + bicycleSim.FaeroDrag.ToString("F1") + " n" + " (" + apercent.ToString("F0") + "%)" + "\n"
    147.                        +"Rounds:  " + rounds.ToString("F0") + "\n";
    148.                      
    149.  
    150.         //    + "Brake Force: " + bicycleSim.Fbrake.ToString("F1") + " n" + "\n"
    151.  
    152.  
    153.  
    154.         speedText.text = "Speed: " + bicycleSim.speed.ToString("F1") + " km/h" + "\n"
    155.                             + "Power: " + powerMeter.instantaneousPower.ToString("F0") + " w" + "\n"
    156.                             + "HeartRate: " +bpm.heartRate.ToString("F0") + " BPM" + "\n"
    157.                           //  + "HeartRate: " + bpm2.heartRate.ToString("F0") + " BPM" + "\n"
    158.                             + "Cadence: " + powerMeter.instantaneousCadence.ToString("F1") + " RPM" + "\n";
    159.         /* stridesText.text =*/
    160.         //   + "Strides:" + GameObject.Find("StrideDisplay").GetComponent<StrideDisplay>().strides.ToString("F0") + "Steps" + "\n";
    161.         //           StrideCadenceText;
    162.         //           StrideDistanceText;
    163.         //          StrideSpeedText;
    164.  
    165.  
    166.         riderText.text = //"TotalWeight: " + bicycleSim.totalMass.ToString("F1") + " kg" + "\n"
    167.                 //    + "Frontal area: " + bicycleSim.frontalArea + " m2" + "\n"
    168.                 //   + "Drag coef: " + bicycleSim.dragCoef + "\n"
    169.                 //   + "Rol resist coef: " + bicycleSim.coefficientOfRollingResistance + "\n"
    170.                 //   + "Drive train effectiveness: " + bicycleSim.drivetrainEffectiveness + " %" + "\n"
    171.                 /*+*/ "Drive Distance: " + distance.ToString ("F2") + " Km" + "\n"
    172.               //  + "Time" + timerFormatted.ToString() + "S" +"\n";
    173.               + "Time: " + niceTime.ToString() + "\n"
    174.               + "Climb: " + bicycleSim.totalClimb.ToString("F0") + " Meter" + "\n"
    175.               + "W/Kg: " + ftp.ToString("F2") + "\n";
    176.  
    177.  
    178.     }
    179.  
    180. }
    181.  
    182.  
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    It is impossible to have 2 different objects with the same identifier in the same class
    It also doesn't make any sense to, take this example:
    bpm.myValue

    Would you expect this myVale to return from the FitnessEquipmentDisplay object or the HeartRateDisplay object
     
    Andreas12345 likes this.
  5. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    If the PowerMeterDisplay active i want to grab Power and Cadence from this PowerMeterDisplay
    If the FitnessEquipmentDisplay active i want to grab Power and Cadence from the the FitnessEquipmentDisplay

    the same with HeartRateDisplay vs. FitnessEquipmentDisplay
     

    Attached Files:

  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    The code provided in the first reply would work. But I might suggest device1 and device2 (or even better names) instead of new'ing device twice.
     
    SparrowGS likes this.
  7. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    Sorry but i do not understand what to do :(
    I tried this, but will not work:
    Code (CSharp):
    1. public abstract class DemoUIDisplay : MonoBehaviour
    2.  
    3. {
    4.     public abstract float bpm { get; }
    5.  
    6.  
    7.   //  public ProfileSessionManager profileSessionManager;
    8.     public BicyclePowerSim bicycleSim;
    9.     public PowerMeterDisplay powerMeter;
    10.  
    11.     // public HeartRateDisplay bpm;
    12.  
    13.     public class HeartRateDisplay : DemoUIDisplay
    14.     {
    15.         public override float bpm
    16.         {
    17.             get
    18.             {
    19.                 return bpm;
    20.             }
    21.         }
    22.     }
    23.     public class FitnessEquipmentDisplay : DemoUIDisplay
    24.     {
    25.         public override float bpm
    26.         {
    27.             get
    28.             {
    29.                 return bpm;
    30.             }
    31.         }
    32.     }
     
    Last edited: Jan 18, 2019
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Use the first code provided, along with my suggestion. Also, where are you checking which device is active? Your code is a bit complex so make a simple example, based on the first code, so that we can better understand your issue.
     
  9. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    I do not check if active or not: both scripts or more are active. See my screenshot.
    https://forum.unity.com/attachments/unbenannt-png.361549/
    All device can deliver a value, they are physical devices.
    The Fitness Device can deliver power,cadence or the powerMeter can deliver power,cadence.
    do i need to deactivate a script that is not active and i can just write for an example:
    Code (CSharp):
    1. riderAnimator.speed = Mathf.Min(GameObject.Find("PowerMeterDisplay").GetComponent<PowerMeterDisplay>().instantaneousCadence / 50f, 2);
    2.  riderAnimator.speed = Mathf.Min(GameObject.Find("FitnessEquipmentDisplay").GetComponent<FitnessEquipmentDisplay>().cadence / 50f, 2);
    so if PowerMeter active values will be used from there
    and otherwise FitnessEquipmentDisplay will be used!?
     
  10. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    So this works for my power.
    Code (CSharp):
    1. powerInput = (GameObject.Find("PowerMeterDisplay").GetComponent<PowerMeterDisplay>().instantaneousPower) | (GameObject.Find("FitnessEquipmentDisplay").GetComponent<FitnessEquipmentDisplay>().instantaneousPower);