Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Dynamically adding variables/if statement to scripts

Discussion in 'Scripting' started by lwaarbroek, Sep 4, 2012.

  1. lwaarbroek

    lwaarbroek

    Joined:
    Oct 9, 2010
    Posts:
    19
    Hey everyone:D,

    I am trying to come up with a system for damageTypes which designers can easily extend.

    I have 3 scripts:
    DamageTypes.cs - Is currently just an Enum with the available damageTypes.
    Projectile.cs - Designers should be able to select via dropdown menu which damageType this projectile has.
    Health.cs - Designers should be able to fill in float values for each damageType.

    Current workflow to add new DamageType:

    DamageTypes.cs
    Add Fire to enum
    Code (csharp):
    1. public enum damageTypes {
    2.  
    3.     flesh,
    4.     armor,
    5.     fire // new option
    6.    
    7. }
    Projectile.cs
    Checks what options are in the DamageTypes.cs and shows it as public variable.
    Code (csharp):
    1. public damageTypes damageType;
    $projectilePrefab.png

    Health.cs
    Add float fireDamage
    Code (csharp):
    1.  
    2. public float armorDamage;
    3. public float fleshDamage;
    4. public float fireDamage; // public float so designers can edit
    5.  
    Add if check with new damageType and adjust adj value with new fireDamage float

    Code (csharp):
    1. public void adjustHealth(float adj, damageTypes dmgType){
    2.        
    3.         if(dmgType == damageTypes.armor){
    4.            
    5.             adj *= armorDamage;
    6.         }
    7.        
    8.         else if (dmgType == damageTypes.flesh){
    9.  
    10.             adj *= fleshDamage;
    11.         }
    12.        
    13.         else if (dmgType == damageTypes.fire){ // have to add if check for new damageType
    14.             adj *= fireDamage; // adjust adj value with new float fireDamage
    15.         }
    16.     }

    So I want the stuff I have to add to the code now to be generated whenever a designer adds a damageType in an editor window or custom inspector.

    Picture to clarify stuff:
    $damageTypes.png

    Any ideas on this would be greatly appreciated.
     
  2. PAEvenson

    PAEvenson

    Joined:
    Nov 12, 2009
    Posts:
    47
    Well, in theory, you could use basic file IO to open the cs files search for specific text like "//@new damage types", insert your new line of code, then save it out. I wouldn't suggest it.

    I would suggest making your classes a little more modular.

    Code (csharp):
    1.  
    2. [System.Serializable]
    3. public class DamageTypeClass
    4. {
    5.     public string damageType;
    6.     public float damageMultiplier;
    7. }
    8.  
    9. [System.Serializable]
    10. public class ResistDamageClass
    11. {
    12.     public string resistType;
    13.     public float resistMultiplier;
    14. }
    15.  
    16. //Then over in Health.CS
    17. public class Health
    18. {
    19.     public List<ResistDamageClass> resistances;
    20.  
    21.     //pass in all of the damageTypes of the Projectile
    22.     public void adjustHealth(List<DamageTypeClass> damageTypes)
    23.     {
    24.         foreach(DamageTypeClass damaetype in damageTypes)
    25.         {
    26.             //TODO:check for resistance, if so reduce damage
    27.             damage *= damaetype.damageMultiplier;
    28.         }
    29.     }
    30. }
    31.  
    32. public class Projectile
    33. {
    34.     public List<DamageTypeClass> damaeTypes;
    35. }
    36.  
    Designing it this way will prevent you from corrupting your class files.
     
  3. lwaarbroek

    lwaarbroek

    Joined:
    Oct 9, 2010
    Posts:
    19
    Thanks man! Really had no idea how to solve this but this certainly helps.
     
  4. lwaarbroek

    lwaarbroek

    Joined:
    Oct 9, 2010
    Posts:
    19
    The resistance part:

    Code (csharp):
    1.  
    2. public void adjustHealth(float adj, List<DamageTypeClass> damageTypes) {
    3.  
    4.     foreach(DamageTypeClass damagetype in damageTypes){
    5.        
    6.          foreach(ResistDamageClass resist in resistances){
    7.            
    8.             if(resist.resistType== damagetype.damageType){
    9.                 adj *= (damagetype.damageMultiplier / resist.resistMultiplier);
    10.             }
    11.            
    12.             else {
    13.                 adj *= damagetype.damageMultiplier;
    14.             }
    15.         }
    16.     }
    17. }
    18.  
     
  5. PAEvenson

    PAEvenson

    Joined:
    Nov 12, 2009
    Posts:
    47
    No problem. Glad it helped.