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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Compomemt based design - having some problem understand it

Discussion in 'Editor & General Support' started by Zocky, Sep 29, 2014.

  1. Zocky

    Zocky

    Joined:
    Dec 26, 2013
    Posts:
    22
    Hey all!

    So, though i'm somewhat new to programming, i recently came across this Component based design for making games, and idea seems to be really interesting, and seems like Unity already use it aswell, so i'd like to try myself at it on simple project, mostly just to learn how that thing works.

    However, i have problems with understand how exactly it works, so i'll try to give one example, and maybe you guys can help me understand it through this example, since game would use something similar anyway.

    So lets say i have shmup-like game.
    So lets say player will be able to choose between various ships with various weapons, shields, hulls and other things.
    I would also like to have enemies with again various ways of moving, flying, walking, swimming, two legged mecha enemy, four legged, threaded wheels, etc. And it would have all those abilities like my spaceship would have too.

    So, how do i design my game with components in mind?

    I'd maybe think something like this maybe. I would make these components:
    - Health
    - DamageManager(what to do when object gets it)
    - WeaponLaser
    - WeaponPlasma
    - WeaponRockets
    - Hull
    - Walk
    - Fly
    - Shield
    - Attack
    ...

    So lets say then that i have empty gameobject with some model of enemy. In theory, if i assign to it Fly, DamageManager, WeaponLaser and Attack components, from what i understand, i should be having enemy, that flies, can receive damage and shoot at player if it spots one.

    Is this correct so far?

    Now, if i do same with player spaceship and assign both weapons, like laser & plasma, how does one say , when you press X button, you fire one of them and not the other one.
    Or if you have both fly & walk, how does one tell ship that default is walk, but player can use fly component if player press button Y?

    Basically, how do you work with these components to achieve something, when player have option to choose between various actions ? Any hints on this topic?
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    I think you're making class inheritance in Unity a little more complicated than it needs to be.

    See if this helps:
     
  3. Zocky

    Zocky

    Joined:
    Dec 26, 2013
    Posts:
    22
    Hm, tnx a lot man, it helps for sure!

    I'm still not sure in my case with ship, how would i go upon doing that.

    Like, one of the things is, that i would like more modular weapons, so that i can use same weapon on both enemies and player ship. Like, you have laser gun, and i would use same laser gun on one type of enemy and player as option.

    How would i approach this, if i want to have more component oriented design?

    If i make LaserGun as one class, and PlasmaGun as second class, that would i guess allow me to use same class/weapon in both cases.

    I guess next thing is find a ways to communicate, when enemy or players fires from that weapon, how to talk to the target's health system to do damage, based on this weapon.

    I mean, does it make sense to you guys to split weapons in such way? Any thoughts on it?
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Check out my tutorials and look at how i do a Universal Health System. Same concept applies. 1 health script and damage function for both player and enemies.
     
  5. Zocky

    Zocky

    Joined:
    Dec 26, 2013
    Posts:
    22
    Oh, sorry for late reply. Tnx a lot for your vid, it helped me a great deal! Really appreciate it!