#pragma strict? Do I need it?

So I sort of have a very limited idea of what it does with making the code more strict about variables (I don’t know much more than that) and I’ve been trying to use it because other people have told me I should. But all of the tutorials I’ve found that are even remotely in depth like the walker boys series don’t use it and if I do use it at times it becomes game breaking. I ran into an issue earlier and decided to keep #pragma strict even though I know it’s the issue. I’ve been working on this for a month and nothing I write works no matter what source material I look at I’ve read through the script reference’s description of use for the thing I’m trying to use (GetComponent) and I’ve gotten so close to a solution but I’ve been working on if for a month now and the semester’s about to end. I can’t ask the teacher for help because it’s an independent study and I don’t think I have the time to keep trying to figure it out. So I just want to know why exactly I need to use #pragma strict?

EDIT: I’m using Java Script.

If you’re coding in C# you don’t need it, AFAIK.

Nope, Java Script…

Also AFAIK?

As Far As I Know.

pragma strict prevent dynamic typing from working.

Since it’s done at runtime, it’s slower. Hence the recommendation to use #pragma strict.

Okay, thanks In your personal opinion do you think I really need it?

If you ever want to publish to mobile platforms, you will need #pragma strict. If you are following tutorials that don’t work with #pragma strict, it’s time to follow different tutorials. And If those tutorials are in C#, it’s time to learn C#.

Ah, who am I kidding, it’s time to learn C#. :wink:

Here’s the deal: UnityScript (what they call JavaScript) has a feature where it doesn’t necessarily need to know the type of a particular variable right when it’s declared.

var x;
x = "foo  ";
Debug.Log(x.Trim());
x = 23;
Debug.Log(Mathf.Max(x, 10) );

UnityScript will understand that x is a string for line 2, and it will be able to call the string’s .Trim() function. And come line 4, it knows that x is now an integer, so it can allow it as a parameter in Mathf.Max.

Nice feature, right? Very newbie-friendly? Well, here’s the problem: Code that does that is basically impossible to compile, in the traditional sense of the word “compile”. When you type x.Trim() on an object that it knows at compile-time is a string, the compiler can have that function point directly to the bit of code that executes that function. When you type x.Trim() and it doesn’t know at compile time what x is going to be, it has to compile this as a bit of code that says, basically, x.WhateverThisIsFindTheFunctionNamed(“Trim”);

So when you stick a string into x, it has to do a little tiny bit of “compilation” to figure out that x is a string, and where that class’s function named “Trim” is, and it has to do that on the device, as it’s running. It’s a clever strategy, don’t get me wrong. BUT…

  1. It’s slow. Searching for the function by name takes much longer than just pointing it to the function that it knows ahead of time.

  2. Compiling code on the device is a big no-no with Apple. (I think Android Play and the Windows Store have similar policies, but don’t quote me on that.) It has the potential to be wildly insecure.

Basically, all #pragma strict does is turn off that feature of the language. The speed is a factor in the recommendation to use #pragma strict. But I never saw #pragma strict in the wild until Unity started compiling to iPhones. Being able to publish to iPhone is the main reason to use #pragma strict.


P.S. There's one subtlety that might confuse you if you start experimenting, and its name is *type inference*. This code will not run with #pragma strict:

```
var x;
x = "foo";
```

But this will run just fine (and is valid in C#, even!):

```
var x = "foo";
```

The first is *dynamic typed*. Once it gets to the "var x;" line, it knows nothing about x, it just makes x a bland object and marks it as "I'll figure it out later", and it's the figuring out later that causes the problems.

The second is uses *type inference*. By looking at the line as a whole, the compiler can figure out that by "var", what you really meant was "string". If, in the next line, you set x = 23, it will then give you an error, because it's already decided that x is a string.

Type inference is really useful if you're dealing with certain advanced data types, like generics:

```
//with type inference
var someDictionary = new Dictionary<string, SomeDataTypeWithALongName>();
//without it
Dictionary<string, SomeDataTypeWithALongName> someDictionary = new Dictionary<string, SomeDataTypeWithALongName>();
```
2 Likes

(I was typing something also, but @StarManta said everything I would have dreamed of saying and more)

Thank you so much that really helped! Like of all the definitions that was easily the most informative. But now I want to ask; I’m still new I’ve only been learning for a couple of months now with javascript, should I decide to just go with C# instead? Or will that be too hard now?

You should start learning C#, and your knowledge of UnityScript gives you a good foundation. You just have to unlearn a few quirks of the language you’ve been using so far :slight_smile:

Alright I think I’ll try it then, thanks!

To the OP: Feel free to ignore this post. The general tone of the messages above are good. Forget about UnityScript. Go learn C#.

All vey good points on static typing versus dynamic typing. The only thing I want to add is that UnityScript doesn’t actually support dynamic typing. (It still compiles through an IL stage, where no dynamic typing exists.)

UnityScript fakes dynamic typing. Underneath the variables are still statically typed. The compiler makes a guess at the variable type, and normally ends up calling it a System.Object if it gets confused. That’s always a valid choice, because every type is a System.Object. Even value types can be boxed into a System.Object. The classic noob UnityScript error is “System.Object does not contain a method xxx”.

pragma strict tells the compiler not to guess types. This puts a little more work back on the programmer, but it prevents problems when the compiler guesses wrong.

2 Likes

UnityScript is just there to attract web developers to Unity…I got fooled by it and see now I am game developer who started to love C# more …

Just go for C# it has great advantages you will start to discover once you get more experienced in it.

1 Like

Thanks, The more information the better!

You also don’t get namespaces with UnityScript. That’s pretty important when you want to avoid your classnames colliding with 3rd party code (and for neat, tidy code in general).

That’s what I would do. In case you need more convincing that this is the right decision:

  1. Most professional Unity developers use C#. That means that when you ask for help with C#, you’ll get help from people with years of professional experience. When asking for help with UnityScript, you’ll get hobbyists.

  2. C# is an established, popular language outside of Unity. UnityScript exists only within Unity. When searching for sample code to do something not specifically game-related, most code you find for “javascript” will be web JS, incompatible with Unity. Most C# sample code you find will work beautifully.

  3. If you want to code professionally (or have that door open to you in the future), you will pretty much have to use C#. Everyone you collaborate with will be using it.

  4. UnityScript is just falling out of popular use in general.

@orb : Is that girl in your picture puking up an octopus leg? What’s going on there?

Yep, this is another very important reason to avoid UnityScript. UT themselves are downplaying it now, and knowing C# helps you outside Unity. Even other cross-platform engines are starting to use it.

It’s just another bleh day in Innsmouth, and somebody needs a hug.

UnityScript autocomplete is apparently not supported in the latest official MonoDevelop version. So that’s yet another reason to jump ship.

Alright I’ve already started the Unity tutorials (I’ve always been kind of discouraged by the fact that I couldn’t really work with those since they were in C# anyway) and everything seems to be going well. Thanks for the Input everyone! :smile: