• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Erm... I'm Kind Of A New JASS Triggerer, I Hope You'll Help Me Here...

Status
Not open for further replies.
Level 19
Joined
Oct 15, 2008
Messages
3,231
Erm, this is my first Trigger I tried to make using JASS, it's supposed to Kill a Unit right after it's Spawned.

JASS:
function Spawn_Kill takes nothing returns nothing
call CreateUnit (hpea)
// I'm trying to create a Peasant //
call KillUnit (hpea)
endfunction

This doesn't seem to work, please help me someone...
 
Last edited:
Level 20
Joined
Jul 6, 2009
Messages
1,885
JASS:
function Spawn_Kill takes nothing returns nothing
    call KillUnit(CreateUnit(Player(0),'hpea',0.00,0.00,0.00))
endfunction

Would be something like that since you can instantly create and kill a unit in single line.
Or:
JASS:
function Spawn_Kill takes nothing returns nothing
    set bj_lastCreatedUnit = CreateUnit(Player(0),'hpea',0.00,0.00,0.00)
    call KillUnit(bj_lastCreatedUnit)
endfunction
Now when creating a unit,you need to fill required arguments/parameters.
In this case,first is which player should own the unit,then unit ID,the next are unit's X and Y where it's supposed to be created and last is it's facing.

As you might have noticed,the owning player is Player(0). In JASS,player indexes have their value smaller by 1 than in GUI,which means this associates Player 1 in GUI.

Also this is a very useful tool when working with JASS.
 
Level 14
Joined
Jul 26, 2008
Messages
1,009
Think of it in rational terms.

You tell the computer to destroy -A PEASANT- but not -THE PEASANT-

We have to figure out a way to tell the computer to destroy the peasant we just created.

bj_LastCreatedUnit is just a variable the game uses and that you too can use. It saves you time from making your own variables.

You can save that unit to that variable, and then tell the game to kill that specific unit via the example shown.

The other problem is of course you need to fill in the extra values asked for. Garfield has shown you what these values are. If you get a helper program, most of them will tell you the extra values needed.

Hopefully this helps you get that much closer to making your own JASS stuff.
 
Level 19
Joined
Oct 15, 2008
Messages
3,231
Thank you so much, I really hope I didn't trouble you both... +Reputation!
I have another question though, about what Garfield1337 said:
which player should own the unit,then unit ID,the next are unit's X and Y where it's supposed to be created and last is it's facing
Erm... How about the 'Fly Heights' of the Units and stuff like animation of the unit, etc. Where do those go?
 
Level 19
Joined
Oct 15, 2008
Messages
3,231
This Is A Confusing Trigger I Wanted To Do . . .

Erm... I have another question, I hope you people wouldn't mind:
JASS:
function Peasant_Eats_A_Tree takes nothing returns nothing
// This Function Is Supposed To Kill A Peasant After He Eats A Tree With My 'Eat Tree' Spell //
call CreateUnit (hpea)
call ability (aeat)
endfunction

// I'm kind of confused around here, sorry. //
Caster_UnitId takes nothing returns integer
return 'hpea'
call KillUnit
endfunction

Please help me somebody! I hardly know what I'm writing :/
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
Ummm...you should really read some tutorial - this is where i learned JASS from.
Also about killing peasant after creating him,did you see the function i wrote?
JASS:
function Spawn_Kill takes nothing returns nothing
    call KillUnit(CreateUnit(Player(0),'hpea',0.00,0.00,0.00))
endfunction

Now it's a bit confusing what you're trying to achieve. You say that you want to kill peasant after he uses your Eat Tree ability - why you create peasant then?
If you want to kill unit that uses Eat Tree ability,use trigger like this:
JASS:
function Condition takes nothing returns boolean
    return GetSpellAbilityId() == 'Aeat'
endfunction

function Actions takes nothing returns nothing
    call KillUnit(GetTriggerUnit())
endfunction

