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

Edit other script with script

Discussion in 'Scripting' started by Valiunia, Sep 28, 2014.

  1. Valiunia

    Valiunia

    Joined:
    Dec 31, 2013
    Posts:
    2
    Hello everyone, very urgent question here
    How can I edit other scripts with main script and then save those scripts and turn them on, while in 1 game session.

    Or the other question:
    how can I check statement inside of a string?
    example var A :String = " A || B && C";
    if ( A ){
    }

    I need this because I'm trying to mutate that string and I need my if statement to be dynamic to addapt to new statement inside of the string...
     
  2. Cuniculator

    Cuniculator

    Joined:
    Sep 7, 2014
    Posts:
    9
    Can't you just use a public variables?

    If you only have one instance of your class you could make one or more public static variables so you can always acces it. Else you can refer to a class trough the object it's attached to.

    For example:
    Code (CSharp):
    1. public class Class1{ //the one you want to acces
    2.     public static bool A = false;
    3.     public static bool B = false;
    4.     public static bool C = false;
    5.  
    6.     public void test(){
    7.         if(A || B && C){
    8.             print("Yay");
    9.         }
    10.     }
    11.  
    12. }
    Code (CSharp):
    1. public class Class2{ //the one you acces Class1 from
    2.  
    3.     public void setVars(bool a, bool b, bool c){
    4.         Class1.A = a;
    5.         Class1.B = b;
    6.         Class1.C = c;
    7.     }
    8.  
    9. }
     
    Magiichan likes this.
  3. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Based om these questions it sounds like you are trying to aproach your problems in a wrong way.

    Firstly you are better off using states and state machine. This will allow you to change the behavior of a script on runtime. You dont need to touch sourcode through scripts unless you are making some sort of editor script.

    As for the second question, youd need to implement some sort of crazy string parser that handles each statement for you separately. which again is probably something you dont want to do.

    Dynamic if statements are easier to do with interfaces, states and delegates depending on what, where and why you are comparing something.
     
    Magiichan likes this.
  4. Valiunia

    Valiunia

    Joined:
    Dec 31, 2013
    Posts:
    2
    Believe me, I know what I'm doing. I need to mutate the whole statement inside of the "if". Because I need to check something like if ( A && B && C ) and sometimes I need to check if ( A && B && ! C ) and then I need to check something I haven't checked before, and I need to check all possible ways and after that like add one more variable Do and check everything again. ... It is very complex and I have the statements generation going I just don't know how to check things I generate...
     
  5. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Delegates a.k.a. "function pointers for C#" are pretty useful for this sort of thing:

    Code (CSharp):
    1. enum LogicState
    2. {
    3. First,
    4. Second,
    5. Third
    6. };
    7. private delegate void LogicDelegate;
    8. private LogicDelegate RunLogic;
    9.  
    10.  
    11. void Start()
    12. {
    13.   RunLogic = First;
    14. }
    15. void SetLogicState(LogicState state)
    16. {
    17.   switch(state)
    18. {
    19. case First:
    20.    RunLogic = First;
    21.    break;
    22.   case Second:
    23.    RunLogic = Second;
    24.    break;
    25.   case Third:
    26.   RunLogic = Third;
    27.    break;
    28. }
    29. }
    30.  
    31. void First(){}
    32. void Second(){}
    33. void Third(){}
    34.  
    35.  
    36. void Update()
    37. {
    38.    if(RunLogic != null)
    39.    {
    40.       RunLogic();
    41.     }
    42. }
     
  6. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Here's another way to do this.

    Code (CSharp):
    1. //Validation interface
    2. public interface IValidateObject
    3. {
    4.     bool Validate(ValidationTarget val);
    5. }
    6.  
    7. //class for validation
    8. public class ValidationTarget
    9. {
    10.     int _A, _B, _C;
    11.     public List<IValidateObject> _validations;
    12.  
    13.     public int A { get { return _A; } }
    14.     public int B { get { return _B; } }
    15.     public int C { get { return _C; } }
    16.     public List<IValidateObject> Validations { get { if (_validations == null) _validations = new List<IValidateObject>(); return _validations; } }
    17.  
    18.     public ValidationTarget(int a, int b, int c) { _A = a; _B = b; _C = c; }
    19.  
    20.     //Simply goes trough all validations that have been added to the list.
    21.     public bool Validate()
    22.     {
    23.         Debug.LogWarning(A + " " + B + " " + C);
    24.         bool valid = true;
    25.  
    26.         if (Validations.Count < 1)
    27.         {
    28.             Debug.LogWarning("No validations given");
    29.             return valid;
    30.         }
    31.  
    32.         for (int i = 0; i < Validations.Count; i++)
    33.         {
    34.             if (!Validations[i].Validate(this))
    35.             {
    36.                 valid = false;
    37.             }
    38.         }
    39.  
    40.         return valid;
    41.     }
    42. }
    43.  
    44.  
    45. //validation A > B
    46. public class AGreaterThanB : IValidateObject
    47. {
    48.     static AGreaterThanB _instance; //Singleton since it's only used to validate given objects.
    49.     public static AGreaterThanB Instance
    50.     {
    51.         get
    52.         {
    53.             if (_instance == null)
    54.                 _instance = new AGreaterThanB();
    55.  
    56.             return _instance;
    57.         }
    58.     }
    59.     public bool Validate(ValidationTarget target)
    60.     {
    61.         return target.A > target.B;
    62.     }
    63. }
    64.  
    65. //validation2 B > C
    66. public class BGreaterThanC : IValidateObject
    67. {
    68.     static BGreaterThanC _instance;
    69.     public static BGreaterThanC Instance
    70.     {
    71.         get
    72.         {
    73.             if (_instance == null)
    74.                 _instance = new BGreaterThanC();
    75.  
    76.             return _instance;
    77.         }
    78.     }
    79.     public bool Validate(ValidationTarget target)
    80.     {
    81.         return target.B > target.C;
    82.     }
    83. }
    With this way you can add and remove checks even on runtime and even call pubic methods straight from the validation itself to act on the results. Validations are implemented as singletons as they do not hold any data, this way one object can validate multiple objects and values.

    Example usage:

    Code (CSharp):
    1.   ValidationTarget vd = new ValidationTarget(3,2,1);
    2.         vd.Validations.Add(AGreaterThanB.Instance);
    3.         vd.Validations.Add(BGreaterThanC.Instance); //object passes Both validations
    4.         Debug.Log("Validation result: "+ vd.Validate());
    5.  
    6.         ValidationTarget vd2 = new ValidationTarget(3,1,2);
    7.         vd2.Validations.Add(AGreaterThanB.Instance);
    8.         Debug.Log("Validation2 result: "+ vd2.Validate());
    9.  
    10.         vd2.Validations.Add(BGreaterThanC.Instance); // object wil not pass this validation as objects B < C
    11.         Debug.Log("Validation2 result: " + vd2.Validate());
    12.  
    13.         if (vd2.Validations.Contains(BGreaterThanC.Instance)) // remove validation to make it valid again
    14.             vd2.Validations.Remove(BGreaterThanC.Instance);
    15.  
    16.         Debug.Log("Validation2 result: " + vd2.Validate());
    if you are unfamiliar with these sorts of design patters, this might take a bit to swallow. But this can solve both of your problems as similar thing can be used with state machine as well to influence the behavior of your objects on runtime.
     
    Last edited: Sep 29, 2014