Search Unity

C# Same script, two different objects, modifying variables on different scripts

Discussion in 'Scripting' started by Euld, Nov 29, 2012.

  1. Euld

    Euld

    Joined:
    Nov 23, 2012
    Posts:
    4
    I have three objects in play: Player1, Player2, and Duel_Arena. I need to make Duel_Arena calculate a fight between Player1 and Player2, taking into account their health, weapon and armor variables. Player1 and Player2 both use the same PlayerHealth, Armor and Weapon scripts. The Duel script is able to access the variables on the objects, but the Duel script won't do the math. Here's the code:

    Code (csharp):
    1. public class Duel : MonoBehaviour {
    2.    
    3.     public bool BeginDuel;
    4.     //player 1 stats
    5.     public PlayerHealth P1Health;
    6.     public Armor P1armor;
    7.     public Weapon P1weapon;
    8.     public Potion P1potion;
    9.     //player 2 stats
    10.     public PlayerHealth P2Health;
    11.     public Armor P2armor;
    12.     public Weapon P2weapon;
    13.     public Potion P2potion;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.    
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.    
    23.     if (BeginDuel==true)
    24.         {
    25.         //player 1 attacks first
    26.        
    27.         //P1Health = Player1attack (P1Health, P2weapon, P1armor);
    28.            
    29.         //     var otherScript: OtherScript = GetComponent(OtherScript);
    30.         //otherScript.DoSomething();
    31.         //P2Health = P2Health - (P1weapon - P2armor);
    32.         //player 2 attacks second
    33.         //P1Health = P1Health - (P2weapon - P2armor);
    34.         }
    35.         else
    36.         {
    37.         }
    38.     }
    39.    
    40.     public int Player1attack(int x, int y, int z)
    41.         {
    42.         int result;
    43.         result = x - (y - z);
    44.         return result;
    45.         }
    46.    
    47. }
     
  2. BlankFoxGirl

    BlankFoxGirl

    Joined:
    Apr 26, 2010
    Posts:
    71
    Basically, you want to calculate an Effect. Basically the Effect variable should be the Final amount of damage from a single hit, or for the whole fight.

    So the first thing would be to declare the variable, in this example, I am using (int) because I want it to be a whole number.

    PHP:

    public class Duel :  MonoBehaviour{

    public static 
    int Effect 0//Set to 0 first.

    public static void playerAttack(string Actorstring Context){
    //Use a standard integer for the min / max damage.
    int min 1;
    int max 5;
    int critChance 15//This is a percentage!
    System.Random RandNum = new System.Random();
                        
    Effect RandNum.Next(mindmgmaxdmg);
                        if (
    RandNum.Next(100) <= critChance){
                             
    Effect = (int)Effect 1.5//Add the critical hit multiplier, then turn into int / whole number.
                        
    }
    player p player.FindByName(Context);
    p.removeHP(Effect); // This is where you go about removing player Health Points.
    }

    }

    Naturally, this is a single script to run through a basic attack, when the attack has been calculated, it will run the public void "removeHP" function, and deduct the Effect from the Context Player's health.

    To call this attack, simply call;
    PHP:
    Duel.playerAttack(string AttackingPlayerstring PlayerBeingAttacked);
    You can easily turn this into a duel script by creating a public static bool called duel, and code in some more logic to verify the players are both within the duel, and then allow the attack, otherwise, do nothing, or return with a message stating that the target is not able to be attacked.

    If you are doing this online, I strongly recommend using this as a ServerSide script only as it calculates the amount of damage done to a player, this will prevent the occurrence of a Trainer, or a hack to spoof a large amount of damage via packet spoofing.
     
  3. Euld

    Euld

    Joined:
    Nov 23, 2012
    Posts:
    4
    I understand everything up until you call the removeHP function. Now I know how to call functions, but I think I need to see what's in your removeHP function. Even when I make an Effect variable, Unity gets upset when I try to do any calculations with integers from other scripts. Like so:

    Effect = player1's armor - player2's damage;
    player 1's health = player 1's health - damage;

    edit: well now I feel silly. A classmate said to check our instructor's notes online, and apparently we're not supposed to put multiple scripts on single objects yet. Silly me, that's why I'm lost.
     
    Last edited: Nov 29, 2012