I understand that when you see the curved brackets…()…next to a function…function Foo()…that they are designed to carry information about the function. Would anyone have the time to explain how to use these? 99% of the time I leave them empty, Id just like to know of what we can use this functionality for.

Curiousity mangled the cat…
Thanks though
AC

Let me illustrate that with a function I just wrote: I’m doing something where I want a seeded pseudo-random number. You can’t set the seed for the random function in Javascript, so I had to write my own. I came across a cheap cheerful pseudo-random algorithm on an ancient web page (don’t use this for your cryptographic needs, folks! ), and I’m just interested in getting back a repeatable but “random” series of integers (which changes only when you change the seed number), so I ended up with this:

``````function RandomNumber(range : int) {
randomSeed = (randomSeed*9301 + 49297) % 233280;
var returnValue : int = (randomSeed / 233280) * range;
return returnValue;
}
``````

Note that I’m using the variable “range” in the calculation, which I’m defining as an integer. But “range” comes from outside the function, passed by whatever has called it. So now I can do stuff like this:

``````var randomSeed = 1.0;

function Start() {
print ("Here's a so-called random number between 0 and 9: " + RandomNumber(10) );
print ("Here's a so-called random number between 0 and 19: " + RandomNumber(20) );
print ("Here's a so-called random number between 1 and 100: " + (RandomNumber(100)+1) );
}
``````

So you use it for passing values to your functions. I’m not sure I can think of a reason to pass values to something like Update(), but it’s a function just like any other so at least there’s that possibility. The values can be anything…strings, gameObjects, etc. Also you can have multiple values passed at once, if so needed.

–Eric

when you make a function you usually just “define” it, instead of writing its body. An example function could be the print function. Like this

c# code

`````` int Add( int num1, int num2 );
``````

This is just the prototype. The type of the function is “int” which means essentially it has to return data of type int (integer). If you were to put float, that would mean the function would have to return data of type float (where it be a variable containing something 0.0f, or 0.0f.) Add is the name of the function, and the two variable inside of the ‘(’ and ‘)’ are the parameters (also know as arguments). The parameters are variables you can pass to function to use.

This is how you would go about writing the definition of the function

``````int Add( int num1, int num2 )
{
int sum = num1 + num2;
return sum;
}
``````

This should be pretty self explanatory. It’s just the prototype with the body or definition of the function. What we do is create another variable of int called sum, and set it equal to num1 + num2, and then return the data (you’ll see what I mean by returning it in a second). See how we use the parameters?

This is how we could use that function in context.

`````` ...

int one = 1;
int two = 2;
int OnePlusTwo = Add( one, two );

print( OnePlusTwo ); // This function is made up, pretend it prints it's parameter ( OnePlusTwo ) to the screen.
``````

we make two variables and store 1 and 2 in them. Then, since the function Add is of type int and thus must return something of type int, we can set a variable of type int equal to what the function Add returns. In this case, since we pass in one and two as the parameters/arguments, Add will return the sum of those to variables, in this case is one + two, or 1 + 2, or 3. So, we set OnePlusTwo equal to what Add( one, two ) returns, which is 3.

Hopefully you understand this. This uses C#, so data types are important. If you were to use JS, then the data type doesn’t matter as much. Though you can explicitaly declare what type function and variables are like this

`````` var myVar : int;
// or for function
function Add( var num1 : int, var num2 : int ) : int
{
//....
}
``````

The reason I used C# is so you understood how function return data, and why/how parameters can be useful in this.

edit - Beat to it

Thanks heaps, I’ll chew this over and ask when I get stuck…
AC

That’s OK…as you can see, I was editing my post as you were posting. So you only have to type slightly faster next time. Better to get things from more than one perspective anyway…I only sort of know what I’m talking about…sometimes…

–Eric

AC

I usually think about it visually. Each function is a box with a bunch of plugs. To make it work you’ve got to plug the right things in.

``````function MyFunction( firstPlug : int, secondPlug : String ) {}
``````

you’ve got to plug an int and a String into that function when you call it to make it work.

``````MyFunction( 5, "Hello" );
``````

By plugging something into a function, you’re “feeding” it something to use.

I got a private message from another forum member suggesting I dont ask noob questions and just "buy a book"so I just want to re-itirate(sp) my thanks, because posts like the ones you 3 have provided are really helpful. I think if your already a programmer then unity programming is probably playtime, but Im actually a qualified blacksmith instead and I find it quite tricky.

Is there any other 3d type of instance where you use javascript?
Something where I could read external learning resources-(not how to code a website) ideally a bit of physics scripting?

I know for a fact that when someone asks a beginner question, the answer assists dozens of other beginners.

PS the Docs are really really great and it most certainly wasnt an OTEE person who said that.
AC

Hmm. Something that has been encouraging to me has been that all the object-oriented programming languages operate in very similar ways.

There is some difference in the syntax, but the idea of an “object,” a “function,” and a “variable” with a “type” are quite common. Thinking about this stuff has been much more helpful for me than thinking about Javascript or C# or whatever in particular.

If you’re going to get a book, then I bet getting a Javascript book will be confusing because most of them focus on web development and I don’t think Javascript is usually used in an object oriented environment. If there’s a book on the ideas in object-oriented programming it might be worth it.

Even better though would be an introductory Computer Science class that has a programming aspect. It’ll probably use a different language (Java is supposed to be a good language to learn on), but you’ll probably learn everything you need to get going quite quickly. Just a semester should do it.

that’s just silly. sure a book could help but don’t let that garbage bug you, t. ask away…

I agree with Marble…it’s not the specific language, so much as basic programming concepts, that’s important. In fact, if you’re going to take a class (and that’s a good idea), I think it would probably be best to learn a language that Unity doesn’t support, and it should be about programming something other than games. That may initially sound like it wouldn’t be useful, but I believe that general knowledge of how programming works is actually far more important than just focussing on “plug value B into slot A” specifics.

