Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Scriptable Object card game pls help

Discussion in 'Visual Scripting' started by Alterrion, Apr 8, 2021.

  1. Alterrion

    Alterrion

    Joined:
    Apr 8, 2021
    Posts:
    3
    So I am using Bolt to create a simple card game and need a way to assign card effects to the specific cards. Right now I have a scriptable object setup so that each card has its own description of mana costs, name, even sprite image, but I don't know how to make it do an effect when it is played/clicked. E.G. when a "red" card is in my hand, I know it is a "red scriptable object" and can assign the correct image to it, when played I can adjust its cost etc., but how to make it let's say deal damage to an opponent when clicked (without having all the code for each card in one flow machine and just checking which one to activate)?

    I though about whether you can add something like a new flow macro to the scriptable object, when you can even add "public Sprite", so that I could just access and run that (I can't really do C# apart from that), or perhaps I am doing it all wrong? Thanks for any tips.
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    113
    You could do card effects as enums, assign an enum card effect to a card SO, then use Switch On Enum node in Bolt/UVS to do different things based on the played cards enum.
     
    Last edited: Apr 8, 2021
  3. Alterrion

    Alterrion

    Joined:
    Apr 8, 2021
    Posts:
    3
    Never done that, but looking at how enums work, it looks like it just acts as a type-to-integer converter. If I understand correctly, I would still need to have all the card effects somewhere in a macro and just choose one of them based on the enum, but I was more looking for a way to have the actual macro/effect inside the SO. IDK if that's possible but I feel like that approach works for things like drawing cards or getting mana, but once you have complex and unique effects, it's super inefficient. But thanks, I guess it's a good start.
     
  4. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    113
    Can't have Bolt/UVS logic inside scriptable objects. You could reference a macro graph in SO I guess, but I don't know anyone who's been able to build something modular that way. At best you can swap a whole graph on some FlowMachine in the scene, I think. Never done it myself. And C# docs are near nonexistent for both Bolt and UVS, can't really tell what's possible without reading source and trying to reverse engineer stuff.

    You could have a list of <Effect> - a C# class or struct that holds an enum and an int inside it. Enum for the Switch on Enum and int for effectStrength. The list can hold any amount of Effects so you just run a for each loop over it and have switch on enum sort it out. It can quickly get out of hand if you have a lot of unique events, however. That's true. But with a list you can combo the effects in any way you need. So SO only has descriptions of events, the logic is then handled by some GameBoard Bolt graph via Switch On Enum.
     
    Last edited: Apr 13, 2021 at 12:03 PM
  5. Alterrion

    Alterrion

    Joined:
    Apr 8, 2021
    Posts:
    3
    That sounds like it could work, yeah. What's the format for the SO then? public List something? Thanks.
     
  6. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    113
    I'd structure it something like this:

    1. Have a base Card SO that contains info all card types share. ie Card Name string, Card Art sprite, Description string, etc.

    Code (CSharp):
    1. using UnityEngine;
    2. using Ludiq;
    3.  
    4. [IncludeInSettings(true)]
    5. public class Card : ScriptableObject
    6. {
    7.     [Header("Basic Card Info")]
    8.     public string cardName;
    9.     [TextArea]
    10.     public string cardDescription;
    11.     public int cardCost;
    12.     public Sprite cardArt;
    13. }
    2. Create a card effect struct or class and event effect type enum.

    Code (CSharp):
    1. using System;
    2.  
    3. public enum EventEffectType
    4. {
    5.     DoDamage,
    6.     RestoreHealth,
    7.     AddDefense
    8. }
    9.  
    10. [Serializable]
    11. public struct EventEffect
    12. {
    13.     public EventEffectType eventEffectType;
    14.     public int effectStrength;
    15. }
    3. Create an event card that inherits from base Card SO and adds data that only event cards will have.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using Ludiq;
    4.  
    5. [IncludeInSettings(true)]
    6. [CreateAssetMenu(fileName = "New Event Card", menuName = "New Card/Event")]
    7. public class EventCard : Card
    8. {
    9.     [Header("Event Card Variables")]
    10.     public List<EventEffect> eventList = new List<EventEffect>();
    11. }
    This way you can specialize cards and have them selectively contain data only applicable to that specific type but you can also hold all of these card types which inherit from Card in a List<Card>. As for events - it's just a List<EventEffect> collection.

    The GameBoard logic would first check which of the card types is played (could also be an enum and switch on enum or a series of if statements) and if the card is an event card, then run a for or foreach loop over the List<EventEffect>. Finally, based on EventEffect's eventEffectType enum do something using Switch On Enum.
     
    Last edited: Apr 13, 2021 at 5:46 PM
    Alterrion likes this.
unityunity