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.
*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…
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:
public class Airplane
{
public float Acceleration;
public float Torque;
public float EmptyWeight;
public Airplane(float acceleration, float torque, float emptyWeight)
{
this.Acceleration = acceleration;
this.Torque = torque;
this.EmptyWeight = emptyWeight;
}
}
public class MainScript : MonoBehaviour
{
private Airplane AssignedAirplane;
public void AssignAirplane(Airplane plane)
{
this.AssignedAirplane = plane;
}
public void Fly()
{
this.Move(this.AssignedAirplane.Acceleration * Time.deltaTime - this.AssignedAirplane.EmptyWeight.... blah blah blah
}
}
public static class Airplanes
{
public static readonly Airplane F15 = new Airplane(9001.0f, 10.0f, 2000.0f);
public static readonly Airplane Boeing747 = new Airplane(500.0f, 50.0f, 70000.0f);
public static readonly Airplane F16 = new Airplane(8000.0f, 15.0f, 1800.0f);
... and so on
}
public class StartupScript : MonoBehaviour
{
private void Start()
{
MainScript main = FindMainScript();
Airplane gameAirplane = Airplanes.Boeing747;
main.AssignAirplane(gameAirplane);
}
}
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.