- Home /
workaround for lack of nested functions in Unity?
I just discovered that nested functions do not work due to Unity's architecture. so is there anyway to achieve essentially the same thing by calling an entire script full of functions within the function in another script? what would that code like in JS? Thanks in advance.
Tell us what a nested function is?
void A() {}
void B() {}
void Both() {
A();
B();
}
"is there anyway to achieve essentially the same thing...?"
We can't answer that without knowing what it is you're trying to achieve. The way nested functions work can vary between languages but the main uses that spring to $$anonymous$$d are 1) to restrict access to the nested function and 2) to make enclosing function data available to the nested function. Other than that, you get the same functionality by just not nesting them. If you think it's important to restrict access I guess the "workaround" would be to use namespaces and/or classes to wrap things, for the other thing you could just pass parameters in.
I don't see the lack of nested functions as a Unity thing in particular. They're not usually a feature of C-style languages.
I think most people just use a Class, where other member functions act as the "nested" functions (and a Unity script is a class, so, I guess the same thing you said.)
I think the idea is, once you have classes, you no longer need nested functions.
I guess I'm being semantic but I thought a nested function was when you define a function, inside another function.
Outside()
{
float outsideLocalVariable;
..stuff..
Inside(int a){ ..do stuff using outsideLocalVariable.. }//declare nested function
..stuff..
Inside(1); //call the nested function
if(..some condition...)
Inside(2); //call the nested function
}
This DOES work in unity (c#, at least). The REASON I sometimes use this is because all the local variables defined in Outside(), are within scope to the Inside() function: this can save a lot of parameter passing. (as bonfireboy's #2 above mentions)
EDIT: this comment is wrong! see correcting comment below.
@Glurth That's the usual meaning of "nested function" and the interpretation I was assu$$anonymous$$g. I get compiler errors (in Unity and $$anonymous$$onoDevelop) when I try that.
Answer by cdrandin · Apr 17, 2015 at 04:08 PM
if they have similar parameter signature then you could go about using delegate or anonymous methods.
void A()
{
Debug.Log("A");
}
void B()
{
Debug.Log("B");
}
delegate void DelegateController();
DelegateController d = ()=>{A (); B ();};
d.Invoke(); // <- this is what executes both functions
Output:
A
B
Answer by NanjaKilo · Apr 17, 2015 at 10:27 PM
So here are the various functions that I want to reduce to one "MechName" function called Shadowcat. that way when I click something to invoke the Shadowcat function in a GUI setting of the loadout screen, all the data for that Mech is called forth from Voodoo Child.
function ShadowcatCenterTorso ()
{
BackstageTonnage.Hardpoint = 1;
BackstageAmmo.Laser = true;
BackstageArmor.Armor = 30;
}
function ShadowcatRightTorso ()
{
BackstageTonnage.Hardpoint = 2;
BackstageAmmo.Missile = true;
BackstageArmor.Armor = 20;
}
function ShadowcatLeftTorso ()
{
BackstageTonnage.Hardpoint = 2;
BackstageAmmo.Missile = true;
BackstageArmor.Armor = 20;
}
function ShadowcatRightArm ()
{
BackstageTonnage.Hardpoint = 2;
BackstageAmmo.Laser = true;
BackstageArmor.Armor = 10;
}
function ShadowcatLeftArm ()
{
BackstageTonnage.Hardpoint = 2;
BackstageAmmo.Ballistic = true;
BackstageArmor.Armor = 10;
}
function ShadowcatRightLeg ()
{
BackstageArmor.Armor = 10;
}
function ShadowcatLeftLeg ()
{
BackstageArmor.Armor = 10;
}
You need to add this to your question, rather than post it as an answer.
Your answer
Follow this Question
Related Questions
Script architecture for tile-based tower-defense game 1 Answer
Unityscript: Accessing functions from other scripts 1 Answer
running code outside of update! 3 Answers
Is it ok to use nested classes, to cache and access components? 1 Answer
Best way to make these tree iterations? Arrays? tags? nested etc 1 Answer