Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global object in javascrript #1734

Open
Avatarchik opened this issue May 25, 2024 · 7 comments
Open

global object in javascrript #1734

Avatarchik opened this issue May 25, 2024 · 7 comments

Comments

@Avatarchik
Copy link

detail | 详细描述

How can I specify a global object from C# so that it is available in JavaScript?
MoonSharp example
_scriptEngine.Globals["Engine"] = this;

@chexiongsheng
Copy link
Collaborator

There is no direct support, you can achieve this by calling js functions from c#.
ps: It is not recommended to change global, whether from C# or js.

@Avatarchik
Copy link
Author

Avatarchik commented May 27, 2024

Can you show an example for Unity? How can I do this or how can I pass an instance object to javascript by reference
How can I do or do I want to make an API for modding the game?

@chexiongsheng
Copy link
Collaborator

chexiongsheng commented May 28, 2024

js function

function setGlobal( key, value) {
    globalThis[key] = value;
}

Assign setGlobal to C# as a Action<string, object> variable, then call this delegate variable in C#.

@Avatarchik
Copy link
Author

Hi! Thank you!

   private Action<string, object> setGlobal;
   
   jsEnv.Eval(@"
            global.setGlobal = function(key, value) {
                global[key] = value;
            }
               ");
               
                setGlobal = jsEnv.Eval<Action<string, object>>("setGlobal");
                
                setGlobal("Engine",this);
                
                jsEnv.Eval(@"
                  Engine.AddDATFile(""res:/main.dat"")
                ");
                

When calling a function it throws an error
jsEnv.Eval(@"
Engine.AddDATFile(""res:/main.dat"")
");

System.Exception: 👎 TypeError: not a function

@chexiongsheng
Copy link
Collaborator

AddDATFile is non public ?
It is ok in my project.

@Avatarchik
Copy link
Author

yes
I'm using a dotnet project, a console application
PUERTS_GENERAL;

public void AddDatFile(string url, bool isCache = false)
{
}

Can you post your example please?

@chexiongsheng
Copy link
Collaborator

Add testcase to https://github.com/Tencent/puerts/blob/master/unity/test/Src/Cases/CrossLang/CrossLangTest.cs

        private string _url;
        
        public void AddDatFile(string url, bool isCache = false)
        {
            _url = url;
        }
        
        [Test]
        public void SetToGlobal()
        {
            var jsEnv = UnitTestEnv.GetEnv();

            jsEnv.Eval(@"
              global.setGlobal = function(key, value) {
                  global[key] = value;
              }
            ");

            var setGlobal = jsEnv.Eval<Action<string, object>>("setGlobal");

            setGlobal("Engine", this);

            jsEnv.Eval(@"
                  Engine.AddDatFile(""res:/main.dat"")
                ");
            Assert.AreEqual("res:/main.dat", _url);
        }

do test

cd puerts\unity\test\dotnet
node ../../cli dotnet-test v8_9.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants