Lesson 1, 'Hello World'

Overview


In this lesson, I will go over with you a very simple function example. I will explain everything you will need to know about it and then at the end I will give you a 'challenge'. This challenge contains some content from the next section, so you will be able to find out your answer in Lesson 2

Now that you've looked at an introduction to the advantages of JASS, you're probably eager to get started with some code, right? Now, forget GUI (regular triggers), JASS is different from GUI, so I'll be treating it that way. Now, open up a text editor of some sort such as Notepad (windows) or some other program depending on your operating system.

Now, I'm going to show some code. It will be a bit confusing at first, but I'll explain it throughout this lesson.

Code:
function HelloWorld takes nothing returns nothing
    call DisplayTextToPlayer(Player(0), 0, 0, "Hello World")
endfunction

Now, don't put this into the editor yet as it's not complete. First off, I'll start with the first line. JASS, like many programming languages, is made up of a series of functions. I won't go into detail on that yet, but for now just know that you are creating a function.

Now, the next line:

Code:
 call DisplayTextToPlayer(Player(0), 0, 0, "Hello World") 


This line 'calls' a function, and while I told you to forget GUI, I need to refer to it for one thing. Think of functions almost as actions, they do something. Creating a unit involves using a function, and so on. Now, back to JASS.

This line calls a function which displays text to a certain player. You tell it what player, (ignore the 0s for now) and what message to display and it will show the message for that player. Now, players in JASS are refered to starting with 0 and ending with 15. Player(0) would correspond to Player 1(Red). You basically, take the player number and subtract 1 from it.

Code:
endfunction



Now, the final line is pretty simple, when you create a function it needs to end somewhere, right? This line just tells you that the function ends here.

Now, I've gone over with you a very basic example of JASS, if you wish to test this function in the World Editor, simply:
  1. Create a new map
  2. Go to the trigger editor
  3. Click on the map icon at the top of the list of triggers
  4. Paste the code in the custom script section.
Now, create a trigger (sorry for the GUI reference), you can make the event whatever you want, I'd suggest that you make it go off whenever you press an arrow key. Now, for the action, just find custom script, and in the box that it gives you, type:

call HelloWorld()

And there you have it, you can now test the function in the game.

Notice how you call a function in that line. I'm now going to go over the function declarations a bit. When you call a function, you have to have parenthesis after it with all the values it needs. For the DisplayTextToPlayer, it needed a player, two other values that place it on the screen, and a message. Let me copy the function declaration again for your function:

Code:
function HelloWorld takes nothing returns nothing


When a function has 'takes nothing', that means you don't need anything in the parenthesis when calling it. I won't go into detail about the return value yet, that will be covered some time in a later chapter.

Now, feel free to mess around with the HelloWorld function, you can mess with the 0, 0 values if you wish, they actually just mess with where the text is placed on the screen.

Challenge #1: Guess what this code does:

Code:
function HelloWorld2 takes nothing returns nothing
 local string message = "Hello World"
    call DisplayTextToPlayer(Player(0), 0, 0, message)
endfunction


The answer will be given in the next section as well as an overview of variables.