Javascript strong typing on iPhone

Hi

Can someone give a review of the proper variable typing set up for iPhone?

I know its supposed to be strong typing - yes?

( im asking because I seem to be struggling with the various types and casting issues - stuff that I believe would work with less strict type rules – such as all the examples in the manuals? )

Do we have to type check into parameters and etc?

( right now all the examples in tutorials / manuals are not using strict type checking - right? - it would help if someone could give a quick review of strong typing rules for iPhone - or showed good coding examples for newbees to follow! )

cheers.

Tj

iPhone JavaScript behaves as if #pragma strict were set on normal Unity. Dynamic typing is disabled, which means everything becomes strongly typed. However, type inference still works. It’s important to understand the distinction. You can still do:

var someString = "hello";
var someVector = Vector3.Up;

These variables are strongly typed (to String and Vector, respectively), despite the fact that you didn’t specify their type. It happened implicitly. If it helps, you can optionally declare the types:

var someString:String = "hello";
var someVector:Vector3 = Vector3.Up

You’re probably getting tripped on on places where dynamic typing helps. Instantiate(), GetComponent(), Arrays, and so on all deal with more generic types. You can only put the most generic type, Object, into an Array or ArrayList, and GetComponent() returns a Component.

Take this:

var myRigidbody = GetComponent(Rigidbody);

What type is the myRigidbody variable set to? The answer isn’t Rigidbody; it’s Component! If you tried to use myRigidbody.velocity, you would get an error.

This is because GetComponent() needs to return the most generic type possible. All Rigidbodies are Components, but not all Components are Rigidbodies.

The answer here is to declare the variable type:

var myRigidbody:Rigidbody = GetComponent(Rigidbody);

This will attempt a cast.

…and if this doesn’t clear things up, just declare types on each and every variable :wink:

Nice…

this is EXACTLY the issues I was having… :smile:

Now I know why I kept tripping up on these!

cheers and many thanks!

Tj

Thanks Matthew, great example. Great time. Clear explanation.

Many many thanxx for the Explanation !!!
Need to be in iPhone FAQ !!!
THIS IS SO IMPORTANT FOR iPhone Dev’s !!!
shit that i found it so late !!! I had lots of stress less if i know before… :cry:

So Moderator do your Job and put it on the iPhone FAQ Page because 30 % of the Questions here are about this issue !!!

many many thanxxx for the great and understandable explanation!!!
we need more of this who take the time to give a useful answer !!!

:sweat_smile: kerstin

Its documented in so many places, threads and I think even in Unity Answers that anyone not finding it actually is to blame himself and kind of “deserved the lection” he just learned.
It might actually even be mentioned in the generally by desktop unity users ignored iphone specific docs

ha ha since the last Version and it’s not very well done… These here is so clear if you compare it to the Doc’s… or why have so many Problems with it and ask still here ?

Very good explanation.

My problem is with GetComponents (Plural)

I was able to get rid of the compiler warning with:

var myArrayOfStuff:MyC#ScriptName[] = go.GetComponentsInChildren(myC#ScriptName);

but I get a run-time error ‘cannot cast from source type to destination type’

What’s the proper casting/syntax for arrays?[/code]

Very good explanation.

My problem is with GetComponents (Plural)

I was able to get rid of the compiler warning with:

var myArrayOfStuff:MyC#ScriptName[] = go.GetComponentsInChildren(myC#ScriptName);

but I get a run-time error ‘cannot cast from source type to destination type’

What’s the proper casting/syntax for arrays?[/code]

I may have answered my own question: use a generic variable as the target of GetComponents(), then in the loop that goes through them, define and use a strongly-typed var to hold the ‘current’ item. Yes?

Bumping this because it is listed in the iphone FAQ. iPhone javascript users should add #pragma strict to the top of their code so that everything need to be strongly typed or it will throw a compile time error. This is the best/cleanest way to strong type and will catch any that slip through the net.

Actually, I found that declaring the type does not attempt a cast strongly enough. But using “as (type)” does:

var MyScript:ScriptName = gameObject.GetComponent(ScriptName) as ScriptName;

is highly redundant, but it satisfied #pragma strict and all compiler targets I’ve tested.

Can anyone convert this to pragma please ?

vehicle.GetComponent(VehicleControllScript).controlsEnabled = true;

because I get this error :frowning:

Assets/Resources/OldScripts/Vehicle scripts/WehicleScript.js(78,53): BCE0019: ‘controlsEnabled’ is not a member of ‘UnityEngine.Component’.

there is no space in controls … this is messing with me today lol

Try this instead:

var controlScript:VehicleControllScript;
controlScript = vehicle.GetComponent(VehicleControllScript);
controlScript.controlsEnabled = true;

And see if it works. Of course you can put the variable definitions (the first 2 lines) in Awake() or Start(), no need to do that every time when you need to access some property of the script. Strict typing, strict typing…