Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question List of Scripts to Execute Issues

Discussion in 'Scripting' started by BlackSabin, Jul 22, 2021.

  1. BlackSabin

    BlackSabin

    Joined:
    Feb 27, 2021
    Posts:
    75
    First time asking for help, figured its about time after struggling on this for long enough.

    The thought process is this; I have scripts that have variables that will each do a single thing. I have an asset object that contains a list of these scripts to execute, and it also houses their respective variables. Theoretically, the C# class system means I don't have to do anything special and I can just use List<Action> for this. I've had some trouble making an inspector class (as I haven't done it before) and it was giving me issues, so I decided to simply go about making my own editor tool to modify and manage them. There have been.... Issues.

    Problem 1: I can't simply grab all assets that derive from class "Action". Not the biggest problem, there are workarounds like using AssetDatabase to search in specific folders and such, but if I could just grab all Actions, that would be awesome.

    Problem 2: I can't make an object field work with just the class "Action". They aren't scriptable objects so they don't exactly have an object reference, but they're still scripts so there should be some way to reference them.

    If I can fix either these problems, I think I'll be able to work through the rest on my own. However, additional help/tips/comments/feedback would be vastly appreciated.
     

    Attached Files:

  2. BlackSabin

    BlackSabin

    Joined:
    Feb 27, 2021
    Posts:
    75
    Should probably make a few things a bit more clear; my main goal is to fix the part of code in the ActionSequenceEditor script so that I can properly have a list of potential scripts to add to the ActionSequence object, and upon doing so edit the various properties that show up within said Actions.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    Pretty sure that's not a thing in any case. Assets derive from UnityEngine.Object, and a System.Action is something else, and C# doesn't do multiple inheritance.

    I think you need it to be clearer because all of this won't work on Android or iOS (which are ahead-of-time compile targets), and if you use IL2CPP anywhere else, that won't work either AFAIK.

    This sorta sounds like an XY Problem. What are you actually trying to do, and perhaps describe it in terms of a construct in an existing commercial game you are trying to implement.
     
  4. BlackSabin

    BlackSabin

    Joined:
    Feb 27, 2021
    Posts:
    75
    The end-product game is probably not going to be mobile-accessible; the primary target is Windows/Linux in terms of operating system.

    In this game, there are going to be a massive amount of interactable objects, and so my concept solution was to instead of writing a bunch of individual scripts detailing every single action these objects would take, I would instead make an object and editor that allowed me to program in my own "higher-level" language, so to speak.

    A player character triggers a pressure plate's collider. The plate then calls upon the ActionSequence to see what is supposed to happen. The first action lists "disable trap", second action lists "start animation", and the third action lists "grant player XP". That is an example of what I want to happen.

    When I open the editor, I want to see the properties of the ActionSequence visible. There'll be a button to "Add Action", and it will add an action to the list, and show me the properties so that I may assign them.

    Does that help?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    That's a pretty big lift to begin all at once. I'd start a bit smaller and get a few things up and running so you can see how it all plays in the Unity Editor, which you definitely want to leverage as much as possible.

    One handy way to categorize code and access it opaquely is to use C# interfaces, which play kinda nice with Unity3D MonoBehaviours.

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.
     
    BlackSabin likes this.
  6. BlackSabin

    BlackSabin

    Joined:
    Feb 27, 2021
    Posts:
    75
    The thought process was along the lines of "big work now, little work later", if I get a good enough backbone for my game going, then making content will be a cinch. I've only started using interfaces a little bit, so if that's your suggestion then I have a few questions regarding that.

    In that first link you posted you showed "GetComponent<IMyInterface>();"...

    var myComponent = GetComponent<IMyInterface>();


    1. What type will myComponent return as?
    2. Would there be a way to return all scripts that have "IMyInterface" inherited?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    That's a great idea when engineering satellite software.

    For interactive game engineering, not so much. It's waaaaaaay more iterative and difficult to plan.

    I'm not saying do not plan, I'm saying plan as much as is reasonable, and it sounds like you would benefit from rolling some more code in Unity that begins to simulate your problem space.

    The variable will be IMyInterface type of course.

    At runtime you iterate the MonoBehaviors and ask "are you a IMyInterface?"

    One day soon FindObjectsOfType<T>() and / or GetComponents<T>() will handle interfaces. Might already actually.

    At edit time you can ransack assets and do the same thing.
     
    BlackSabin likes this.
  8. BlackSabin

    BlackSabin

    Joined:
    Feb 27, 2021
    Posts:
    75
    Thank you so much for your help, I had been stuck on this particular issue for a while now. I've since taken your advice and started using the type MonoScript to grab references for my other objects, as well as now implementing more interfaces to combat these issues. I'll be investigating the "FindObjectsOfType<T>()", but in the event it doesn't handle interfaces, I share your hope that it soon will.