//===========================================================================
function InitTrig_JASS_Trigger takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition(t,Condition(function Condition )
    call TriggerAddAction(t, function Actions)
    set t = null
endfunction

1 more thing,when adding comments in JASS,you only need 2 slashes ( // ) at the start the text. (You write 2 more at the end of your comment which is unnecessary)
Write it like this:
JASS:
function Actions takes nothing returns nothing
    call KillUnit(GetTriggerUnit()) //This kills triggering unit :O
endfunction
 
Level 19
Joined
Oct 15, 2008
Messages
3,231
I'm really sorry, I was just really confused at the trigger format, because it's the first time I'm doing JASS, I get lost easily when I read the tutorials so I thought I'd do it myself and see how I learn it:) Thanks.
PS: Also, if I don't write the two slashes at the end, how do you set it so that you can 'tell' that it's ending?
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
I'm really sorry
Sorry about what? You're free to ask anything :D

Anyway,the tutorial i gave link for is easy to follow. Also it's not easy to learn JASS without a tutorial or someone who would explain you how to write codes.
And when typing comments: There's no need to 'tell' when it's ending because you can make whole line a comment or make a comment after calling some function.
Example:
JASS:
function lolzor takes nothing returns nothing
    //this whole line is a comment
endfunction

function moar lolz takes nothing returns nothing
    call SomeFunc(blabla) //this comment lies after the function,so there's no need to mark it's end
endfunction
 
When you learn JASS, a few things you need to keep in mind:

1. Conditions and Actions are almost the same thing. The differences between the two are unimportant for your level of JASS knowledge.

2. Functions are the bread and butter of JASS. A function is comparable to a math equation (SquareRoot, Cos, Sin, etc.), because you can use it over and over for many different things.

3. There are parameters, return-types and local variables in JASS which are nonexistent in GUI.

4. Syntax errors! GUI does not have these, but they can help you find your mistakes.

5. Endblocks...

JASS:
//Every
function <name> takes nothing returns nothing
//Must end with...
endfunction
//Every
if (true) then
// Must end with...
endif
//Every
loop
//Must end with...
endloop
//Every bracket or quote must end with a closing bracket or quote.
( )
[ ]
" "
' '

Ask questions based on new information you've received, and you will receive more specific answers.
 
Last edited:
Level 19
Joined
Oct 15, 2008
Messages
3,231
After reading some guides, I still don't understand what it meant by:

JASS:
function Random takes nothing returns nothing
// Well, I kind of don't understand what to put in the Returns and Takes tab, 
// Like, returns integer, etc. What do they do?
endfunction

Please do help me out, I'm really sorry for troubling anyone...
 
Level 22
Joined
Feb 3, 2009
Messages
3,292
The takes keyword is a value which the function needs, if we put

JASS:
function Random takes integer a returns nothing
endfunction
Now if we call the function, we need to parse a number to it, or it won't work properly:

JASS:
call Random(5)

// or

local integer number = 5
call Random(number)
If the function Random takes integer as we have above, and we try to call it without parsing the argument, we'd get a compile error:
JASS:
call Random() // will give a compiler error when saving!
The return keyword is a value tha the function will return with the return command.

JASS:
function Random takes nothing returns integer
return 5
endfunction


function Main takes nothing returns nothing
local integer number = Random()
endfunction

And for the last I will provide a full example for takes and returns:

The function will return a random number among the 1st argument and the 2nd argument.
In this case a random number between 1 and 50.


JASS:
function Random takes integer n1, integer n2 returns integer
local integer rnd = GetRandomInt(n1, n2)
return rnd
endfunction

function Main takes nothing returns nothing
local integer number = Random(1, 50)
endfunction
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Let's take the mathematical function that was mentioned earlier and modify it a little: ƒ(x) = x²

Clearly what the function ƒ does in this situation is take a parameter "x" and square it. Notice that the value returned could be a decimal point number - in Warcraft III the type associated with this is a real, so let's make this function in JASS:

JASS:
function F takes real x returns real // I will explain further
    return x*x;
endfunction

In function ƒ you've got a single parameter "x". Notice how when we define a "parameter" (in this situation the parameter is real x) that there is a type real and a label x. The label or "name" of the parameter allows you to reference it throughout the function. In function ƒ we give it the name "x" and reference it as "x" in the function. Multiple parameters must be separated by a comma.

Also notice that the return parameter does not have a name. Think of the name of the return as the name of the function; it gives a "type" value to the function. In the case above of function ƒ the name of the function is "ƒ" and the type would be a real (as shown by the return). This is very important information because when you reference a function as a value you need to think about the type of the function.

Let's use this in a more elaborate example.

JASS:
function F takes real x, real y returns real
    return x*x + y*y;
endfunction

function DoMath takes nothing returns nothing
    local real array pointX
    local real array pointY
    local real distance

    set pointX[0] = 50
    set pointY[0] = 80 // yields point (50, 80)

    set pointX[1] = -800
    set pointY[1] = -70 // yields point (-800, -70)

    // Notice how the variable "distance" has been 
    // defined of type "real". This means that we need
    // to use real values to associate with it.

    set distance = SquareRoot( F(pointX[0]-pointX[1], pointY[0]-pointY[1]) )

    // The parameters being given to function "F" are
    // shown above. The "x" parameter would be satisfied
    // as "pointX[0]-pointX[1]" and the "y" parameter 
    // would be satisfied as "pointY[0]-pointY[1]". 

    // This example (function F) will sum the squares of
    // the "x" and "y" parameters; once these operations
    // have been performed a value of type "real" will be
    // returned - this value will then be assigned to the
    // reference named "distance".

endfunction

I hope this helped without saying too much that has already been said.
 
Level 19
Joined
Oct 15, 2008
Messages
3,231
It's confusing, but I'll try to get it, thanks:)
Erm, there's another question if you guys wouldn't mind though:

