[ad_1]
I’m programming a Jobs Queue in Unity, in order that I can ship Tasks/Jobs to a central queue the place the duties might be processed.
First, I outlined 3 doable job varieties inside a Scriptable Object.
public enum jobsType {justPlay, playAndWait, justWait}
Then I outlined a category inside the identical Scriptable Object, so that every Jobs might have 3 inputs: a SportObject, a jobs Type, and a float. Here’s 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 SportObject thisGameObject;
[SerializeField]
public float jobsNumber;
}
}
Then in my code, I take advantage of a Switch to route the roles processing based mostly on the roles Type enum. This code is working nice – up to now, so good!
That stated, I do not need to have to change this JobsData Scriptable Object each time I need to add a brand new jobs Type. I’m making an attempt to honor the Open-Closed Principle: “software program entities (courses, modules, strategies, and many others.) ought to be open for extension, however closed for modification.”
It could be supreme if I might outline jobsType to be a separate Scriptable Object… after which use a Switch to route between numerous jobsTypes.
At first, I used to be contemplating doing the next:
- Create Scriptable Object property utilizing JobsData, the place every asset represents a Jobs Type
- Create a List of those Scriptable Object property
- Use that List to dynamically generate an Enum (unsure if that is doable)
- Then use that Enum to energy a Switch. (additionally do not see how that is doable).
But as I assumed it via, I noticed that this may not be the most effective path ahead… since as I perceive it, Enums have to be specified at compile time. Beyond that, I am unable to determine how I might even use any dynamically-generated Enums in my Switch.
Right now my code works as a result of I outline my Enums within the class, however unsure tips on how to obtain the identical factor with a dynamic Enum (if these are even doable).
I’d love to have the ability to outline my JobsTypes as Scriptable Object property, after which route jobs based mostly on these property. Given that:
- Is there an alternative choice to a Switch I might use to route JobsTypes to the suitable processing code?
- Or alternatively, is there a manner I might use dynamically-generated Enums in a Switch?
Or perhaps I’m enthusiastic about this all incorrect? Open to any and all concepts!
[ad_2]