Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Card Game scriptable objects

Discussion in 'Scripting' started by Shadoso, Apr 19, 2021.

  1. Shadoso

    Shadoso

    Joined:
    Apr 7, 2021
    Posts:
    5
    I know the basics of python, but I never used C# and I’m not that good transforming “Things” into scriptable objects. I would like to make a game similar to Underhand, for those who don't know this game I will put some images explaining about the game.
    Cartas resources.png Cartas eventos.png
    And there are some things that I want to do, like in the florest is rule 1 and in the city is rule 2 (in the forest you can starve, at city you can’t starve), I don’t know if I add those things to the cards or this need to be a condition, others mechanics like intimidation; use your evil reputation instead of money
    I'm having trouble transforming the event cards and the resource cards into scriptable objects. What it needs, what it doesn’t needs

    The below part is just a poor example.

    ------------------------------------------------
    EventCards
    ImageFront; .png
    Name; (“Goblin”)
    BelowDescription; (“A worthy opponent")
    Theme; (MiddleAge) (Which theme it can be used)
    GameMode; [Mage, Warrior] (Which mode it can be used)
    GameRules; [Normal, Vilain] (Maybe this isn't necessary, making a condition can solve it )
    ImageBack; .png
    CardsAdress; [#0011, option2, option3]
    EventKeyword; (SwitchOptions) (For special events that need more than 3 options, )
    Rarity; (Legendary)
    ------------------------------------------------
    OptionCards
    ImageFront; .png
    Name; (“Fight”)
    ResourcesNeed; (2 unities of power card)
    ResourcesGain; (1 unit of gold card)
    Description; (“Kill the goblin")
    OptionKeyword; [TradeEvent, Shuffle 2 goblins into the deck]
    Theme; [MiddleAge] (Which theme it can be used)
    GameMode; [Mage, Warrior] (Which mode it can be used)
    Address; (#0011)
    ImageBack; .png
    ------------------------------------------------
    In this part I'm very lost

    ResourcesCards
    Name; (“WildCard”)
    ResourceKeyword; [Food, Gold, etc]
    ImageFront; .png
    ImageBack; .png
     
  2. chengwang2077

    chengwang2077

    Joined:
    Nov 23, 2019
    Posts:
    131
    maybe use prefab is better In this situation
     
  3. Shadoso

    Shadoso

    Joined:
    Apr 7, 2021
    Posts:
    5
    After you commented on the prefabs, I went to research more about it, it probably wouldn't be the best solution, in the long run the prefab makes the game heavy, and also if I used prefabs I would have to create 300 prefab variants, and the goal of the prefab is to be able to reproduce 1 thing en masse, and at first each event card is completely different, probably for resource cards like gold and food it could work extremely well
     
  4. chengwang2077

    chengwang2077

    Joined:
    Nov 23, 2019
    Posts:
    131
    Two ways there
    1 Scriptable objects + prefabs
    2 Editor script + prefab
    If you use Scriptable objects + prefabs, then you may need this component "ClassTypeReference-for-Unity", because the card ability is script and "Type" cannot be serialized
    If you user Editor script + prefab, Don't make variants of prefabs, because modifying the script can easily lead to the loss of design values. It is better to use editor scripts to generate prefabs
    I think the editor script + prefab is better because you can see what card like in editor without play game!
     
    Shadoso likes this.
  5. Shadoso

    Shadoso

    Joined:
    Apr 7, 2021
    Posts:
    5
    I took a look at what you said about not being able to be serialized, so maybe Json could solve what you think?
    I still don't know json, but from what I saw it seems to be a solution too
     
  6. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    you said 300 prefab variants, when you are expecting numbers this high, you probably need some automation process... doesnt really matter wether you do scriptables or prefabs at this point, both would take forever

    what I have done for one of my projects where I had a simillar problem:

    1 general base prefab that has a component that reads out info from scriptable object (in your case reading out the sprite, the name, the description etc and writing those to a spriterenderer/ text fields)

    1 general scriptable object script that defines all the general stats every card has like name sprite etc
    potentially more scriptable object classes that inherit from the first one for the different card types (event/ option), alternatively you could do this all in a single scriptable object script and fill out the for the card relevant fields

    and lastely a defined directory structure in my assets, like for you this might be something like:
    CardsDirectory > {OptionCards, EventCards} > [cardname] > {Sprite, infoJson}

    and then you just simply need an editor-only script (as in one that wont be in the runtime build) that goes through your asset hirachy, reads out all the card information and generates all the scriptable objects you need

    as in for every card type in CardsDirectory it goes through every defined card and looks up its defined informational assets and creates a new scriptable object, fills it out with the card information, and then saving the scriptable object

    ideally you want 1 more scriptable object that is simply a list of all the scriptable objects of your cards, to which you automaticly add every newly created scriptable

    your runtime application than only needs to get that list and read it out^^
     
    Last edited: Apr 21, 2021
    Shadoso likes this.
  7. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    about the serialization:
    what chengwang2077 is pointing out is that while general files like sprites, strings, numbers etc can be easily saved (serialized and written down), and loaded from that serialization, you cant say the same thing about behaviour definitions

    you can not just save behaviour definitions into a json or scriptable object and load it back into the game

    what you can do is predefine behaviours or behaviour components in unity scripts and save an id or some sort of reference to them in the scriptable objects (as in save a number not a behaviour)

    as a summary:
    you can not say: save a behaviour and load it into your object on creation
    you can say: save a refernce/ id to an existing script, and when loading in your object, find that specific script using its id and add it to your object

    in other words, any special abilities your cards can perform need to be predefined outside of your card definition, and than loaded into the card using some sort of reference (unless you do prefabs for every card than you can just attach the component for that behaviour to the card)
     
    oskar-s, Munchy2007 and Shadoso like this.
  8. Shadoso

    Shadoso

    Joined:
    Apr 7, 2021
    Posts:
    5
    Sorry english isn’t my first language (Sometimes my english gets rusty ) It was a little bit hard to understand, and like I said I never used unity, I just know the basics of python. All the things that both said I’m searching about it, but I got confused about “1 general base prefab that has a component that reads out info from scriptable object (in your case reading out the sprite, the name, the description etc and writing those to a spriterenderer/ text fields)” So If I get it, will be like, there are just one prefab that show informations, and when I do an action this prefabs will read another information, and again and again ?
    If you know some website or video that have some example it will help me a lot, for while the best example that I found was in this channel
     
  9. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    i dont really have a tutorial for this because its just something i figgured out for myself on my own

    what I meant with:
    is:

    you create a prefab that has everything a card would need:
    - ui images or sprites to display the cards textures
    - text fields to display the cards name/ description/ stats
    (- if different card types have different layouts than for every card type a child object that corrensponds to the layout (as in if event cards display their image somewhere else than option cards))

    this prefab will be the base of all your cards, when loading in a card into your game, you will simply make a clone of this prefab, and then fill out all the relevant fields with the cards information

    so you instantiate a general base card that could be anything, and afterwards you tell that instantiated clone of the general base what image, text, etc it shall display

    to actually steer all the cards properties, you want to give this base prefab a script, that has all the relevant components linked

    a small example:

    the script has a variable:
    public SpriteRenderer cardImage;
    you link to this variable the SpriteRenderer of your prefab by dragging it in in the inspector

    and then a function where you set the sprite
    cardImage.sprite = someNewSprite;

    thus the script is able to change the look of the instantiated clone of base prefab ingame

    the idea than is to allow that script to read out the cards properties (name, sprite, etc) from a scriptable object where they are defined and apply it to the game object
     
    Last edited: Apr 21, 2021
    Shadoso likes this.
  10. Shadoso

    Shadoso

    Joined:
    Apr 7, 2021
    Posts:
    5
    Thanks S2