JASS:
function Unit_Enters_Region takes nothing returns nothing
call CreateUnit
// I'm actually quite stuck here, how do you make an event (Any event would do) which happens when a unit enters a region?
endfunction

I hope you guys wouldn't mind...

PS: What are 'reals' ?
 
Level 37
Joined
Mar 6, 2006
Messages
9,243
You can do it like this. I'm using playable map area as the region.

JASS:
private function InitTrig_SomeTrigger takes nothing returns nothing
        local region mapArea = CreateRegion()
        local trigger t3 = CreateTrigger()
        
        call RegionAddRect( mapArea , bj_mapInitialPlayableArea )
        call TriggerAddAction( t3 , function SomeFunction )
        call TriggerRegisterEnterRegion( t3 , mapArea , null )
    endfunction

Integers = 1 , 5 , 100 ...
Reals = 1.15 , 1.1 , 3.668 ...
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Above the rect bj_mapInitialPlayableArea is being added to the region mapArea so that the trigger-event can be registered. This is necessary as there are no natives that allow events to be registered using rects.

The final step is to add an action to the trigger with the registered event so that when the registered event occurs your designated function (in this case function SomeFunction) will be executed and from there you can use event responses to reference associated handles.

Good luck.
 
Level 13
Joined
May 11, 2008
Messages
1,198
come on people...if he wants the mark the comment end for his own purposes...let him. the computer doesn't care...so why do you care?

btw, if you use latest vjass you can start a comment with /* and end with */ maybe that will suit your style nicely! :)

this style of comment ignores lines...it's whatever is within the two markers, so it's more flexible than //
 
Level 19
Joined
Oct 15, 2008
Messages
3,231
Hmm... So, besides reals and integers, what are the other parameters allowed to be added into the:

JASS:
function parameter_test takes nothing returns nothing
// What other parameters could replace the 'nothings' that might be important for me to know about if I want to know JASS?

I'm really sorry if I caused too much trouble, I hope somebody would help...
 
Level 5
Joined
Oct 14, 2010
Messages
100
You mean Jasshelper? It's included in the Jass Newgen Pack (http://www.wc3c.net/showthread.php?t=90999)

Jass Newgen also has an "advanced" script editor that allows you to look up functions, which will definitely help you finding what you need.

Functions really are the backbone of programming. Jass functions show similarities but also show differences with mathematical functions. Although this was already explained in this thread, repetition might help you understand jass better.

