Thursday, November 10, 2022
HomeGame Developmentscriptable object - Finest approach to dynamically enumerate an inventory of varieties,...

scriptable object – Finest approach to dynamically enumerate an inventory of varieties, after which route duties primarily based on these varieties?


I’m programming a Jobs Queue in Unity, in order that I can ship Duties/Jobs to a central queue the place the duties shall be processed.

First, I outlined 3 attainable job varieties within a Scriptable Object.

public enum jobsType {justPlay, playAndWait, justWait}

Then I outlined a category within the identical Scriptable Object, so that every Jobs may have 3 inputs: a GameObject, a jobs Sort, and a float. Here is the entire Scriptable Object.

utilizing System;
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.Audio;
utilizing Unity.VisualScripting;

public enum jobsType {justPlay, playAndWait, justWait}

[CreateAssetMenu(fileName = "New JobsData", menuName = "Jobs Data", order = 51)]

public class JobsData : ScriptableObject {

    [SerializeField]
    public JobsItem thisJobsItem;

    [IncludeInSettings(true)]
    [System.Serializable]
    public class JobsItem {

    [SerializeField]
    public jobsType typeOfJob;

    [SerializeField]
    public GameObject thisGameObject;

    [SerializeField]
    public float jobsNumber;
}
}

Then in my code, I exploit a Change to route the roles processing primarily based on the roles Sort enum. This code is working nice – to date, so good!

That mentioned, I do not need to have to change this JobsData Scriptable Object each time I need to add a brand new jobs Sort. I am attempting to honor the Open-Closed Precept: “software program entities (courses, modules, strategies, and many others.) needs to be open for extension, however closed for modification.”

It could be preferrred if I may outline jobsType to be a separate Scriptable Object… after which use a Change to route between varied jobsTypes.

At first, I used to be contemplating doing the next:

  1. Create Scriptable Object property utilizing JobsData, the place every asset represents a Jobs Sort
  2. Create a Listing of those Scriptable Object property
  3. Use that Listing to dynamically generate an Enum (unsure if that is attainable)
  4. Then use that Enum to energy a Change. (additionally do not see how that is attainable).

However as I assumed it via, I noticed that this may not be the perfect path ahead… since as I perceive it, Enums should be specified at compile time. Past that, I am unable to determine how I might even use any dynamically-generated Enums in my Change.

Proper now my code works as a result of I outline my Enums within the class, however unsure easy methods to obtain the identical factor with a dynamic Enum (if these are even attainable).

I would love to have the ability to outline my JobsTypes as Scriptable Object property, after which route jobs primarily based on these property. Provided that:

  1. Is there a substitute for a Change I may use to route JobsTypes to the suitable processing code?
  2. Or alternatively, is there a approach I may use dynamically-generated Enums in a Change?

Or possibly I am occupied with this all mistaken? Open to any and all concepts!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments