- Home /
Expanding functions
Alright, this is going to be a very nooby question. I've made a search with google and unity answers but, there is a fundamental terminology that I am missing to make my search successful.
I remember back in highschool learning VB and there being a way to add user-pre-written functions into the code simply by setting them up ahead of time, and including #myextraFunctions at the top of the code.
At the time I used it to include a search and replace command for string data to prove I understood the concept to my teacher.
Here I am more than a decade later learning Javascript in conjunction with unity and have been racking my brain to remember what that was called so I can find a JS~Monodevelop equivalent. As I understand unity I could use getcomponent to simulate this but its heavy on resources.
Would someone be willing to enlighten me once more, and send my a link to a tutorial on how to accomplish this with JS in Monodevelop?
Answer by Eric5h5 · Aug 22, 2013 at 04:39 PM
Perhaps you're thinking of static functions:
class Stuff {
static function Add (a : int, b : int) : int {
return a + b;
}
}
Which you would use like:
function Start () {
var x = Stuff.Add(1, 2);
}
The script with the "Stuff" class doesn't have to be attached to anything, since it's a static function, basically meaning that only one instance can exist.
So you are saying that the first block of code doesn't have to exist in the same script as the second block of code?
If so, how does other scripts find the static function?
Also, maybe something to do with Import, I had to use:
import UnityEngine;
import System.Collections;
import System.Collections.Generic;
import System.Text;
import System.Xml;
import System.IO;
in order to get some X$$anonymous$$L stuff working... maybe I need to make a import file with all my extras...
how does other scripts find the static function?
The scripts find the static function because it's the only one that exists. You identify it with the class name as I showed in the Start code.
maybe I need to make a import file with all my extras...
No. The "import" keyword is slightly misleading, since it doesn't "load" or "import" anything, aside from the namespace. You did not have to use it to get the X$$anonymous$$L stuff working, it just makes things more convenient. For example, ins$$anonymous$$d of doing things the long way:
var foo = new System.Collections.Generic.List.< int >();
You can import the System.Collections.Generic namespace and do
var foo = new List.< int >();
While this is nice, it can occasionally result in ambiguity, since the same class may exist in more than one namespace, such as Random existing in UnityEngine.Random and System.Random. So if you do "import System;" and try to use code like "var x = Random.value;" it won't work since the compiler doesn't know which Random you're talking about. If you import the System namespace, you'd have to disambiguate by saying "var x = UnityEngine.Random.value;".
I will have to try out the static function before I can definitely Check you as the correct answer but, everything you've said so far is simple, easy to understand, and brilliant. Up-votes inco$$anonymous$$g!
$$anonymous$$eep you posted, probably tomorrow.
This is exactly what I needed. Thanks once again! Self $$anonymous$$ching this stuff is fun but, I miss very important things along the way. Regardless...
You rock!
Your answer
Follow this Question
Related Questions
Source code for library function "RecalculateNormals()"? 4 Answers
Array Problem - Error Code BCE0022 1 Answer
Help with function code, passing Vector3 info 1 Answer
Help with a simple function. 1 Answer
function error 1 Answer