# Methods Possible?

Are there any methods to define constants like we do in programming languages



supported in java script in unity?.

define works in C#, so does #if and the like; I'm not sure about #include but I don't think so (C# has added "partial classes" which partially solves what #include was for ... but I think in general #include comes from times when we had no real OO-programming but don't flame me for saying that ;-) ). [EDIT: Oh, and I've never tried using partial classes in Unity, so I don't know if that's implemented there].

With JavaScript/UnityScript, there's no such things AFAIK. And one major limitation (even with C#) when it comes to using #if is that you can't do global #defines via the editor that are passed during compilation, yet. So, you have to #define for each file where you want to use the define.

Oh - and I think that's not really the way to define constants; for that you'd use "const" in C#. Not sure how that works in JavaScript, though.

One random link to a discussion about #include is here.

I did some testing with partial classes a while ago, and as far as I can remember they worked fine, but not if they were MonoBehaviour subclasses. Don't quote me on that one though.

You can use a static class for all of your global "defines". It's hacky and not good programming practice for most things, but it's there if you really need it.


Ah - cool to know that partial classes would work (even though I personally don't need them ;-) ).

I'm not so familiar with what these "constant defines" are usually used for in other languages - but assuming that this is "stuff" that you define once and then tweak according to your needs the best approach for doing that in Unity may actually be having some sort of Singleton-game object (see AManagerClass on the Unifycommunity for a starter) that you keep in a startup scene.

That way, you can use Unity's nice "tweak everything in the editor" approach which I'd prefer to changing variables in code (but that's really just a matter of what you like more). It does come at the price of a bit of extra complexity, though: For that approach to properly work in all circumstances, you'd probably have to put some code into all your scenes that assures that this "startup-scene" is loaded if the given object is not available.

Actually, I guess using a startup scene for this might be overkill and instead a simple prefab would probably be even better. A nice thing about using a prefab that always gets instantiated in every scene for this specific purpose is that I think prefab changes stay if you apply them during play (unlike changes in the scenes).

My point being: You can create a very convenient environment for yourself when you keep the possibilities of the editor ("tweaking properties during runtime") in mind while designing your code-framework. But if you prefer changing the "constants" in code, what Jeremy suggested is probably the best approach (I don't even find that this hacky ;-) ).

Heck, while we are at it, XML yourself if you like. :-)

Back at iceherosubzero, as you are probably gathering by now, in Unity the need for preprocessor symbols is not nearly as great as with other languages such as C/C++. I rarely ever touch them anymore.

Conditionals can be helpful, but still mainly during debugging, and as Jashan and I suggested there can be much nicer ways to do it that fit Unity a bit better.

If you have specific reasons for using them, ask on the forums here, and people will probably be able to suggest better/quicker ways of doing it.

Good luck!

I think there's one very interesting use case for conditionals in Unity: Building for different platforms or applications. Like: If you build for the Web player, you might want to "physically" exclude some stuff (like, for instance, a logging framework which would bloat the Web player download size). Or: If you have an authoritative server running as standalone, implemented with Unity, you might have a lot of "server-only code" that you don't want to distribute with the clients (as this would make hacking the clients much easier as you basically "deliver" the server code - including any checks against client-hacks ... as well as all information about where you missed those checks ;-) ).

So that could look something like

public void ServerSideRPC(someparameters) {
    // some very secret server-only code ;-)

That particular use case is why I'm really looking forward to whenever I can define global defines for the editor in Unity (which currently is not possible). Worthy of note is that Unity iPhone does define the conditional compilation constant "UNITY_IPHONE" (not perfectly sure if that's exactly that - it seems to be a bit undocumented), so you could have iPhone or non-iPhone specific code very easily.

Thanks a lot jashan and jeremyace for your help. I know its too late to reply. Never checked the forum after I posted a few queries here after 2009. :P


[quote=“jeremyace”, post:3, topic: 397099]
I did some testing with partial classes a while ago, and as far as I can remember they worked fine, but not if they were MonoBehaviour subclasses. Don’t quote me on that one though.


Quoting you on that :wink: Partial classes should work just fine so long as both partial classes are named correctly and placed in the same assembly (i.e. you don’t have one in an Editor directory and one not).

In terms of the #defines, it’s not generally considered great practice in C++ - that’s what the const keyword is there for. It comes in handy for defining macros and symbols though.

As Jashan says, the only real time you need them in Unity is for conditional compilation - and it’s a bit of a shame that you can’t set a global define directly in a source file. That being said, you can always set global defines for your project in Unity’s Player settings. (Or via script actually with PlayerSettings.SetScriptingDefineSymbolsForGroup)