- Home /
Pass a function(float) as variable
So I have this simple function:
function FadeIn(speed : float){
fadeSpeed = speed;
fadeDir = -1;
}
and I want to add it to a class by doing something like this:
//The class type
class ScriptStep {
var subScript : List.<ScriptStep>;
var stepHoldLength : float;
var stepTransform : Transform;
var audioClip : AudioClip;
var textLine : String;
var stepFade : function(float);
//And the function
function AddStep (currentScript : List.<ScriptStep>, stepFade : function(float)){
var step = new ScriptStep ();
step.stepFade = stepFade;
currentScript.Add(step);
}
But whenever I try to actually run it:
AddStep (gameIntro, dialogInteractions.FadeIn(0));
I keep getting the error:
No appropriate version of 'Dialog_Manager.AddStep' for the argument list '(System.Collections.Generic.List., void)' was found.
What am I doing wrong exactly? A bit new to javascript and just started to learn how to declare variables of the type function...
Any help very appreciated!
Answer by Eric5h5 · Jul 07, 2013 at 01:59 AM
Your code should be:
AddStep (gameIntro, dialogInteractions.FadeIn);
You can't pass in an argument when you're adding a function as a variable. You can only use an argument when you actually call the function, such as
step.stepFade(0);
By the way, if you want to be 100% explicit then you can do
var stepFade : function(float):void;
in order to specify the return value of the function. It's void by default so it works without it, but anyway.
BTW, what is the right way to declare the variable for the function?
var action : Function;
var action : function ();
Ok...it still doesnt seem to work... this is what I have now:
var stepActionFloat : function(float):void;
function AddStep (currentScript : List.<ScriptStep>, stepAction : function (float) : void, duration : float){
var step = new ScriptStep ();
step.stepActionFloat = stepAction(duration);
currentScript.Add(step);
}
and I get a
BCE0022: Cannot convert 'void' to 'function(): void'.
Is it because I'm adding it as a variable again? I think I understand...Only when I call it! Super clear answer.
Answer by umangindianic · Jul 05, 2013 at 04:46 AM
Use of file in top of the script.
using System.Collection.Generic;
this is use for the List which is used by you in your script.
List can not add any type of whole function but it can add the value of function. So, make your function with its return type.
You mean import System.Collections.Generic;
right?
I'm not sure I understand what you mean by "List can not add any type of whole function but it can add the value of function." The function that I want to use (FadeIn) doesn't return anything, it just does a couple of steps, what should I ask for it to return?
I noticed that I can make it work if the function doesnt pass a value. For example if I have this function :
function UnPauseGame (){
worldisUnpausedTime = Time.time;
worldIsPaused = false;
}
I would also get an error, but if I add a "yield to the end of it:
function UnPauseGame (){
worldisUnpausedTime = Time.time;
worldIsPaused = false;
yield;
}
I can now do this: AddStep (knowsAboutJournal01, worldInteractions.UnPauseGame);
I noticed the function ends up changing from being a function () : void
to function () : IEnumerator
but I don't understand what that means and how it makes it acceptable to now be used.