Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Accessing information from and instancing subclasses procedurally

Discussion in 'Scripting' started by lossenc, Dec 30, 2015.

  1. lossenc

    lossenc

    Joined:
    Jan 23, 2014
    Posts:
    1
    Howdy, all!

    So, I've been working on a character builder for a pencil-and-paper RPG in C# recently, but have come across a conundrum. The main goal I've been trying to reach seemed pretty simple at the time. The idea was to write up a series of classes that outlined the "rules", make these accesible to the UI elements, and have a central CharacterInformation class to store the chosen information. The reason that this all got a bit more complicated was because I wanted the ability to add new species/career/specialization options in future updates, that could be optionally loaded or hidden. My first thought was to store the species, careers, and specs in XML files that could be loaded/unloaded on the fly. That idea became problematic for me once I started adding species and career-specific game logic. I then thought I might get away with storing them in separate namespaces. (ReleaseA.Species.Human, etc) However, as I went on in my coding, I soon realized the problem may very well be in how I'm accessing my character class. I have dropdown menus on the main Character Builder screens that are assigning instances of specific species, careers, and specializations, but all of these are pre-built into the UI. If I want to be able to safely add more to the class structure, the interface needs the ability to "know" what it has access to. I'm racking my brain for what I'm missing, but I've been doing this too long not to know when to ask for another set of eyes.

    Any thoughts?

    Maybe appending a static list of subclass instances by each subclass in its constructor? *shudder* That seems soo wrong just thinking about it. Apologies if that is very poor logic, but my brain is fried and I want to learn!

    Here's a basic outline of how my class/subclass structure is working:
    • BaseCharacterClass
      • BaseSpeciesClass
        • SpeciesClassA
        • SpeciesClassB
        • SpeciesClassC
      • BaseCareerClass
        • CareerClassA
        • CareerClassB
        • CareerClassC
      • BaseSpecializationClass
        • SpecClassA
        • SpecClassB
        • SpecClassC
    • CharacterInformationClass
      • Single GameObject retained across all level loads
      • Saves/stores all chosen character information in static variables.
      • All information will be translated to another class when the user calls to save to a file.
    • UI Scripts
      • I want the UI to dynamically load available subclass and their data to populate dropdowns, etc, for the user to choose on character creation.
      • All of this works fine, now, but I'm specifically calling, "If A is chosen, instance SpeciesClassA". I want this to be procedural, so I can add/remove classes in updates.
     
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,

    Maybe you can :
    • Create a BaseClass of Species/Career/Spec with a public or protected ClassType field. (Classic way is to use an enum)
    • Build 3 Lists (like tables in a database) : Species/Career/Spec
    • You can use Linq to create request on the list, like GetAllSpeciesOfType (ClassType type)
    • You should be able to do "When X is chosen, instance Species(X).
    Depending on your needs, you can also use Interfaces, something like :
    Code (csharp):
    1.  
    2.   public class BaseCharacterClass
    3.   {
    4.        public ISpecies Career{get;set;}
    5.        public ICareer Career{get;set;}
    6.        public ISpec Spec{get;set;}    
    7.   }
    8.  
    And also maybe generic, even if that example don't seems really logical i guess
    Code (csharp):
    1.  
    2.   public class BaseSpeciesClass<T> where T : BaseCharacterClass
    3.