I have searched for 2 days and cannot figure this one out. I have a script that I use with NGUI that uses OnPress to simulate a controller on screen. I want to be able to use this script on all buttons that require a held function. Before I was using SendMessage until I found out that it is really slow to call on in a fixed update on a Android.
OLD SCRIPT Here you just entered the function in call, Your release function if needed in release and boom works.
#pragma strict
var target : GameObject;
var call : String;
var release : String;
var go : boolean;
var releaseCall : boolean = true;
function OnPress (isPressed : boolean) {
if(enabled){
go = isPressed;
if(!isPressed releaseCall){
OnRelease();
}
}
}
function OnRelease(){
if(target == null){return;}
target.SendMessage(release, gameObject, SendMessageOptions.DontRequireReceiver);
}
function FixedUpdate(){
if(!go){
return;
}else{
Send();
}
}
function Send(){
if(target == null){return;}
target.SendMessage(call, gameObject, SendMessageOptions.DontRequireReceiver);
}
NEW SCRIPT Here my problem is that callScript is the script on the ship I want to call. But I need different functions for Up, Down, Left, Right, Thrust, ECT. In send I want to call the function Thrust for main thrust and Right to move right but cannot figure out a way to use a variable there.
#pragma strict
var target : GameObject;
var go : boolean;
var releaseCall : boolean = true;
private var callScript : LEMSep;
var sendVariable : Function;
var releasedVariable : Function;
function Start(){
callScript = target.GetComponent(LEMSep);
}
function OnPress (isPressed : boolean) {
if(enabled){
go = isPressed;
if(!isPressed releaseCall){
OnRelease();
}
}
}
function OnRelease(){
if(target == null){return;}
callScript.releasedVariable(); <---Here I need to be able to call a variable Function from script callScript ---->
}
function FixedUpdate(){
if(!go){
return;
}else{
Send();
}
}
function Send(){
if(target == null){return;}
callScript.sendVariable(); <---Here I need to be able to call a variable Function from script callScript ---->
}
A function variable is defined like this for example:
var aFunction : function(int):String;
That matches a function that takes an integer and returns a string, like this:
function Blah (x : int) : String {
So assuming the function Thrust takes a GameObject and returns void, you can do
var someFunction = callScript.Thrust;
// or
var someFunction : function(GameObject):void = callScript.Thrust;
The different functions should all have the same pattern (in this case, having a single GameObject parameter and returning void). You can then use it like this:
Thank you Eric for the reply. However I get how to use a function variable within a script, using this
var someFunction : function(GameObject):void = callScript.Thrust;
What I am looking for and it may not exist is to be able to call a particular function from another script that varies because of what it is attached to. If possible this could be definable from the inspector. Is it possible to define a string and have it call the function.
var someFunction : function = callScript.variableFunction
Where variableFunction is a string, function or something that is inspector definable so I can call Left, Right, Thrust ECT from callScript. Thank You in advance.
No (at least not without hacking around with reflection, which doesn’t work on all platforms). If it has to be in the inspector, you could make an array of functions, and then make an enum for the various array indices. So the variables could be set up like this:
enum Functions {Thrust, Up, Down, Left, Right}
var thisFunction : Functions;
private var functions : function(GameObject)[];
You’d initialize the array in Start (making sure the order of the functions in the array matches the order of the Functions enum):