- Home /
Static Function Aliases?
So I think you all know how "transform" is actually "UnityEngine.Transform", and "renderer" was changed to "GetComponent.()" or "GetComponent()".
How can I do this with my static functions? if I have a script named Func with a static function named "MyStaticFunc" then how could I make it where instead of doing "Func.MyStaticFunc(variables in here)" I could just do "myFuncAlias(variables in here)" or something similar.
Thanks in advance! And no need to rush as this isn't an important issue, but more of just a question. :)
This isn't a direct answer to using static aliases, but there might be a misconception here. "transform" will return the UnityEngine.Transform that is attached to the game object. It's a property that calls GetComponent() in the getter.
"renderer" used to do the same thing for mesh renderers, but is now obsolete, I believe.
However you can write non-static properties that do something similar and cache the results in a private field. For example, all of my base behaviours have this property:
private Transform _transform;
public Transform Transform
{
get
{
if (_transform == null) _transform = GetComponent<Transform>();
return _transform;
}
}
Then I can say myObject.Transform and it will only call get component if the component has not yet been found, and it will never return null. This is with c# properties. If you're using Javascript, I'd probably just cache them in awake ins$$anonymous$$d.
Any idea how to convert this to UnityScript? I put C# in the tags because I can translate most C#, though this is new to me but this is EXACTLY what I was looking for.
Thanks for the reply! :)
in unityscript you can declare explicit set and get functions if you inherit monobehaviour, take a look at this post http://answers.unity3d.com/questions/51910/can-i-declare-properties-in-js.html
$$anonymous$$y email says that ninja_gear posted a comment saying "
So, what you are saying is you want to reference a GameObject variable and access a $$anonymous$$onoBehavior component attached to that GameObject, and you wish to do so without a GetComponent() call? Similar to the way:
Transform myTransform = transform;
retrieves that GameObjects Transform component?
" but I can not find it, so this is my reply to it.
Yes that is EXACTLY what I was looking to do, so if that's possible then that would be awesome!
This is an example of the methodology I use to remove all but one GetComponent() call for each component in each Class: (not sure about the Unityscript, I haven't used it in awhile).
class myComponent : $$anonymous$$onoBehavior
{
var xform : Transform;
var myScript : myCustomClass;
function Awake()
{
xform = transform;
myScript = GetComponent.(myCustomClass);
}
}
References are basically just routing commands, they take up zero resources. The instantiated object is what eats CPU/GPU/RA$$anonymous$$. Since the GameObject is there and running, and the Components are already active and running on it, its best to get a reference to any Components the script is using when those GameObject's are created, that way you don't ask Unity to de-reference the Component during runtime. Then throughout the script, just use the reference.
Would this work to where I can put that in one static class and then from then on use "gameObject.item" ins$$anonymous$$d of "gameObject.GetComponent(ItemPickUp)"?
It seems like it shouldn't be hard to do as "GetComponent(Transform)" automatically works as "transform".
Answer by DiegoSLTS · May 27, 2016 at 05:53 PM
Apart from the misconception that other people explained, what you want to do can't be done in Unity right now. There's a new feature in C# 6 that's exactly for that, you write "using static " and the class that has the static methods: http://intellitect.com/static-using-statement-in-c-6-0/
I explained the question poorly, I want to be able to do "someGameObject.structure" ins$$anonymous$$d of "someGameObject.GetComponent(StructureCore)" and "someGameObject.item" ins$$anonymous$$d of "someGameObject.GetComponent(ItemPickUp)".
So this isn't possible? Well if so then that sucks but are there any workarounds?
Oh! $$anonymous$$ake a member of your monobehavior called "structure". In Start() call.. structure=GetComponent<StructureCore>();
Done (just don't forget to check for null when you used it). If it might change, take a look at "accessors" (get/set stuff).
I know, but they are called thousand of times using different objects. But I'd like to be able to use it just like "someGO.transform.position" so I could do "someGO.structure.structureHP".
I know about creating variables but these are player placed objects, it's not too big of a deal but it would save me a good bit of time ins$$anonymous$$d of putting GetComponent(StructureCore).
Your question was really confusing, the first part where you talk about transform and renderer is done, like everyone said, using properties and it works with both C# and UnityScript: http://unity3d.com/es/learn/tutorials/topics/scripting/properties
You can do that for sure.
The second part where you talked about static functions is what you can't do, you can't access static functions without specifing the class (unless you're calling a static function in the same class it's defined). Note that you're talking about REALLY different things here.
I know that you cannot access static features without the class name, I use static features a lot.
But I was wondering how Unity did it, but I guess it's just hard-coded.
Yes, UnityEngine.Debug and $$anonymous$$athf are great candidates for that new feature.
Answer by Glurth · May 27, 2016 at 04:50 PM
"UnityEngine.Transform" Refers to the namespace, dot the class. If you put using UnityEngine
at the top of your file, you wont need to always specify the namespace and can call the class simply "Transform".
The only place you will ever be able to call a static member of your class, WITHOUT specifying what class you are talking about, is INSIDE the class in question. e.g.
Class Func
{
void Func1()
{ StaticFunc();// since I'm "inside" the Func class, it assumes I'm talking about a member.
}
static void StaticFunc()
{... }
}
Class AnotherClass
{
static void StaticFunc(){...}// since this class & function MIGHT exist, we NEED to specify the class when calling StaticFunc() from outside of these classes
}
I know that you do not have to put UnityEngine but I was just saying that it's derived from UnityEngine.Transform.
And you don't have to put UnityEngine while in a class as long as you do this:
class $$anonymous$$yClassName extends $$anonymous$$onoBehaviour{
}
Also, thank you for the reply!
Answer by ninja_gear · May 28, 2016 at 07:05 PM
If you want to extend MonoBehavior to include your own custom methods, such as an function that always attempts to fetch said Component, you can, either directly or through inheritance.
class MyNewGameObject extends MonoBehaviour
{
var myCustomClass : CustomClass;
function Awake()
{
myCustomClass = gameObject.GetComponent(CustomClass);
}
function GetMyCustomClass()
{
return myCustomClass;
}
}
You should be able to call this thru .myCustomClass, or .GetMyCustomClass();
*Note: the function is not static as you will be calling it on an instance of the MyNewGameObject.
Answer by gorsefan · Jun 09, 2016 at 12:14 PM
You cannot alias functions in C#, the closest you can get is wrapping them in something else or maybe using delegates.
You can alias classes/namespaces/types with the "using" directive.
Also, if you do this kind of thing you end up with code only you will understand. And the you in 6 months time probably won't understand it either ;)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GetComponentInChildren not working in a function, help please? 2 Answers
OnTriggerEnter function in c# 0 Answers
Distribute terrain in zones 3 Answers
Unity2D moving on the y-axis 0 Answers