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

MoonSharp type-hinting / auto-completion for non-static classes?

Discussion in 'Editor & General Support' started by NickVst, May 7, 2020.

  1. NickVst

    NickVst

    Joined:
    Feb 17, 2017
    Posts:
    25
    Hope this is the proper subforum between this and Scripting (I figured Scripting is solely for C# at the moment, and this doesn't really touch on scripting itself. Moreso on the tools surrounding it).

    I've got MoonSharp integrated in my project. It works completely fine. If I have something like the following script:

    Code (Lua):
    1.  
    2. Process = function(entity__Entity, dt__Single)
    3.     entity__Entity.SetVelocity(dt__Single * 10, 0)
    4.     Debug.Log("Moved entity")
    5. end
    6.  
    then when I call this function from C#, it works without a problem.

    However, I'd like to make the process of scripting as simple as possible. Either I could create a docpage for every method I expose (and risk creating outdated docs due to changes in API design), or I could find some way to generate helpers for the tools others might be using. Since these are updated at compile time, they are theoretically never out of date.

    Currently, I've gone for the first solution, and implemented auto-completion for static members with the help of this article ( https://www.arborinteractive.com/?p=450 ) . What it boils down to, is that I now have a globals.lua file stuffed somewhere in my project, it gets renewed every time Unity compiles with the [DidReloadScripts] attribute, and adds the names of static classes and their public functions to this file if I expose them to MoonSharp. Right now it looks like this.

    Code (Lua):
    1.  
    2. --- My globals.lua file!
    3.  
    4. Debug={};
    5. Debug.Log=function(message__String)end
    6.  

    This works great in Rider + EmmyLua plugin. If I edit a Lua file and type in Deb, it'll autocomplete to Debug, and if I hit period after that, it'll autocomplete to Log().

    EmmyLua assumes I want to call these, but MoonSharp takes care of calling the appropriate C# methods instead. Nice!

    Would it be possible for me to do this for non-static types somehow as well? The article's methods only seem to work for classes you could access globally anyway (so static classes), but ideally I would want to have either some type hinter for Lua so I could tell it "hey, this parameter is an entity, and entity has a method SetVelocity in globals.lua / CSharpLand, so you can autocomplete entity__Entity to that method".