- Home /
Get list of all "Action" classes
I'm trying to make a quest system, however I'm having a little trouble. I have created two interfaces, IQuestAction, and IQuestObjective. I'm trying to write a customer editor to add actions/objectives to a quest however I am having trouble with it.
I have a sample action called KillNPC which implements IQuestAction, it is contained as a script/class within my quest namespace. I've tried using the following code
questActions = Resources.LoadAll<IQuestAction>("WorldActions");
This should find all IQuestAction scripts within the WorldAction folder in the Resources folder, however it returns an error saying that IQuestAction cannot be converted to UnityEngine.Object, as it is not a ScriptableObject, and it is not an asset.
I'm wondering how I would be able to find all IQuestAction classes and view them in a drop down window to add an action to a quest
This is very similar to how Behavior Trees work like Behavior Bricks would work as I imagine, as in Behavior Bricks you extend their action class and add [Action("Type of Action/ Action")] above the class. I believe the [Action("...")] line is what actually adds it to the list however I am unsure how it works as Action is a class called ActionAttribute that extends attribute and only has a constructor with one string parameter called name, and a bool return function called isPresentIn which takes a type t param, and a bool param (I believe this code is within a DLL file and most of it is unreadable, according to Visual Studio).
AFAI$$anonymous$$, Unity is not able to serialize abstract classes and interfaces, which explains why you can't retrieve your objects with a class implementing your interface. You may have to "rethink" the inheritance tree of your objects if you want to use Resources.LoadAll
Well I am unsure if Resources.LoadAll is even necessary, that's just where my $$anonymous$$d went to initially. BehaviourBricks does something like this https://imgur.com/a/pjPO4 I don't need the fancy editor graphics, all I need to know is how it finds [Action("...")] on all actions and puts them in a List (List<>/Array to display from). I tried googling more and I've found nothing other than this Attribute.GetCustomAttributes() which I have gained more confusion from.
Answer by NeonTheCoder · Oct 06, 2017 at 10:50 AM
Alright I was able to find out how to find all classes with an attribute, but it's kinda sloppy as I don't know if it would work as a package?
//QuestEditor can be any class as long as it's something you have written.
foreach (Type type in typeof(QuestEditor).Assembly.GetTypes()) {
if (type.GetCustomAttributes(typeof(WorldActionAttribute), true).Length > 0) {
//Code goes here
}
}
If anyone knows of a more cleaner way to do this, and or how to store the type let me know.
Your answer
Follow this Question
Related Questions
Confused about custom GameObjects,Custom GameObject confusion 0 Answers
objects in array/list are cast back to base class on editor activity 1 Answer
Stop a MonoBehavior from being addable as a script? 0 Answers
How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer