Getting Started

Back to lessons

Getting Started

This guide is meant to get you started from programming no vjass to basic code. You won't need any previous coding experience to follow this tutorial, but GUI experience is recommended, if anything. The first thing you'll need to do is install a vjass compiler. I recommend SharpCraft but NewGen World Editor works as well.

Create a map, open the Trigger Editor, and create a new trigger. I call mine VJass Demo. Select the trigger, then select Edit on the menu bar and find and select Convert to Custom Text, five items from the bottom. This will open up a code window where the GUI editor normally is. Delete any auto-generated content in this window.

VJass code can be contained in three types of blocks: library, scope, and struct. Libraries are typically used for code systems, and scopes for stand-alone code like spells. Structs are more complicated and will be covered later. For now, we will program a scope. To begin this, just type scope MyName. You also need endscope at the bottom of the code to tell the compiler that your scope is over.

scope VJassDemo

endscope


VJass code in scopes are placed inside functions. Think of a function as a block of code that accomplishes a single purpose. The syntax for a function is as follows:

function myFunction takes nothing returns nothing

endfunction


There are several parts to this function declaration:

1. The name - comes immediately after the word "function". In this case, it is called myFunction.
2. Arguments - comes immediately after the word "takes". We can specify that our function needs data to work. If the argument is "nothing", then we don't need to provide any data to use our function.
3. Return - comes after the word "returns". We can specify that our function will give a value back after running.

The generic syntax is:

function (name) takes (arguments) returns (return)

endfunction


For simplicity, right now we will only use a function that takes nothing and returns nothing. Let's create a function called onInit inside our scope. The purpose of this function will be to activate and do something on map initialization.

scope VJassDemo
    function onInit takes nothing returns nothing

    endfunction
endscope


Now, a function does not automatically run just because it is placed in a scope. Currently, this function is never used. In order to link up this function to be called at map initialization, we can add the initializer keyword to the scope declaration line:

scope VJassDemo initializer onInit


Now, as soon as the map loads, it will execute the code inside the function onInit. Finally, we need to add some code to the function onInit in order to make it do something. There are lots of functions we can call in vjass. In general, each GUI Action you can use corresponds to one function we can call in vjass, as well as many functions that do not exist in GUI.

For this demo, we will make it display "Hello World" to all players. There is a function provided by Blizzard to display text for 60 seconds to all players, called BJDebugMsg. Let's place this function call inside our function onInit:

call BJDebugMsg("Hello World")

Let's take a closer look at this line.

1. call keyword begins the line. This specifies that we are calling a function, as opposed to setting a variable or something else.
2. Function name - in this case, BJDebugMsg. The function name is whatever you are trying to call. There is a big list of functions we can call in the Functions List option of the editor.
3. Parenthesis - in a function call, open parenthesis always comes after the function name. This is where we specify any data the function needs to run (called the argument)
4. The argument - in this case, "Hello World". This is a String that contains the words Hello World. This data is passed into the BJDebugMsg function, which is then shown to the user. Arguments go inside the parenthesis.

This is my final scope:

scope VJassDemo initializer onInit
    function onInit takes nothing returns nothing
        call BJDebugMsg("Hello World")
    endfunction
endscope


Try running this in a map. It should display Hello World to you for 60 seconds upon startup.

To review:

- VJass code should be located in libraries, scopes, or structs.
- In a scope, all vjass code is contained in functions.
- To declare a simple function: function myName takes nothing returns nothing
- Placing the initializer keyword along with your function name after the scope name will cause that function to be run on map initialization
- There are a lot of built in functions into the language. Call them using the syntax: call functionName(arguments)

Back to lessons