–Eric

Buying a book on general programming concepts is actually a great idea. Once you pick up the basics; variable assignments, casting, control structures (if, else, while, for … etc), exceptions, you provide yourself with the tools to write code.

You mention you’re a qualified blacksmith? I know nothing about it apart from seeing horseshoes and swords being made on TV. My point being that I doubt you’d recommend me just using a tongs and hammer and an anvil without a little on-the-job training first?

Programming is a mental task, you need a little training first to grasp the basics.

Having said that I’d probably suggest you enrol in a night-school course and maybe do a little visual basic or Java - It’s not javascript or C# but it should ground you in basics (no pun intended)

The bar is much higher these days for entry into programming, compared to the old 8 bit days of typing

10 PRINT "HELLO! THIS COMPUTER SUCKS! ";
20 GOTO 10

At your local electrical store, and then running off laughing

Okay, the concept of learning unrelated stuff as being generally helpful is a new one to me, I’ll look into it…and I value the advice.

Classic. That is almost exacly all I could do before Unity came along. And some non-code dreamweaver stuff
Cheers see ya
AC

Well, the thing is that it’s not actually unrelated. Learning the building blocks of programming allows you to understand why Unity works the way it does. Like if you learn how to use arrays to build a little spreadsheet or something, and understand the concept, you can see how you’d use them to control a fleet of spaceships as well.

It’s like I see how some people just focus on learning by rote to press button X to achieve result Y using MegaProgram2006, and when they upgrade to MegaProgram2007 and have to use button Z instead, they’re lost. But if you understood how to use the program in general, it doesn’t really matter what buttons you press specifically, so any changes just take a brief adjustment instead of having to learn everything over again.

This sort of thinking let me test out of a computer course years ago, without ever before having used the programs I needed to use to pass the test. I was able to figure out everything on the spot because I understood the general concepts, which sure beat tediously slogging through an entire semester of “press button X to achieve result Y”. In the same vein, if you understand loops in general (for example), then you don’t really care whether you’re typing “for x = 1 to 10” or “for (x = 1; x <= 10; x++) {”. That’s just a matter of syntax; the concept is what’s important. So you’d have an idea when you’d be better off using “while (x <= 10) {” instead.

–Eric

Since this thread has rightly turned into “what resources would help me learn to be a programmer,” I’d like to ask what was the name/kind of general computer science class you took that was most relevant to object oriented programming in Unity?

I ask because my only formal programming course was in Pascal 25 years ago, and since then I’ve learned OOP sort of by doing ActionScript programming. That is obviously not a coherent approach to learning good programming, so now I need something that is not an intro course, but one that does cover the fundamentals.

Any thoughts on what that course would be?

A lot of Java classes will hop right into the OOP stuff nowadays.

Java is not a bad lead in to C#, either. (Not so much for Javascript, ironically.)

Another idea is to go for the Bruce Eckel books. Thinking in Java would give you a great foundation in OOP. You can get the first 7 chapters in a pdf here at the official site: http://mindview.net/Books/TIJ4

I haven’t read it through, but it’s very, very well regarded.

I’ve only heard the best about those books. The C++ ones looks excellent.

The 3rd edition for “Thinking in Java” is totally free

Almost any OOP programming book or tutorial can help you a lot (even in another programming language) if you approach is as learning the concepts like Eric said.

Once you understand the root concept of object-oriented programming, the rest is just learning the dielect to tell the compiler what’s going on.

Here is good online C# tutorial that I have found:
http://www.softsteel.co.uk/tutorials/csharp/lesson3.html

If you go through that and skip the pointers, delegates, and .net 2.0 specific stuff at the end, it should help anyone understand things a bit better. (perhaps even people using JS exclusively as it shows you a bit more what’s going on behind the scenes)

If you take it one bite at a time, and follow a good tutorial or book so you learn the concepts in the right order (no learning about polymorphism until you know what an array is ), it is not as hard as it seems to get up and running.

Perhaps we should have a sticky in the scripting section about programming learning resources?

-Jeremy

I started with procedural programming a few years back and the best book I ever read for going OO was the Aaron Hillegrass book on Cocoa and Objective-C. It did a great job with OO-type design, it is well written with nice tone (not dry), and you work with a really well thought out framework. Objective-C looks funny to many people, but it is actually pretty easy to read. After I did some Obj-C I found Java a chore and C# only ok. The downside with Obj-C is that it is mainly on the Mac and going from it to a static type language can be painful since you have to type so much more.

You could also look at one of the scripting languages such as Ruby, which is OO. I believe it ships standard on OS X. Type “ruby -v” (minus the quotes) on the command line in the terminal. If ruby is there then it will give you the version. Ruby is one of the cool kids on the block.

The hardest thing if you have not done much OO is to think in OO terms instead of procedural terms. It sounds like you have used all the basic constructs such and conditionals, loops, and functions, so it seems you just need to get comfortable with objects and how you think about them.

The problem with JavaScript books is that they tend to focus on web page related stuff and are geared around doing things with web pages. If you were to choose between Java and C#, I would go with C# because you can use it in Unity. If you go with C#, get a book that deals with the .Net framework since you will be using it from Unity. Just skip the stuff on UI. .Net is not a bad framework and is rather nice in some areas. Also, going from C# to Unity’s JS implementation is fairly straight forward. You can also look at using Python to learn, just use Boo in Unity. Unfortunately, with Boo in Unity you cannot use all the Python libraries. Perhaps Otee will add IronPython I find Python easy to read and quick to program in. The downside is that it is sensitive to indention, but that is what makes it so readable.