Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Javascript strong typing on iPhone

Discussion in 'iOS and tvOS' started by tbarbe, Dec 26, 2008.

  1. tbarbe

    tbarbe

    Joined:
    Dec 22, 2008
    Posts:
    154
    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
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    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:

    Code (csharp):
    1. var someString = "hello";
    2. 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:

    Code (csharp):
    1. var someString:String = "hello";
    2. 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:

    Code (csharp):
    1.  
    2. var myRigidbody = GetComponent(Rigidbody);
    3.  
    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:

    Code (csharp):
    1.  
    2. var myRigidbody:Rigidbody = GetComponent(Rigidbody);
    3.  
    This will attempt a cast.

    ...and if this doesn't clear things up, just declare types on each and every variable ;)
     
  3. tbarbe

    tbarbe

    Joined:
    Dec 22, 2008
    Posts:
    154
    Nice...

    this is EXACTLY the issues I was having... :D

    Now I know why I kept tripping up on these!

    cheers and many thanks!

    Tj
     
  4. mattiedenton

    mattiedenton

    Joined:
    Jul 30, 2007
    Posts:
    11
    Thanks Matthew, great example. Great time. Clear explanation.
     
  5. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Many many thanxx for the Explanation !!!
    Need to be in iPhone FAQ !!!!
    THIS IS SO IMPORTANT FOR iPhone Dev's !!!
    S*** 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 !!!

    :oops: kerstin
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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
     
  7. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    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 ?
     
  8. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    Very good explanation.

    My problem is with GetComponents (Plural)

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

    Code (csharp):
    1.  
    2. var myArrayOfStuff:MyC#ScriptName[] = go.GetComponentsInChildren(myC#ScriptName);
    3.  
    but I get a run-time error 'cannot cast from source type to destination type'

    What's the proper casting/syntax for arrays?[/code]
     
  9. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    Very good explanation.

    My problem is with GetComponents (Plural)

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

    Code (csharp):
    1.  
    2. var myArrayOfStuff:MyC#ScriptName[] = go.GetComponentsInChildren(myC#ScriptName);
    3.  
    but I get a run-time error 'cannot cast from source type to destination type'

    What's the proper casting/syntax for arrays?[/code]
     
  10. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    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?
     
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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.
     
  12. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    Actually, I found that declaring the type does not attempt a cast strongly enough. But using "as (type)" does:
    Code (csharp):
    1.  
    2. var MyScript:ScriptName = gameObject.GetComponent(ScriptName) as ScriptName;
    3.  
    is highly redundant, but it satisfied #pragma strict and all compiler targets I've tested.
     
  13. javanoob

    javanoob

    Joined:
    Aug 15, 2012
    Posts:
    64
    Can anyone convert this to pragma please ?

    vehicle.GetComponent(VehicleControllScript).controlsEnabled = true;

    because I get this error :(

    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
     
  14. mtoivo

    mtoivo

    Joined:
    Jul 30, 2012
    Posts:
    274
    Try this instead:
    Code (csharp):
    1.  
    2. var controlScript:VehicleControllScript;
    3. controlScript = vehicle.GetComponent(VehicleControllScript);
    4. controlScript.controlsEnabled = true;
    5.  
    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...