In the example I will give you now, it is important that you know the vocabulary used in mathematics (http://en.wikipedia.org/wiki/Function_(mathematics)#Vocabulary)
JASS:
function f takes real x returns real
    return x * x
endfunction

  • function f: f is the name of our function
  • takes real x: Everything that comes after the keyword "takes" are arguments.
    The keyword real is the Domain of the function. In this case, our function f takes all real numbers as input. x is the name we've given to this parameter. One thing to notice is that what is known as an "argument" in mathematics is known as a "parameter" in programming. "arguments" in programming are actual values given to parameters.
  • returns real is the Codomain of the function. This is the set of possible values that this function can produce as output (again real numbers in this case)
  • What follows now (on the next line) is the recipe. The recipe tells the computer how to produce the output. In this simple example, the recipe is very easy: simply return x * x.
  • To finish our function, we simply write endfunction

It is important to know that there are a few significant differences between functions in procedural programming (jass) and mathematical functions.
  • A mathematical function can have multiple arguments and multiple outputs. In jass, you can only produce one output. For example: function f takes real x, real y returns real this function uses 2 parameters(named x and y) but can only produce one output (because there is only 1 output possible, it is unnamed)
  • A jass function can produce so-called "side effects". In mathematics, there is no such thing as a "side effect" because all you can do is take inputs and produce outputs.
    To solve this, jass (and any procedural programming for that matter) adds side effects: these are effects that change the state of the game. For example: call RemoveUnit(udg_unitVar). By calling "RemoveUnit", we actually remove a complete unit from the game. There is no mathematical way to describe this.
    If we look at "RemoveUnit" in more detail, we find this:
    native RemoveUnit takes unit u returns nothing
    What this teaches us is that this function takes a unit as an argument but doesn't produce any output at all! (hence the returns nothing). From a mathematical point of view, this function is useless because it doesn't do anything. However, it has a very important side effect of removing a unit from the game.
 
Level 19
Joined
Oct 15, 2008
Messages
3,231
I'm really sorry if this is a simple question, but I'm kind of confused, what is the difference between natives and reals?
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
A native is basically a "built-in" function. It is something that is provided to the World Editor as a means to manipulate almost any aspect of the game, from changing the animation a destructible should play to inflicting damage upon a unit.

A real is a type of variable that is used to store decimal numbers, as opposed to integers which only store whole numbers including 0.

JASS:
local real realValue = 10.75
local integer intValue = 10

Also let's say you were to divide the example integer intValue from above by 3 the result would be 3, without any decimal places or fractions. When an integer is confronted with a division that has a remainder the remainder is ignored completely. So basically: (1 / 2) = 0.

A native would be something like:

JASS:
call KillUnit( bj_lastCreatedUnit )
 
JASS is a script, meaning it's interpreted. C++ is a programming language that is compiled down to bytecode. Warcraft 3 was coded in c++. C++ is the thing that runs JASS.

RtC allows you to inject dlls into warcraft 3, thus allowing you to extend the JASS language. DLLs can actually be written in any .net language, so you don't actually need to know c++, although c++ is the language that the RtC API is written in.

Now as for a language that's built into NewGen (different topic), that'd be Lua : P. Grimoire, the thing that puts everything together, uses the Lua scripting language for customization =D.

Lua can be executed in a wc3 map as well and is used for object editor manipulation. Lately, people have started using it to generate dynamic JASS code (like me).
 
Level 5
Joined
Oct 14, 2010
Messages
100
Actually, jass is compiled to bytecode on map-load. c++ is compiled to machinecode. Since jass is compiled to an intermediate language, it's reasonable to call it a compiled language, rather than an interpreted language.

But as far as you (foxy, that is) are concerned, you don't need to learn lua, and definitely don't need to learn c++.
 
Er... machinecode, assembly, and bytecode are all essentially the exact same thing.... >.>

And yes, I know that JASS is compiled to bytecode at run time ;D.

Also lua is an extremely useful language to know for map creation, especially in team projects. Azlier learned some of it so he could modify/create mass objects. It's a given in systems that use objects as well, although the current techniques for generating objects are poor and sloppy. I've been making a little snippet, but it's not fully released atm : P.
 
Level 5
Joined
Oct 14, 2010
Messages
100
bytecode and machinecode aren't the same thing at all. One is directly executed and machine dependant, the other is interpreted by a virtual machine, which is machine independant on platforms supported by the VM. Bytecode is slower too because it's interpreted (at least in case of jass), more specifically re-interpreted every time it runs.

As far as lua goes, I'm not denying its usefulness in certain cases, but this is way over foxy's head at this point. Besides, both languages are used in different domains, and are unrelated to each other.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
There is no size limitation for Warcraft III maps; there is only a size limit on the maps that are able to be hosted online - the reasons for this should be obvious. Either way, that is completely out of the scope of your map; your map cannot change BNet specifications.

A parameter is just a representation of a value. In programming, you may only have a certain amount of primitive types which represent completely base-level data types. In Warcraft III, those are these:

JASS:
boolean
integer
real
string
handle

I believe there is some sort of "Agent" data-type now but I'm not really sure what that is for, other than that everything extends a handle.

For example, the following are types of data that extend from the handle data-type.

JASS:
widget
trigger
timer
texttag

The amount of general purpose that a timer has makes me want to provide it as a "primitive" type but it isn't, and I believe it extends a handle. I'm not sure whether or not a hashtable extends a handle either.

And yes, I know how to use vJass in maps.
 
It is working properly... you can even play it online.

However, to play an RtC map, each player must have the related dlls. This is the reason why nobody uses it. By using RtC, you severely limit your audience.

How many randoms on b.net do you think have RtC or will ever have it? : P

Even if you tell them to download it in the map, they probably never will.
 
A lot...

stacks (two ways to do it)
lists
queues
dynamic memory allocation
structs (and how to code them from scratch)
interfaces (and how to code them from scratch)

And that is just to start...

you also need to learn about local data and networked data, syncing, group enumerations, conditions vs. actions, and typecasting.

Lua is a good language to learn as well

The above should start you on your way to becoming a master coder for wc3 ; P

I didn't mention leaks because they will be rather obvious to you while you are learning about instantiation and the various handles of wc3. I think leaks are only not obvious to GUI users ; P
 
Status
Not open for further replies.
Top