- Home /
how can I create a variable to refer to any gameObject script with pragma strict
I want to create a global variable in pragma strict that refers to the script attached to any game object. Is this possible? scriptTypes are script1,script2,script3 etc...
#pragma strict;
var script; //Defaults to Object;
function GetProperty(go:GameObject,scriptType:?){
script=go.GetComponent(scriptType);
Debug.Log(script.property1);
}
Am I understanding this correctly that you want a variable that can hold a script1, script2, script3, etc., and from what your code says, access properties that are shared between these scripts?
If that's the case, what I'd do in C# is derive all your scripts from a base class and then set the variable to the base type.
e.g., the base class is myClass, and the definition for script1 is
class script1 : myClass
then you'd define your variable as so:
var script : myClass
Any properties you'd want to share between the scripts would need to be defined in the base class and overridden if desired in the derived classes.
Unfortunately, I have no idea how to do this in JS. Hopefully someone else can answer that for you.
Are you saying in javascript I would write
class scriptType{ var script1:script1; var script2:script2; }
var script:scriptType; I'm confused about how this would help.
syclamoth: can scriptType be a variable that I can pass into a function? So I could do.
function getProperty(go:GameObject,scriptType:$$anonymous$$onobehavior){
script=go.GetComponent(scriptType);
script(as scriptType).property=10;
}
or something like that...?
Well, even if that were possible (which it is, in C#, but we'll get to that later if you want to), you'd still have the same problem- You wouldn't be able to know what the exact type that was inputted into the function was!
So, say you have a type 'scriptType' that includes the variable 'property', and another type called 'anotherType' that does not- there's no way to know that your hypothetical function 'getProperty' has been called like this:
getProperty(myObject, scriptType);
ins$$anonymous$$d of like this:
getProperty(myObject, anotherType);
So, you come back to the same problem. You need to specifically limit the number of types that you can use, if you want to be able to access a specific variable.
The best way to do this, if you were using C#, would be to use a common interface. This way, you could implement it however you liked, as long as the script conformed to a given 'signature'- and then you could use that to access the variables ins$$anonymous$$d.
But you're using JS, so I can't really help you.
Answer by syclamoth · Mar 20, 2012 at 09:33 PM
There are two things that you're trying to do here, and they kind of conflict.
If you have a variable that you want to be able to refer to any script, it's simple enough to just go-
var script : MonoBehaviour;
But, this won't allow you to access
script.property1
because that is specific to a certain subclass. Because at that point in the program, that object could be anything, there's no way that the compiler can know exactly what type you expect there, so it won't allow you to use it that way.
There are a few ways around this. The simplest is, if you (the programmer) know what the type is expected to be, to just manually cast the object into the correct type.
script(as scriptType).property;
Keep in mind that this won't work if you get it wrong.
The other way you could go would be with @NOAA_Julien's solution, but this puts a hard limit on the types that you can use with the system- so you have to be aware of the limitation. Also keep in mind that using this architecture can screw around with the serialization- which isn't a problem if it only needs to work at runtime, but make sure it doesn't catch you unaware.
var script:$$anonymous$$onoBehaviour;
Is there a javascript equivalent of this line:
script(as scriptType).property;
or for typecasting script below as a cubescript?
script= GameObject.Find("Cube").GetComponent(cubescript);
I think the generic version should work properly-
var script : cubescript = GameObject.Find("Cube").GetComponent.<cubescript>();
that or
var script : cubescript = GameObject.Find("Cube").GetComponent(cubescript) as cubescript;
I'm running into trouble. I'm trying to set the script once globally depending on the object -- so that all other functions can use the script.
var script:$$anonymous$$onoBehaviour; function getScript(name,type){ if ( name=="cube"); var script:cubescript= GameObject.Find("cube").GetComponent(cubescript) as cubescript;
}
function setProperty(){ getScript("cube","cubescript"); script.property1=20; Debug.Log(script.property1); } setProperty();
also how are you highlighting the code in a comment. I couldn't find any info about how to do that...
For starters, I was talking hypothetically- I'm pretty sure you can't do what you're trying to do here. Also, for the code highlighting, I'm taking advantage of the fact that there's no functional difference beetween the syntax in the 'answer' field and the syntax in the 'comment' field- for long segments it's as easy as leaving one empty line on each side, and four spaces before each line, and for short segments use the tags
and < /code >
(without the spaces, but obviously I can't write that) to mark individual words and sentences (you see what I did there?).
If it's all too hard, you can also write everything out in the 'answer' field, use the preview to make sure it looks right, and then cut and paste it into the comment box!