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

Best way to incorprate variable libraries?

Discussion in 'Scripting' started by Mirage, Jan 15, 2011.

  1. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    So I'm working on the flight sim engine and want to incorporate a easy method of importing/adding new aircraft. My plan is to have a function in my flight engine script that reads data from (somewhere) and sets its variables accordingly. Probably straightforward I just don't know too much about how to do this in Unity.

    My initial idea is to have separate scripts that are only used to store variables; for instance a script that holds F15.acceleration, F15.torque, F15.weight_empty... etc and set the standard "acceleration, torque, weight_empty" variables in the flight engine to the F15 set.

    I would normally do something like "main-script.acceleration = F15.acceleration" but since the "F15" part will always be changing I'm not sure how to handle this...

    Might be that the above is the easiest way, I would hate to spend hours coding things a certain way only to find out later about an easy alternative.
     
  2. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    *bump


    So far the only thing I can think of is using nested arrays, first array are airplane classes and their sub arrays store all the data required.

    This seems like a very tedious way to handle things, inevitably an array value will be off and an f16 will end up with the stats of a Boeing 747...
     
  3. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Do all your airplanes share the same types of values? That is, do they all have "acceleration", "torque", "weight_empty", etc? Then you should just define an "airplane" class and work from that:

    Code (csharp):
    1. public class Airplane
    2. {
    3.     public float Acceleration;
    4.     public float Torque;
    5.     public float EmptyWeight;
    6.    
    7.     public Airplane(float acceleration, float torque, float emptyWeight)
    8.     {
    9.         this.Acceleration = acceleration;
    10.         this.Torque = torque;
    11.         this.EmptyWeight = emptyWeight;
    12.     }
    13. }
    14.  
    15. public class MainScript : MonoBehaviour
    16. {
    17.     private Airplane AssignedAirplane;
    18.    
    19.     public void AssignAirplane(Airplane plane)
    20.     {
    21.         this.AssignedAirplane = plane;
    22.     }
    23.    
    24.    
    25.     public void Fly()
    26.     {
    27.         this.Move(this.AssignedAirplane.Acceleration * Time.deltaTime - this.AssignedAirplane.EmptyWeight.... blah blah blah
    28.     }
    29. }
    30.  
    31. public static class Airplanes
    32. {
    33.     public static readonly Airplane F15 = new Airplane(9001.0f, 10.0f, 2000.0f);
    34.     public static readonly Airplane Boeing747 = new Airplane(500.0f, 50.0f, 70000.0f);
    35.     public static readonly Airplane F16 = new Airplane(8000.0f, 15.0f, 1800.0f);
    36.     ... and so on
    37. }
    38.  
    39. public class StartupScript : MonoBehaviour
    40. {
    41.    
    42.     private void Start()
    43.     {
    44.         MainScript main = FindMainScript();
    45.        
    46.         Airplane gameAirplane = Airplanes.Boeing747;
    47.        
    48.         main.AssignAirplane(gameAirplane);
    49.     }
    50.  
    51. }
    Might be better examples, but hopefully gives you some ideas. There are other ways of defining the different airplanes (for example, XML serialization) but this is just a pretty simple example and from the sounds of it, I think might suit your purposes.
     
  4. Kaya

    Kaya

    Joined:
    Dec 21, 2010
    Posts:
    50