- Home /
how do I create a static Instance in javascript
How do I create an 'Instance' in JavaScript for my other scripts to reference ?
C# example :
using UnityEngine;
using System.Collections;
public class TP_Controller : MonoBehaviour
{
public static TP_Controller Instance;
void Awake() {
Instance = this;
}
}
My js workaround for where the C# script creates an 'Instance' to read and write vars from , I found the other scripts and stored them in local vars.:
public var TP_Motor : Script_TP_Motor;
function Awake() {
TP_Motor = GetComponent("Script_TP_Motor") as Script_TP_Motor;
}
Any help or info would be greatly appreciated.
This is the same as my other question , but it was answered with (helpful) information not specific to my question. http://answers.unity3d.com/questions/235181/help-with-creating-a-class-in-javascript.html
I shall remove the duplicate when I hopefully have an answer to help clean up the system. Thanks.
In my example I am trying to show :
C# creates an Instance of TP_Controller which TP_$$anonymous$$otor can read from.
$$anonymous$$y method loads TP_$$anonymous$$otor into a var which through that var TP_Controller commands direct statements to TP_$$anonymous$$otor .
This works , but what if I want another script to read from TP_Controller ?
Answer by by0log1c · Apr 04, 2012 at 06:23 PM
Just to clarify, static class and instances are opposite concepts. From the look of things, I'd say you're interested in the Singleton pattern.
EDIT: Well you could either have a static class or a regular class with static variables. Here's a quick example:
//MyScript.js
public class MyScript extends MonoBehaviour
{
public static var myName:String = "By0log1c";
public var myLocation:String = "Quebec, Canada";
}
//MyStaticScript.js
//static class must extend from object (just leave it blank)
public static class MyStaticScript
{
//myAge is static since its within MyStaticScript
public var myAge:int = 23;
}
//SomeScript.js
public class SomeScript extends MonoBehaviour
{
function Start():void
{
Debug.Log(MyScript.myName); //MyScript.myName is static
Debug.Log(MyStaticScript.myName); //MyStaticScript is static
Debug.Log(MyScript.myLocation); //nothing static here = error!
}
}
Frankly, the singleton pattern is what really taught me what static is. Here's a JS example:
//let's combine the best of both world!
public class MySingleton extends MonoBehaviour
{
private static var instance:MySingleton;
public static function Instance():MySingleton
{
return instance;
}
var nonStaticVariable:boolean = false;
function Awake():void
{
instance = this;
}
}
//SomeScript.js
public class SomeScript extends MonoBehaviour
{
function Start():Void
{
//we access MySingleton through its static Instance
//function and use a MonoBehaviour like it was static!
Debug.Log(MySingleton.Instance().nonStaticVariable);
}
}
That's a lot of reading but trust me, you'll love singleton.
I am having trouble expressing the problem. I want to write in JavaScript (not knowing any C# and limited js, looking at your Singleton link it's in C#?).
The only way I can make an analogy is with my previous experience in actionscript2.
1/ in frame 1 I have 'health = 10;'
2/ in frame 2 I have an empty $$anonymous$$ovieClip with the action '_root.health -= 5;'
health is just a var that is created and exists somewhere special. And anytime I want to read or write to health, I call it with _root.
So in js, I want to create an 'Instance' my _root. master read/write/store data place. So when I want to have multiple scripts access data, they can call scriptInstance.foo ins$$anonymous$$d of me loading all the other scripts in each script with GetComponent.
$$anonymous$$y understanding of static was calling static on a var would then only allow one Instance/var be in existance. But of course I could be wrong.
This is awesome information , thankyou for taking the time to explain. $$anonymous$$y $$anonymous$$d is slightly blown, I'm still playing with your first example using class and extending $$anonymous$$onoBehaviour (something else I didn't understand from following the 3D Buzz TPC tutorial). I have lots to pick up and absorb before playing with Singletons , but I shall definitely check them out too. for now $$anonymous$$any Thanks :D
Just so you know, JS scripts are classes that extend $$anonymous$$onoBehaviour by default. So you don't actually need the "public class $$anonymous$$ySingleton extends $$anonymous$$onoBehaviour" stuff, you can just leave it out and it will work the same. You only need to define the class if you don't want to extend from $$anonymous$$onoBehaviour.
Thankyou both @BY0LOG1C and @Eric5h5 , the information I have gained and things I have learned from your comments have been amazing. Being self-taught from forums, sometimes the ter$$anonymous$$ology is lost on me. Yes, google is my friend, but sometimes it's nice to have a breakdown or an example so I can see what's happening.
The whole idea of the SINGLEton pattern is to have only one object of said Type or the last object to fill the instance variable will be your singleton, making every other object just regular instance. There's lots of room to add your own logic on top (hmm, a Dictionary?), but as a general guideline, its not very appropriated. As much as I love singletons, I only really use them in case where they actually make sense :D
Answer by Eric5h5 · Apr 04, 2012 at 06:45 PM
The JS equivalent of the C# code you posted is:
static var Instance : TP_Controller;
function Awake() {
Instance = this;
}
Thankyou , but @BY0LOG1C answer helps me understand what I'm trying to do (as I only half-knew what I wanted!), as well as introducing class and extending $$anonymous$$onoBehaviour. I appreciate you showing me the C# to JS conversion for this though, thanks again.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do i reference a non static singleton? 2 Answers
How do i reference a non static singleton? 1 Answer
Distribute terrain in zones 3 Answers
Can't access a javascript static variable from c# script 1 Answer