How To: Set Project-Wide #pragma Directives with JavaScript

Create a text file in the root of your project's Assets folder:

Your Project/Assets/us.rsp

With the following:

-pragmas:strict,downcast

Voila! No more implicit variable declaration bugs without needing to #pragma every script.

Note that this includes the Editor/ compilation pass; if you play fast and loose with dynamic typing in editor scripts this may create more hassle than it resolves.

Thanks to Rodrigo for the tip!

Any idea if there is some way to get #defines globally like this for C#?

1 Like

Why would you want it for C#?

That's a nice trick Matthew. As a joke, I was thinking of coming in here and saying, "Yup, that's easy to do: switch to C# :p"

I would love a global #define Debug rather than having to add it to every single script.

You can add a file called smcs.rsp to the Assets/ folder. The content of the file will be added as arguments to the compiler.
Do a 'smcs -help' to get all the options. Compiler bat location: \Unity\Editor\Data\Mono\bin\smcs.bat

If you add '-define:MyDefine' to smcs.rsp, MyDefine will be a global define for all C# scripts.

1 Like

@kjems, that is awesome. I never knew about that. It appears that once you set something in the smcs.rsp file then remove it the define is still present. Do you know if there is some way to clear the cache or wherever it is storing the define?

Disregard. It appears to clear itself after a few runs. Awesome tip!

[quote=“prime31”, post:6, topic: 431634]
@kjems, that is awesome. I never knew about that. It appears that once you set something in the smcs.rsp file then remove it the define is still present. Do you know if there is some way to clear the cache or wherever it is storing the define?
[/quote]

Changes to the smcs.rsp will not trigger the automatic refresh of the AssetDatabase as scripts and other assets do.

JFYI - I just tried this in Unity 3.3 and smcs.rsp did not seem to work. However, I changed to gmcs.rsp (gmcs being the newer compiler, I think) and it works great! =D

What does the pragma downcast do? I use the #pragma strict in every javascript, so was curious if I should use downcast too.

Gigiwoo

[quote=“anon_81618884”, post:9, topic: 431634]
JFYI - I just tried this in Unity 3.3 and smcs.rsp did not seem to work. However, I changed to gmcs.rsp (gmcs being the newer compiler, I think) and it works great! =D
[/quote]

Any thoughts on Unity 3.4? I’ve tried with smcs.rsp and gmcs.rsp (and even mcs.rsp) but no luck.

[quote=“anon_31032468”, post:11, topic: 431634]
Any thoughts on Unity 3.4? I’ve tried with smcs.rsp and gmcs.rsp (and even mcs.rsp) but no luck.
[/quote]

Sorry for double-posting; just realised that my last post was entirely unclear.

I am also trying to set project-wide #defines for C#. I am on Unity 3.4 and have tried creating gmcs.rsp, smcs.rsp, and even mcs.rsp under the Assets folder of a project, but none of these seem to be read. I’ve tried “-define:TEST_DEFINE” and “/define:TEST_DEFINE”. Just wondering whether anybody else had come across this since upgrading to 3.4 or if I’m just doing it wrong…

TIA.

I tried the smcs.rsp on Unity 3.4 and it works fine.
The one thing to remember is that you should make sure to save your affected files even if there are no changes to the file.
This is because Unity will not re-compile your code unless the Saved Date is different.

Examle:
if I write "-define:TEST_DEFINE" in smcs.rsp and I add

if TEST_DEFINE

....Some Code....

endif

in code and save, it will work.

if then I change the smcs.rsp file to say "-define:TEST_DEFINE_2" and run Unity it will still run the "....Some Code...." because the code in the define was previously compiled and Unity did not detect a change in the code file, so it did not bother to re-compile it.

Once you re-save your code file (just press the save button or CTRL-S), Unity will re-compile the code correctly according to the new Defines in smcs.rsp.

I hope this helps.
:)

Thanks, I hadn't realised that re-importing the .rcs wouldn't cause a recompile. All sorted.

I got global defines running for C# using gmcs.rsp and Unity 3.4, thanks for the tip!

Is there also a way to pass a define argument to the Boo compiler?

I can not get this to work, and I am not a coder, but I am trying to get this sorted myself.... I am using Unity 3.4. In my Assets folder I have the file: smcs.rsp with the following code inside: -pragmas:strict,downcast However when I quit and restart unity to get the scripts to recompile I get this errors: Internal compiler error. See the console log for more information. output was:error CS2007: Unrecognized command-line option: `-pragmas:strict,downcast'

Basically what I am trying to do is open an old project in 3.4 and I learn't that I need to disable Dynamic typing? So I found this solution which I can not get to work for the life of me.

Also I tried the other solution where I put #pragma strict after the vars but before the function start () example below:

private var noOfZaps : int;

#pragma strict

function Start()
{
    gibbing = false;

When I do that I get these errors:

Assets/02Scripts/GameManager.js(57,27): BCE0043: Unexpected token: ;.

and

Assets/02Scripts/GameManager.js(59,7): UCE0001: ';' expected. Insert a semicolon at the end.

It is annoying when I an not a coder but I am trying to get this working all help would be very greatful!

Thanks

Only if you're doing iOS/Android. Otherwise switch to web/Mac/PC publishing. (And you needed to do that for iOS/Android before Unity 3.4 anyway, the difference is that now it applies when scripts are compiled, instead of when you build the project.)

--Eric

Sorry I should of been more clear, it is an iOS game. I added "var" in front of each of the errors and it seemed to fix them. Again this is all guess work as I am not a coder hahaha

So what would I need to do to incorporate that anyway for the iOS game to fix the old project? I read somewhere that iOS games in 3.4 automatically use #pragma strict is this true? and if so then those errors I had before need to be fixed another way? It is kinda annoying since the game worked perfect before but I needed to do an updated build to support OF 2.xx

I have the OLD Xcode project which worked like a dream and now with xCode 4.xx it does not even load it crashes after the splash screen. So this is why I am trying to update the actual project.

Thanks.

iOS always used pragma strict automatically, now its just written there.

Thanks