Event-Oriented programming (in JASS)

Daelin

Kelani Mage
Reaction score
172
Event-oriented programming (in JASS)

1. Introduction
This tutorial is not only for those who have worked in programming before but do not know this concept, but also for those who haven’t written their own programs and scripts yet. You do not need to know GUI to understand this tutorial but you may need some basic knowledge of JASS or else some concepts may sound awkward. It does not teach you how to write JASS scripts yet will surely explain some concepts without which programming in JASS would be very difficult.

2. What’s event-oriented programming?
Event-oriented programming (EOP) is based on the cause – effect logic. A series of actions is caused by an event. For example, if you’re driving a car and you push the brakes, the car will progressively stop. If you leave a glass-cup in tin air it will fall to the ground (due to the force of gravity) and break into pieces due to the impact. Well, in programming it’s quite the same, just that the events are a bit different.

But why did event-oriented programming appear? I can’t exactly answer this question, but I can guess. I believe that event-oriented programming appeared due to the increasingly interest into graphical interfaces. We no longer work with the computer by writing complicated commands on a black screen and receive written answers or results. Due to the graphical evolution of the computer technology, we now have friendly windows, practical menus with buttons, interactive files which now are not only different by their extension but also by their icon, colored backgrounds and so on. It is clear that the manners of programming must have changed in ten years.

Event-oriented programming these days bases on the elements mentioned above. Activating a button results in a series of actions, let it be saving a file, creating a new one, or printing it. What is even better is that you do not need to bother about how will you write the code “button is pressed” because there are systems containing a lot of useful functions which handle this for you.

And that leads us to another important element of event-oriented programming at that is the engine behind it. Compared to structural programming, EOP needs a system that contains a set of functions you will work with. That’s the good part. You no longer have to write the whole code from the beginning, due to the existence of these menus. We can say that EOP is an extension of the “old programming way”.

And last but not least concept is “thread of execution”. At first, only one program could be executed at a time. For those who remember the old operating system DOS, also remember that at that time we could not run more than an application at the same time. However, windows on the other hand allows us to run multiple programs at the same time. It’s not something uncommon to surf on the internet while having messenger open, talking with more than a person at the same time, while listening to music too. This is possible just because of the existence of the threads of execution. They permit executing multiple operations at the same time, unlike the sequential programming which requires the operations to be evaluated one by one. The operations found in the same thread will be executed that way, but multiple threads permit executing series of operations at the same time. Take as an example an electrical circuit. Having a single wire, electrons must go one by one through the wire. But splitting the wire into multiple wires results in a circuit. That way, the electricity will be split into multiple wires and so, multiple appliances can run using the same source.

3. Getting into warcraft 3
So how can all this information help me program in a game? It’s not like I’ll only manipulate icons and menus. True! But units for example are also entities that can respond to certain events. Let it be loss of life, use of a spell or death. Combining the event with a series of actions will result in a special behavior of the unit.

Also, I mentioned an engine behind the EOP. Well, warcraft 3 obviously has an engine behind its functionality. The “system” used by JASS is actually a large number of native functions, each doing a specific thing, let it be modifying an unit’s life, creating a new item or changing the visibility of a tree. How are these natives created? You are here to learn to program in JASS, not modify it. That should not concern you. By using these native functions, you are capable of writing scripts in JASS. Without them, the programming language simply does not exist. No engine, no event-oriented programming.

The threads of execution are pretty logical here and do not need a lot of explanations. You can move multiple units on the map, that’s the trick with Real-Time Strategy games. A lot of stuff take place at the same time. Doing this means using threads, so it’s pretty logical that when programming in JASS you will have to use them.

A very important element to understand in JASS is the “trigger”. Each trigger can be assigned multiple (but preferably only one) events and a series of actions. You will notice that a trigger is also assigned conditions. Usually having only events and actions, everytime the event took place, and you wanted the actions to take place only in certain conditions of the event (such as “a footman dies”) you would have to add that condition in the actions manually. However with the help of the engine, you can assign the trigger a boolean expression. Once the event takes place the boolean expression is evaluated. Is it evaluated true, the actions will take place. Events are something extremely general (or extremely specific) so in most cases restricting the code with conditions will be necessary.

Question comes how can you determine certain elements of the event? For example if an unit is dead, you will most likely want to determine the unit that just died and maybe even the unit that killed unit. Well, the engine comes in place here too. Using specific functions that retrieve the unit requested, you will be able to work with them. Using the incorrect function will not return any unit (null value).

