0

Here is the code I am using: public enum TargetingOptions { NoTarget, AllCreatures, EnemyCreatures, YourCreatures, AllCharacters, EnemyCharacters, YourCharacters }

    public enum Faction
    {
    Fog,
    Imperial,
    Monster,
    Pirate
 }



public class CardAsset : ScriptableObject
{
    public CharacterAsset characterAsset; 
    [TextArea(2, 3)]
    public string Description;  
    public Sprite CardImage;
    public int ManaCost;

    [Header("Faction")]
    public Faction faction;

    [Header("Creature Info")]
    public int MaxHealth;  
    public int Attack;
    public int AttacksForOneTurn = 1;
    public bool Charge;
    public string CreatureScriptName;
    public int specialCreatureAmount;

    [Header("SpellInfo")]
    public string SpellScriptName;
    public int specialSpellAmount;
    public TargetingOptions Targets;


}

what I am trying to do is create a different "hand" of cards depending on faction. to do this I have created a separate "hand" script for each of the factions, but I want to create a "handmultiple" script that will choose which "hand faction" script to head to. So if in unity "pirate" is chosen, then all that will appear would be the associated scripts (ignoring fog, imperial and pirate scripts) Does that make sense? thank you!

    enter code here     using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;

public class HandVisualImperial : MonoBehaviour
{
    public AreaPosition owner;
    public bool TakeCardsOpenly = true;
    public SameDistanceChildren slots;

     [Header("Transform References")]
    public Transform DrawPreviewSpot;
    public Transform DeckTransform;
    public Transform OtherCardDrawSourceTransform;
    public Transform PlayPreviewSpot;
    private List<GameObject> CardsInHand = new List<GameObject>();
    public void AddCard(GameObject card)
    {
        CardsInHand.Insert(0, card);
          card.transform.SetParent(slots.transform);
        PlaceCardsOnNewSlots();
        UpdatePlacementOfSlots();
    }
    public void RemoveCard(GameObject card)
    {
        CardsInHand.Remove(card);
        PlaceCardsOnNewSlots();
        UpdatePlacementOfSlots();
    }
The Saint
  • 1
  • 3
  • 1
    Possible duplicate of [C# how to use enum with switch](https://stackoverflow.com/questions/15136134/c-sharp-how-to-use-enum-with-switch) – Samvel Petrosov Jul 02 '17 at 16:08
  • So what is the problem exactly? Please provide the code where you're using the `enum` (or intend to use it) so we can help you. Your question as it stands now looks like a generic "how to use enum?" question which can be answered in a million way. – Racil Hilan Jul 02 '17 at 16:33
  • I edited the question. Hopefully it makes more sense now? – The Saint Jul 02 '17 at 18:28
  • Not really. That's just a list of class fields. I don't see where you want use the `enum` there. Where is the code that uses the `faction` field? – Racil Hilan Jul 02 '17 at 18:38
  • That is the issue. What I need is to be able to use the faction field to create a whole script just to differentiate between the scripts that I want to attach to the differing factions. There is a huge amount of script involved, I just want to see if there is a way to set a single path that will attach to all of the Fog, scripts, or imperial scripts, etc – The Saint Jul 02 '17 at 18:41
  • I will try to make it make sense. Always easier in my head. LOL. So In unity I choose Fog. As soon as I do that, the script no longer sees any scripts that are dedicated to the other three and everything from there on will only be Fog based. Etc for the other three – The Saint Jul 02 '17 at 18:45
  • You don't need to post all your code, just a sample so we see how you're intended to use the `enum`. Yes of course there is a way to set a single path, but that depends on your code. For example, you can use `if(myCardAsset.faction == Faction.Fog) {...}` or you can use `switch(myCardAsset.faction) { case Faction.Fog: ... }`. There are other possible ways, so show us a sample of your code and we can help you. – Racil Hilan Jul 02 '17 at 19:42
  • I added some sample code.. I hope it is what you need? – The Saint Jul 02 '17 at 20:07

3 Answers3

0

So I think I have it figured out: Have a whole bunch more work to do before testing it unfortunately.. but I believe I have it done now... Thank you all for the help!!!

public class HandVisualMultiple : MonoBehaviour
{
    Faction factiontype;
    public HandVisualFog fog;
    public HandVisualImperial imperial;
    public HandVisualMonster monster;
    public HandVisualPirate pirate;



    void Start()
    {
        fog = GetComponent<HandVisualFog>();
        imperial = GetComponent<HandVisualImperial>();
        monster = GetComponent<HandVisualMonster>();
        pirate = GetComponent<HandVisualPirate>();

        if (factiontype == Faction.Fog)
        {
            fog.enabled = true;
            imperial.enabled = false;
            monster.enabled = false;
            pirate.enabled = false;
        }
        else if (factiontype == Faction.Imperial)
        {
            fog.enabled = false;
            imperial.enabled = true;
            monster.enabled = false;
            pirate.enabled = false;
        }
        else if (factiontype == Faction.Monster)
        {
            fog.enabled = false;
            imperial.enabled = false;
            monster.enabled = true;
            pirate.enabled = false;
        }
        else if (factiontype == Faction.Pirate)
        {
            fog.enabled = false;
            imperial.enabled = false;
            monster.enabled = false;
            pirate.enabled = true;
        }
    }
}
The Saint
  • 1
  • 3
0

It's not the answer for your question, but an experienced game developer advice.

Try to use Polimorfism. A single main class. "class Creature" and then a lot of child classes class ImperialCreature:Creature, class PirateCreature:Creature and then set the parameters to each of them.

And later you can set another sub classes. for instance: class JackSparrow:PirateCreature ... and on...

Daniel Santos
  • 14,328
  • 21
  • 91
  • 174
-1

The enum is similar to an array, so if you do (int)Faction.Fog it will return 0 since it's the first item. You could have an array or list with your scripts in the same order as your enum and call them by casting the enum to an int

danivdwerf
  • 234
  • 2
  • 16