This is my first post here, just bought Unity, I got really impressed with this engine, hope to create some great games with it.
My first question is: how can I create a single page will all the scripting? Instead of writing code fragments for each component? I also work in Flash, and it allows you to write code in each frame and inside visual objects (movieclips). That could shorten the programming, but can become a mess when abused. So I like the classic single page coding better
You want a single script for all the code in your game? I guess that would be possible with a lot of painful coding contortion, but also it would be highly inefficient and strongly discouraged. Just go with the Unity flow, and check out the various example projects to see how it’s done.
Thanks! But I’m an expert contortionist , that’s why I’d like to do it that way. I don’t see how it could be inefficient, as javascript is made to deal with that kind of programming.
Question is where can I bind the single code page to the whole scene, and which object contains the scene elements. I might discover it anyway, but wanted to get some clues.
Well that’s the thing… You’d need to create references to each and every object that you’re moving or changing throughout the entire scene. The reason it’s inefficient is that with larger projects you end up taking more time to figure out which reference goes with what new function than is necessary.
The code may run at the same speed, but it’s woefully inefficient for the person running it after a point.
No, you really don’t. Trust me, if you’d been using Unity for a while you wouldn’t even ask the question. Just look at any of the example projects. This one for example…trying to do that with a single script would be a total nightmare.
It isn’t possible to receive events unless you put scripts on the GameObjects that emit those events. If you insisted on only having one centralised script, quite a lot of Unity’s features would be inaccessible, not the least of which is the ability to react to collisions by implementing OnCollision and OnTrigger events. You could potentially put simple scripts on objects which do nothing but forward events to your main script… but then you have a great deal of needless additional complexity and overhead.
If your requirements are very limited, you might be able to make a game with just one script. For anything remotely complicated, you’ll find that a single script design requires a lot more code, is a lot harder to manage and that some things just can’t be done that way.
You would bind the code to a GameObject in the scene.
I guess, you would dynamically create new GameObjects, AttachComponents (which are actually also scripts!) to them and put them in some sort of lists so you can access them from the “MainGame” script.
At Unite08, there was a lecture where the presenter used this kind of approach. ( I think it was called “Writing Re-usable code”?).
Wait for the Unite videos to come up and check the video
in C# if there are delegates used, maybe it is possible? From UnityScript, I tried but I did not get it to work.
I still recommend starting out using MonoBehaviours. It is so fast and easy to access. Design your code so that you can move to more centralized implementation if it worries you
Remember, Unity’s Javascript is not the same as ‘normal’ Javascript, so a lot of things you might take for granted don’t apply.
In fact, you can assign functions to variables, but not anonymous functions:
// This works:
function MyFunction() {...}
function Start()
{
var something = MyFunction;
something();
}
// This isn't supported:
var something = function() {...}
Even so, the event system is not implemented in that way, and you can’t assign an arbitrary function from an arbitrary object to be called when a particular event happens on some other object.
DON’T make it one single script other wise nightmares shall haunt you forever
just kidding but seriously don’t make it one script it definitely would be a terrible nightmare of all the references not to mention all the other code!
Ok, now I understand that the scripting language of Unity is not Javascript, but a resemblance of it. That doesn’t leave much space to contort, but it’s ok to make nice projects, the engine is awesome anyway.
Another question I have, which language generates more optimal to code? Or, are Unity’s JS, .NET and Boo the same animal with diferent skin? I read MonoBehaviors doesn’t support namespaces, so it seems to be another resemblance language… so Unity languages might be just coding flavors for a single intermediate language.
Thanks for clearing out my doubts
PD.: Hmmm, I still will try post-scripting, maybe that’s a good option for “defragmented” programming. 8)