- Home /
Passing a parameter by reference from JavaScript
I'm using a third party library written in c#, which has some functions that go like this:
void SomeFunction(ref string someParameter)
How can I call this function from JavaScript? I'm getting a parameter mismatch if I just put the variable in the caller.
Answer by Eric5h5 · Jul 07, 2011 at 06:41 PM
Sounds like your variable isn't the correct type? Because it works fine just calling a function and using the variable without "ref" in JS. e.g.,
using UnityEngine;
public class Test {
public static void StringTest (ref string test) {
test = "abc";
}
}
And then with JS:
var test = "123";
Test.StringTest (test);
Debug.Log (test);
I'm pretty sure the type was okay. The error went something like "Cannot convert from SomeClass to ref SomeClass". Anyways, we solved it by moving the caller to c#, so I can't test this answer, but I don't want to get flamed for not marking answers, so I'll mark this as the correct one.
Answer by GuyTidhar · Jul 07, 2011 at 06:14 PM
You can do that directly but you may use some wrapper to achieve this:
// A C# Wrapper which you can name "StringHandler.cs"
public class StringHandler
{
public void WrapperFunction(string[] str)
{
// If you string need to be empty DO:
string localStr = "";
// Or if your string should have some data DO:
string localStr = str[0];
// Now call the third party stuff
ThirdPartyStuff.SomeFunction(ref localStr);
str[0] = localStr;
}
}
Now, in your javascript do stuff like the following:
var stringHandler : StringHandler;
function Start()
{
var myStr : String[] = [ "YouBaseTextIfNeeded" ];
Debug.Log("1) myStr = " + myStr[0]);
stringHandler = new StringHandler();
stringHandler.WrapperFunction(myStr);
Debug.Log("2) myStr = " + myStr[0]);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Pairing and GUITexture with a Timer? 1 Answer
How do i fix "Script does not exist" error when trying to add my script to something 3 Answers
About "translating" js into C# 1 Answer