4. First script in JASS
Yep, this is quite a problem for many. Understanding programming concepts may not be enough when it comes to the way programming in JASS works. I mean yes, understanding how the whole function stuff, using local variables and working with units and triggers has been explained a million times. What remains a mystery for many is how exactly are triggers created and how to assign events, conditions and actions to them. Previous GUI coders may rely to GUI -> JASS conversions but this is just a shallow method and should be avoided whenever possible. I am going to show you how global triggers work.

Open the world editor and enter the trigger editor (F4). You will notice a piece of paper in the left side of the screen. That is a global trigger generated as you create the map. However, you will surely need more than one when it comes to programming in JASS. So to create one search in the menu for the same icon. Press it and a new trigger will be created. You can name it however you want (I called it Test).

We are not going to work in GUI, but go directly for JASS. In order to edit the trigger in JASS, go to Edit – Convert to Custom Text and then click ok. The code should now look like this:
Code:
 function Trig_Test_Actions takes nothing returns nothing
endfunction

//===========================================================================
function InitTrig_Test takes nothing returns nothing
    set gg_trg_Test = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Test, function Trig_Test_Actions )
endfunction

Unless you understand what’s going on here it will be practically impossible to work in JASS. We are interested right now by the part after the long row of “=”s. You will notice a function. When creating a trigger the editor added the function between the scripts of the map. When the map would start, this function would be automatically executed. You can create as many triggers as you want. Each will contain a similar function and they will all be executed simultaneously (threads of execution, remember?) when the map starts.

The name of the function is of the form InitTrig_TrgName. Since our trigger is called “Test”, TrgName is replaced with “Test”. Notice that if your name contains spaces, they will automatically be replaced by “_”.

Inside the function you will notice that a trigger is created and is pointed to a variable. gg_trg_Test is actually a global variable. For previous-GUI coders this may seem really weird since you never initialized that variable into the variable editor. Consider that it is the trigger you just created. Its name is also coded gg_trg_TrgName.
Warning: If you remove this line then the trigger will never be created and so it will never actually work, regardless the events, conditions and actions you add to it.

The next line calls a native function which transforms the instructions inside a function into the actions of the trigger. The function must be of type “code”, so it never has parameters or returns a value. You can change the name of function as you like. Some Standards (for example JESP) may request that you do so.

To add a condition you need to use the native TriggerAddCondition. Use as parameters the trigger and a boolexpr. To obtain a boolean expression you will have to use another native, “Condition”. As a parameter use a function just like for actions, but the difference is that the function must return “boolean”. How is function executed? Well, if the event takes place, then the function is executed. At its end, it will obviously return a boolean value. Only if the value is “true” will the actions be executed.

But I didn’t mention anything about adding events. There is more than a single function for initializing events. I suggest you download JASS Shop Pro and type in the search bar “TriggerRegister”. All the natives initializing events will be listed.

I would like to point out that the initial function is much more important than thought by many. Keep in mind that it is executed at the beginning of the map. This makes “Map Initialization” triggers practically useless. For those who want to hide passive abilities by disabling them (or the spellbooks that contain them), there is no need to make a separate function. Just disable them in the initial.

5. Last note
I would not be surprised if the reaction to this tutorial would be something like “that’s great but this doesn’t really explain much about JASS”. It was not intended to do so. It was intended to make the event-oriented side of JASS clearer. If you didn’t get much from it, read a tutorial which teaches you JASS and then reread it. It should be much clearer. Here are some tutorials you may use:

JASS: Basic – by Im_On_56k
JASS: So You Want to Learn JASS? – by emjlr3
JASS: GUI to JASS – by Daelin

~Daelin
 

Daelin

Kelani Mage
Reaction score
172
Actually it should be in tutorials section if approved. This comment was really pointless. As far as I know, all tutorials must be posted here before approved. JASS Section was added later but the "tutorials posted here" stuff remains I believe.

~Daelin
 
D

dadads

Guest
"Tutorial on event-oriented programming" ?
It's just the same as "Tutorial on how to utilize triggers".

Do people actually need to read this tutorial?
Coz the way I see it, Warcraft is already event-oriented (Trigger-based) by itself.
 

Daelin

Kelani Mage
Reaction score
172
Not really. I am not bothering on explaining it in GUI. I am doing it entirely in JASS. And yeah, Warcraft is event-oriented, but not everyone understands how this works, don't ya think? :rolleyes: It's not important only to know that "it is event-oriented" but also how it actually works. And believe me, implementation between GUI and JASS is a bit different. If you actually bothered to read the tutorial you should realize that it is not that useless. Just because you understand this important concept doesn't mean that everyone does. It took me a lot to understand it so clearly. ;)

~Daelin
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top