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

Help / advise needed with code architecture

Discussion in 'Scripting' started by Steve-OTheValks, Feb 27, 2020.

  1. Steve-OTheValks

    Steve-OTheValks

    Joined:
    Jul 29, 2018
    Posts:
    5
    Hello. I am building a couple of systems for my current project. One system will handle enemy spaceships and another will handle weapons which the spaceship can have. A spaceship should be able to have multiple weapons of different types, or all the same type. A weapon should be able to have different properties, such as fire rate, bullet speed, firing patterns, burst fire with configurable number of shots per burst, intervals between bursts etc. I would like to be able to set these properties of the weapon from the enemy spaceship, so I can use the same weapon on different spaceships and have them behave differently. I started off by creating a couple of base classes, one for the spaceship and one for the weapon. Child classes were then derived from the base class for each different spaceship and weapon. I am currently having to declare variables to hold the weapon properties in the spaceship class so I can then configure them from the spaceship and pass them to the weapon classes, but I feel there should be a better way so I don't have to have the same variables in both spaceship and weapon classes. I've tried using interfaces but the properties of the interface don't seem to appear in the Inspector. I've read that classes that inherit from MonoBehaviour are not serializable so that's why they don't show in the Inspector. Is there a better way I should be architecting my code? I'm also looking at splitting the code to handle the different weapon abilities into separate classes (the single responsibility principle), that way if I want a weapon to have a particular ability I can just drag that script onto it. All thoughts are greatly appreciated :)
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Without knowing how you expect to be creating a ship, who is creating the ship, and how the ship is controlled, its hard to give advice

    What i picture is, each weapon is a gameobject with a monobehavior script controlling it, and they are child objects of the ship, the ship can simply do a GetComponentsInChildren to get all its weapons.

    is the player allowed to mod a ship? or even create new ships?

    if the ship is controlled like an RTS, where a target is given and attacks happen, the only thing the ship has to pass to the weapons is what target they are shooting at and all weapons will do the rest.
     
  3. Steve-OTheValks

    Steve-OTheValks

    Joined:
    Jul 29, 2018
    Posts:
    5
    I was afraid my long post would put people off :eek: A little more info...

    The ship will appear on specific levels in the game and will act as like a boss fight. The player will not have any control over the ship. The ship will simply fly to a set position in the middle of the screen deploy its weapons and start firing (think bullet hell). Currently my ships have a basic armature that I use to attach the weapons to, so yes the weapons are child gameobjects of the ship. I'd like to be able to use the same weapons on different ships and depending on the ship it's attached to will have different values for things like firerate, fire pattern, bullet type, bullet speed etc.
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Depends entirely on who's reading, which you cannot control. In general providing alot of relevant info is better than being too brief.

    So we can agree the ship and its weapons are entirely developer controlled. You even mention the weapons are child objects of the ship. What is stopping you from making the weapons prefabs to use on any ship as many times as you wish?
     
  5. Steve-OTheValks

    Steve-OTheValks

    Joined:
    Jul 29, 2018
    Posts:
    5
    Yes, I do have the weapons as prefabs, so putting them on any ship is no problem. My concern was that depending on which ship they were attached to, I wanted the weapon to have different abilities and different values for those abilities. Eg, we have GunType1 which when attached to Ship1 would be able to fire at a rate of 1 bullet per second, but when attached to Ship2 would be able to do a burst fire of 3 bullets and 1 bullet every 0.5 seconds and then an interval between bursts of 1 second. Also other variances depending on what ship a weapon is attached to, such as a different firing pattern, like the weapon might fire in an arc, or simply in a random direction.
     
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    I guess the question here is, what purpose is there for the properties of the weapon to come from the ship. Is anything stopping you from just defining the properties of the weapon directly in the weapon via inspector?