Cross-script function passing

402 views
Skip to first unread message

lengye...@gmail.com

unread,
Dec 16, 2015, 9:07:32 AM12/16/15
to MoonSharp
Hello!

First: I really like MoonSharp!

Second: I hit some kind of wall... I have 2 scripts:

and

The second script calls OverrideScriptGlobal which is a very simple .NET method:

Well, this kind of 'communication' between the independently running scripts work very fine with value types.
I can "override" a script's global variable with any value.

I know the obvious reasons why my solution is not working.
Do you have any idea how to solve this? Any kind of solution would be fine.
Maybe I could convert the "value" (the function itself) in OverrideScriptGlobal to text, and inject somehow to the target script?

Sorry for noob questions, I just started to use MoonSharp a few days ago.

Xanathar

unread,
Jan 8, 2016, 10:10:14 AM1/8/16
to MoonSharp
Hi
you can't share functions or standard tables across scripts (the former because a function simply cannot be shared - apart that the code is private to a script but in theory this could be solved, the _ENV they operate onto is also owned by a script and this could easily become a nightmare to support and code, the latter because tables can contain functions).

There are a couple of ways to share table-like structures among scripts:

1) Use a C# object
2) Use a table built using "null" as a script owner. Note that a table built this way does not allow functions or other tables (unless they are also "null"-owned) to be added.


nat.ad...@gmail.com

unread,
Jan 17, 2016, 3:38:47 PM1/17/16
to MoonSharp
Is that really true? For my project I would ideally like to have multiple files all interacting within one environment so variables could be passed between them as necessary. 

Xanathar

unread,
Jan 20, 2016, 8:27:11 AM1/20/16
to MoonSharp
If they are interacting with one environment, why can't you load all the files in the same Script object ?

Can you explain your scenario a little more in depth ?

Nat Adams

unread,
Jan 20, 2016, 4:55:11 PM1/20/16
to MoonSharp
Can you load multiple files in the same Script object? Documentation is a little vague on the inner workings of it so I'm a  little in the dark

lengye...@gmail.com

unread,
Jan 21, 2016, 9:54:19 AM1/21/16
to MoonSharp
I am doing exactly this, by concatenating all scripts into one script string, and load it into a Script.

Xanathar

unread,
Jan 21, 2016, 4:22:48 PM1/21/16
to MoonSharp
Yes you can do how many DoFile/DoString as you like.. so no need to concat, just call DoFile (or DoString) as many times as you need with different files/snippets of code.

Nat Adams

unread,
Jan 23, 2016, 4:36:10 PM1/23/16
to MoonSharp
Alright, is it possible to create an autorefresh so that my Lua files can be updated while the game is running without recompiling? I've been working on this for a bit, for my HUD so I can adjust it in game, but I can't seem to get any changes

Xanathar

unread,
Jan 23, 2016, 5:01:25 PM1/23/16
to MoonSharp
Not possible, as in theoretically impossible.

If a script changes, you need to recreate a script object (or at the very least, load the code again in the same object, but it's not free from side effects).

Jared Schnelle

unread,
Apr 15, 2016, 5:41:06 PM4/15/16
to MoonSharp
Just thought I'd share a bit of code. This will "sorta" do what you want, but yes, you must keep in mind that it can have side effects if you do a lot of creating / deleting when your script loads.

The code below will not work 100% out of the box, since I am using a library I purchased from the asset store to get back onto the main thread using Task.RunInMainThread. It might work for you if you delete that part, but I can't guarantee.


//Add a private class variable
private FileSystemWatcher _assetFileWatcher;



//Create this method
private void ConfigureHotReload()
   
{
       
var scriptFileName = String.Format("{0}.lua", this.ScriptName);


       
//Set up an asset watcher that will reload our app.lua file
        _assetFileWatcher
= new FileSystemWatcher(Application.streamingAssetsPath);
        _assetFileWatcher
.Changed += OnAssetFileWatcherChanged;
        _assetFileWatcher
.Filter = scriptFileName;
        _assetFileWatcher
.IncludeSubdirectories = false;
        _assetFileWatcher
.EnableRaisingEvents = true;
   
}


//Set up the handler for the file watcher
   
private void OnAssetFileWatcherChanged(object sender, FileSystemEventArgs e)
   
{
       
var scriptFileName = String.Format("{0}.lua", this.ScriptName);


       
if (e.Name.EndsWith(scriptFileName))
           
LoadLuaFile(e.Name);
   
}



//Reload your script
private void LoadLuaFile(string filePath)
   
{
       
if (File.Exists(filePath))
       
{
           
Task.RunInMainThread(() =>
           
{
#if UNITY_EDITOR
               
EditorHelper.ClearConsole();
#endif


                _script
.DoString(File.ReadAllText(filePath));
           
});
       
}
   
}

Jared Schnelle

unread,
Apr 15, 2016, 5:42:27 PM4/15/16
to MoonSharp
Nat, I posted a reply to this request in the thread. I didn't realize your question was unrelated to the topic title, so the code is for hot-refresh of the lua script for you.

Jared Schnelle

unread,
Apr 15, 2016, 5:43:38 PM4/15/16
to MoonSharp
Here's the code to clear the console:

public static class EditorHelper
{
    public static void ClearConsole()
    { 
        // This simply does "LogEntries.Clear()" the long way:
        var logEntries = System.Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll");
        var clearMethod = logEntries.GetMethod("Clear", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
        clearMethod.Invoke(null, null);
    } 
}


Reply all
Reply to author
Forward
0 new messages