Search Unity

Best way to structure classes in a card game?

Discussion in 'Scripting' started by Comafly, Aug 31, 2015.

  1. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Hi everyone. I'm just working on a pet project to get my head around Unity, and it's a little game with different characters represented as cards. I'm planning on maybe 20-30 cards and was wondering the best way to structure the card stats so I don't have 20-30 different class files. They each have a name, ability, and description.

    Is there an easier way to store/retrieve this information than creating separate class files for each one?


    Cheers!

    Brendan
     
  2. jaybdemented

    jaybdemented

    Joined:
    Sep 2, 2013
    Posts:
    112
    I'm doing something similar but just not a card game. I'm making a static reference object that holds a array for each stat.

    Let's say you need the health for card number 5. You check the ref object health [5]. Something like that. Hope that help im not the best coder.
     
  3. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Hey Jaybdemented, just thought I'd let you know I found an amazing tutorial that covered exactly what I needed.



    Simply put, he uses Lists and Enums to create custom items with different stats. Good luck on your project!
     
  4. jaybdemented

    jaybdemented

    Joined:
    Sep 2, 2013
    Posts:
    112
    Right on. I'll check that out after work thx for the share.
     
  5. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Ideally, instead of 30 different classes you would want to create 30 GameObjects that have various components attached to them. You should always tend to look at your cards "what are they composed of" instead "what kind of card this is".

    To understand this, please take a look at a video Mr. @BoredMormon posted, it will help you understand what I mean.


    After you checked out the video, you could structure them as:

    GameObject:
    HealthComponent -> Contains hp, etc
    AttackComponent -> Has all info regarding attack strength, logic, etc.
    DisplayComponent -> handles what image is drawn on card
    MetadataComponent -> rarity, extra info
    AbilityComponent -> Container class for your actual ability scripts (like dmg, heal, status...)

    That way you can reuse same classes for both your and opponent cards and experiment a lot. Making a new card is simply attaching all those components to a empty game object and filling them in with data via inspector, not hardcode. Once the card is done, just drag it to your Resources folder as a prefab.

    Hope this helps!
     
    Kiwasi likes this.
  6. Gerald Tyler

    Gerald Tyler

    Joined:
    Jul 10, 2015
    Posts:
    80
    So are the component's just scripts or what?
     
  7. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Yeah, like:

    Code (CSharp):
    1. public class HealthComponent : MonoBehaviour
    2. {
    3.     ....
    4. }
    5.  
    6. public class AttackComponent : MonoBehaviour
    7. {
    8.     ....
    9. }
    10.  
    11. public class DisplayComponent : MonoBehaviour
    12. {
    13.     ....
    14. }
    15.  
    16. //etc etc