Search Unity

DB or Prefab to save game elements

Discussion in 'Scripting' started by padomu, May 12, 2014.

  1. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    Hi,

    Sry for the bad title.

    Imagen you have a game like Magic The Gathering. You have a play field of 4x4 Cards, where you can drag cards on. So you have 16 fields which are empty GameObjects.

    Each Cards has a few properties like stats, sprite and the Action/Bonus. This bonus can be anything, stuff like "Multiply points with 2", "Double the time" etc.

    So I have several Questions.

    My first thought was creating a basic card prefab. But I don't know if it wouldn't be better to save this in a DB. A DB could be compicated to achieve. I'd need validating and implementing the bonus could be harder by using a DB. When usign a Prefab, I could just add a function which does the manipulating.

    What would you use? Database or Prefab?

    If you use a prefab and do the manipulating fro mthe Bonus from the card via script, how would you do the communication between the Cardscript and the rest of the app, a GameMaster::AddBonus() function, or an event? How to pass what to do? Use a List with references to the function of the current prefab which GM should call? Assuming you have a decoupled architecture with a GameMaster, GUIManager etc. Goal is to just interact with the GameMaster. (But I'm open to everything)

    Oh it should be easily! portable to Web, Mobile, Desktop.
     
    Last edited: May 12, 2014
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Prefabs are probably the best call. They're easier to edit in Unity, and if your cards have many possible functions, they can be easily added and scripted.

    The only good reason to use a database would be if you DON'T plan on building it within Unity. If the card data exists elsewhere (like if you're porting an existing tabletop card game to electronic version), for example, or if you want the player to be able to download new cards without downloading a new version of the game. In that case you might want to use a text-based or database system, because you'll be reusing code then.
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    The prefab is a good system. If that works for you keep going with it.

    I've grown to like putting configs, save data, item data, etc... into JSON files. Then we use LitJSON (or some other JSON loader) to load it all into a bunch of C# structs super easy.

    JSON was written specifically to organize data for JavaScript, but it really works for just about any programming language out there.
     
    Last edited: May 13, 2014
  4. padomu

    padomu

    Joined:
    Sep 8, 2013
    Posts:
    51
    I know JSON etc. not the first time I code, but Unity is something new and I really have to get used to it.

    Ah I forgot, the player can buy new cards and also create desks. Also play online.

    I think the best choice would be:
    Create a prefab for each card and save the structure in db. With structure I mean: Player A has the cards 1,2,3,4,5,6,7,8... and the desk X,Y and Z and they consist of cards .... etc. I think, this makes absolute sense. But I need a identifier, how is this solved? Take the name of the prefab? Create a property in its class? Maybe use some internal ID? (which probably is dynamic so not usable)

    So, hwo would you do the identifier on the prefab?
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I see, a collectable card game with many unique cards, not like a standard deck with suits and set numbers...

    I assume that each card has a unique id. Making the name of the prefab the same as the id of that card makes sense to me.

    Then you can do stuff like load cards from Resources really easily without having to convert ids or translate anything.
    Code (csharp):
    1.  
    2. public static Card GetCard(string cardId) {
    3.   return Resources.Load<Card>(cardId);
    4. }
    5.