• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

[JASS] JASS - Takes and returns

Status
Not open for further replies.
Level 2
Joined
Jul 20, 2004
Messages
27
I don't understand what they are for? and I don't understand how to use them.

The tutorial I'm using is so unbelievably cryptic I'm about to bust a nut. I'll copy/paste the part I need help understanding:

Lesson 3: Functions
Overview

In this lesson, I will be going over how to:
1.Create functions
2.Call functions
3.Anything else I can think of

Part 1: Creating a function

Now, you're probably wanting to know how to create functions, right? Well, to create functions, you first need to tell the program that you're creating a function, so you do that by delcaring a function. This is basically telling the program the function's name, what values it needs to get when called, and what it returns. Take a look at this function:

JASS:
function DisplayAMessage takes string message returns nothing
    call BJDebugMsg(message)
endfunction

Now, the word 'function' tells the program that you will be declaring a function. The next part of the function is the name of the function. You need to make sure your function has a unique name that no other function has, otherwise you'll get errors when saving a map. Next, you tell the program what the function 'takes' or needs pretty much. This function 'takes' a string, which is basically a message of some sort. After you put the type the function needs, you then have to put a name for it (so you can refer to it inside of your function). Lastly, you tell the program the type the function will return. You could make this a string, integer, real, and so on. However for this example, no value will be returned so we put 'nothing'.
After declaring a function, inside the function you need to put what the function does, otherwise it would be pretty useless. This function simply calls another function using the string value that is needed to call it. Lastly, you need to tell the program when the function ends, you do this by putting 'endfunction' at the end of the function.

Part 2: Calling functions

Now, that may not all make sense, so I'll now explain how you call functions, which should hopefully clear things up a bit. First off, you can call a function in a few different ways, but I'll start by the easiest way, which is using the word 'call'.

JASS:
call DisplayAMessage("hello")

This line would call the function I just showed you. When calling a function this way, you need to have the word 'call' followed by the function name which is then followed by parenthesis with the values the function takes inside of them. If a function takes multiple values, they are each separated by a comma ','. It works that same way when declaring functions too (whoops forgot to mention that earlier =P)
Now, I will go over what the return values in functions are. They're pretty simple really, though I don't want anyone to be confused about anything. When a function has a return value, you can set variables that are the same type as the return value to that function. Here's an example:

JASS:
local string player_name = GetPlayerName(Player(0))

Now, see that when you set a variable to the return value of a function, you don't need the word 'call'. Also, if you want to pass the return value of a function into another function call, you don't need to put the word 'call' either, as seen with the Player function that was the needed parameter in the GetPlayerName function.

If when and when not to use call are confusing you, let me just say you can only have 1 call in a line, and call can't be used if you have another word before that such as 'set' or you're declaring a local variable.

Also, and this is important when declaring a function, if the function declaration states that the function returns a value, somewhere in the function if must have a line that 'returns' the value, like so:

JASS:
function TestReturn takes nothing returns integer
    return 5
endfunction

When a function reaches the line with return on it, it returns the specified value and ends (no code below that line is looked at by Wc3)

There is one last thing you need to know when calling functions, you can only call a function that is above another, since that may not make complete sense, here's an example:

JASS:
function SomeFunction takes nothing returns nothing
endfunction

function SomeOtherFunction takes nothing returns nothing
endfunction

Now, this is only used as a visual example so I can more easily describe what I was saying. You can only call a function that is above the function you are currently in. So, 'SomeOtherFunction' would be able to call 'SomeFunction'. However, 'SomeFunction' wouldn't be able to call 'SomeOtherFunction'.

Challenge Time!

Now, I figure you're wondering why the challenges so far aren't very hard (I'm just assuming that), and that's because we haven't taught you enough for us to really stump you on something. I hope this challenge will make you think more than normal. The following code has a few errors in it, find each error in it. There are 5 errors in the following code, can you find them all?

JASS:
function Function2 takes nothing returns string
 local string message = SomeFunction("Hello World")
endfunction

function SomeFunction takes string returns nothing
 local string a message
    a message = 'Something is wrong'
    call DisplayTextToPlayer(Player(0), 0, 0, a message)
endfunction



Good Luck -wyrmlord

Having read this, I'm still not an inche closer to understand the purpose/use of take/return.

For example

JASS:
function TestReturn takes nothing returns integer
return 5
endfunction

Now from what I understand, this function's under purpose is to return 5 to an integer, there by increasing the value of that integer. However what integer are we talking about? assuming what I'm thinking is correct

As for the take part I don't even have a clue, not even remotely what his talking about.

Any and all clearification would be nice
 
Level 16
Joined
Jul 21, 2008
Messages
1,121
Every function in JASS is declared by defining its arguments and return value.
takes - defines arguments of the function
returns - defines the return value

When moving from GUI, first you have to learn Local Variables (used only in the function they are declared in).

Now, when local variables can't be used in all functions you must have a way to "transfer them". Example:

JASS:
function Product takes integer a, integer b returns integer
     return a * b
endfunction

function A takes nothing returns nothing
    local integer a = 2
    local integer b = 3
    local integer c = Product(a, b)
endfunction

Now you see that integer c value will become 6 because function Product returns product of two numbers defined in arguments and stores it in variable c.

If I have set a = 5 and b = 6, c would be 30.

I hope this clears up something.
 
Last edited:
Level 21
Joined
Aug 21, 2005
Messages
3,699
A function in a programming language acts very similar to a function in mathematics.

Take the function y = x³.
Here's how this would look like in jass

JASS:
function MyFunction takes real x returns real
    // Inside this function, whenever we use "x", we are using a real number.
    return x * x * x
endfunction

// Somewhere else:
local real x = 2.0
local real y = 0.0
set y = MyFunction(x)
// This will set y = 8.0, which is x³.
Everything after "takes" are the function's arguments. In y = x³, x is the argument. But just like mathematical functions, functions can use multiple arguments.
In this case: MyFunction takes real x, which means it expects a real number to be specified if you call the function.

The word after returns is the type of variable that is returned after calling the function.
In this case, MyFunction returns real. This means the "result" of MyFunction is a real.

The return keyword will cause the function to return the value specified behind the keyword. This value MUST be of type "real" (since the function returns real). In this case, x*x*x indeed is a real.

Another example of a function:
JASS:
function Max takes integer a, integer b returns integer
// In this function, whenever we use "a", we are using the first specified integer number. Whenever we use "b", we are using the second.
if a > b then
    return a
else
    return b
endif
endfunction

// Somewhere else:
local integer a = 25
local integer b = 123
local integer c = Max(a, b)
// In this case, c is equal to b

The mathematical counterpart of this function is: z = Max(x, y)
You probably notice how the mathematical function looks a lot like the jass we've used:
c = Max(a, b) is basically the same as z = Max(x, y).

In the "Max" function, the function takes 2 integers, and calls it "a" and "b". Whenever you use "a" or "b" inside this function, you will be using either one of these 2 integers.
The "Max" function basically checks if integer a is larger than integer b. If this is the case, a is returned ("return a"). Else, b is returned.

When a function returns nothing, the function doesn't have a result. In mathematics, this doesn't make sense, because a function is designed around the purpose of having a result. In programming languages, there are some functions that don't have a result. For example:
JASS:
call BJDebugMsg("hello world!")
This function returns nothing. Because it returns nothing, we cannot possibly say: local integer a = BJDebugMsg("hello world!"), simply because it's not possible to set integer a equal to "nothing".
This function specifically, BJDebugMsg("hello world"), will display "Hello world!" on your screen.

Then programming languages also have functions that don't feel mathematical. For example:
native CreateUnit takes player id, integer unitid, real x, real y, real face returns unit
This is the "CreateUnit" function. Its purpose is to create a new unit on the map.
It "takes" following parameters:
player id: this player will be the owner of the new unit
integer unitid: this is the id of the unit. For example 'hfoo' is the unit id of a human footman.
real x: this is the X coordinate of the unit on the map
real y: this is the y coordinate of the unit on the map
real face: this is the facing angle of the unit on the map. Facing angle is a number between 0 and 360.

It "returns" a unit variable. This is pretty obvious because CreateUnit indeed creates a unit, and thus returns a new unit variable

An example of usage:
JASS:
local unit u = CreateUnit(Player(0), 'hfoo', 0.0, 0.0, 270.0)
This will create a human footman for player 1 (red) at the center of the map facing the south of the map.

Does this make more sense or not?
 
Level 2
Joined
Jul 20, 2004
Messages
27
Every function in JASS is declared by defining its arguments and return value.
takes - defines arguments of the function
returns - defines the return value

When moving from GUI, first you have to learn Local Variables (used only in the function they are declared in).

Now, when local variables can't be used in all functions you must have a way to "transfer them". Example:

JASS:
function Product takes integer a, integer b returns nothing
     return a * b
endfunction

function A takes nothing returns nothing
    local integer a = 2
    local integer b = 3
    local integer c = Product(a, b)
endfunction

Now you see that integer c value will become 6 because function Product returns product of two numbers defined in arguments and stores it in variable c.

If I have set a = 5 and b = 6, c would be 30.

I hope this clears up something.

While this is logical, and I understand why C = 6 in that situation, I still don't see how I can put this to practical use. I think its because the tutorial's writer, and you, use a termonology I'm not familiar with.

From what I could gather. returns = a type of condition. For example if I have a condition below it, I'd have to write: returns booleans.

Beyond that I'm none the wiser. Please try to reframe from using terms like "argument" and "return value" as I have very limited programming knowledge. Use terms that refer to either the GUI or to something else I can relate to please :)

(no offense intended)
 
Level 2
Joined
Jul 20, 2004
Messages
27
Then programming languages also have functions that don't feel mathematical. For example:
native CreateUnit takes player id, integer unitid, real x, real y, real face returns unit
This is the "CreateUnit" function. Its purpose is to create a new unit on the map.
It "takes" following parameters:
player id: this player will be the owner of the new unit
integer unitid: this is the id of the unit. For example 'hfoo' is the unit id of a human footman.
real x: this is the X coordinate of the unit on the map
real y: this is the y coordinate of the unit on the map
real face: this is the facing angle of the unit on the map. Facing angle is a number between 0 and 360.

It "returns" a unit variable. This is pretty obvious because CreateUnit indeed creates a unit, and thus returns a new unit variable

An example of usage:
JASS:
local unit u = CreateUnit(Player(0), 'hfoo', 0.0, 0.0, 270.0)
This will create a human footman for player 1 (red) at the center of the map facing the south of the map.

Does this make more sense or not?

yes this doesn't indeed make sense.

Edit:

native CreateUnit takes player id, integer unitid, real x, real y, real face returns unit

native?

JASS:
function Trig_Create_footman_Actions takes nothing returns nothing
    call CreateUnitAtLocByName (Player(0), "h000", (GetRectCenter(gg_rct_circle2)), 270.0)
endfunction
//===========================================================================
function InitTrig_Create_footman takes nothing returns nothing
    set gg_trg_Create_footman = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_Create_footman, Player(0), "footman", true )
    call TriggerAddAction( gg_trg_Create_footman, function Trig_Create_footman_Actions )
endfunction

I couldn't make your's work, so I tried to make one that was logical to me, and I ended up with. Why isn't mine as good as your's? I just tried it, and it makes my game freeze. What is wrong with it?
 
Level 21
Joined
Aug 21, 2005
Messages
3,699

Yeah, the "native" keyword is exactly the same as "function" except that you can't use it in jass. A "native" is a function defined by blizzard, which is directly converted in machinecode. All jass you write eventually all comes down to using native functions, which all comes down to machinecode.

call CreateUnitAtLocByName (Player(0), "h000",GetRectCenter(gg_rct_circle2)), 270.0)
This makes your game freeze because there's no unit with the name "h000". Using "footman" should work.
The function you used leaks a location variable. GetRectCenter(gg_rct_circle2) returns a new location, which leaks. In GUI, "Center of region circle2" also leaks a location, and GetRectCenter(...) is the equivalent jass of "Center of region circle2".

Because locations leak, it's much easier, and more efficient to directly use (X, Y) coordinates instead.

JASS:
function Trig_Create_footman_Actions takes nothing returns nothing
    call CreateUnit (Player(0), 'hfoo', GetRectCenterX(gg_rct_circle2), GetRectCenterY(gg_rct_circle2), 270.0)
endfunction
//===========================================================================
function InitTrig_Create_footman takes nothing returns nothing
    set gg_trg_Create_footman = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_Create_footman, Player(0), "footman", true )
    call TriggerAddAction( gg_trg_Create_footman, function Trig_Create_footman_Actions )
endfunction

This uses X and Y coordinates and avoids using locations. Try this, it should work now.
 
Last edited:
Level 11
Joined
Feb 22, 2006
Messages
752
The native just means that it's one of the blizz-defined functions that you can use without having to define them yourself.

And if you still don't get what parameters/arguments are for, the simplest way to explain them is that they are used to pass information into a function. For example, in the product function, to compute a product the function needs to know what two factors to multiply together (it can't just multiply two random numbers and hope those two numbers it picked were the ones you wanted), so you use parameters to pass that info into the function.

Likewise, if you were going to make your own KillUnit() function, you would want to know WHICH unit to kill. The way to do that is to use a unit parameter:

JASS:
function MyKillUnit takes unit u returns nothing
    // blah blah blah...
endfunction
 
Level 2
Joined
Jul 20, 2004
Messages
27
I get alot of errors on line 2. Undeclared function GetRectX, Undeclared function GetRectY, Cannot convert null to real, Cannot convert null to real

edit: now it works
 
Level 2
Joined
Jul 20, 2004
Messages
27
The native just means that it's one of the blizz-defined functions that you can use without having to define them yourself.

And if you still don't get what parameters/arguments are for, the simplest way to explain them is that they are used to pass information into a function. For example, in the product function, to compute a product the function needs to know what two factors to multiply together (it can't just multiply two random numbers and hope those two numbers it picked were the ones you wanted), so you use parameters to pass that info into the function.

Likewise, if you were going to make your own KillUnit() function, you would want to know WHICH unit to kill. The way to do that is to use a unit parameter:

JASS:
function MyKillUnit takes unit u returns nothing
    // blah blah blah...
endfunction

Could someone please give a working example which requires a Takes and Returns, that is actually used for something?

You keep refering to termonology I have no idea grasp off, as I'm not a programmer nor have an experience in programming.

With a working example, I reverse-engineer triggers of my own, and hopefully with time get better.

Edit: Alright Eleandor. Now it works. However it doens't require any takes or returns. So I guess now I'm asking myself, when do I need to put anything in them? If you write "argument, return value, or compute" I'll bust a nut :(
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
To give the mathematical example:
y = x³

we call "x" the function's argument.
Whenever we speak of "arguments" or "parameters", we mean everything inbetween "takes" and "returns".
In MyFunction example, "real x" is the only argument of the function.

Ok, here's an example. Be warned though, since you're new to jass the contents of the function may look overwhelming. But mainly look at the takes / returns.

JASS:
function Dialog takes player id, string prompttext, string options returns integer
    local dialog prompt = DialogCreate()
    local trigger array trig
    local string current=""
    local integer n=0
    local integer i=0
    local integer result=0

    call DialogSetMessage( prompt, prompttext )
    set trig[0] = CreateTrigger()
    call TriggerRegisterDialogEvent( trig[0], prompt )
    set options=options+";"
    loop
        exitwhen SubString(options,i,i+1) == ""
        if SubString(options,i,i+1) == ";" then
            set n=n+1
            set trig[n] = CreateTrigger()
            call TriggerRegisterDialogButtonEvent( trig[n], DialogAddButton( prompt, current,0 ) )
            set current=""
        else
            set current=current+SubString(options,i,i+1)
        endif
        set i=i+1
    endloop

    if n==0 then
        return 0
    endif

    call DialogDisplay( id, prompt, true)
    loop
        exitwhen GetTriggerEvalCount(trig[0]) > 0
        call TriggerSleepAction(0)
    endloop
    set i=0
    call DialogClear(prompt)
    call DialogDestroy(prompt)
    set prompt=null
    loop
        exitwhen i>n
        if GetTriggerEvalCount(trig[i]) > 0 then
            set result = i
        endif
        call DestroyTrigger(trig[i])
        set trig[i] = null
        set i=i+1
    endloop
    
    return result
endfunction

function Trig_DialogExampleAction takes nothing returns nothing
    local integer option = Dialog(Player(0), "Choose an option", "Yes;No;I don't know")
    if option == 1 then
        call BJDebugMsg("Yeah!")
    elseif option == 2 then
        call BJDebugMsg("No?")
    else
        call BJDebugMsg("You sure you don't know?")
    endif
endfunction
//===========================================================================
function InitTrig_DialogExample takes nothing returns nothing
    set gg_trg_DialogExample = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_DialogExample, Player(0), "dialog", true )
    call TriggerAddAction( gg_trg_DialogExample, function Trig_DialogExampleAction )
endfunction

If you want to test this in your map, you must name your trigger "DialogExample".

So basically you can see the function Dialog takes player id, string prompttext, string options
It's purpose is to show a dialog box to the specified player, reading prompttext and having the options you specified at the "options" parameter.
In our example, we have the options "Yes;No;I don't know", that's 3 options.
The return value of "Dialog" is
1 if you pressed "Yes"
2 if you pressed "No"
3 if you pressed "I don't know".

Remember that in GUI creating such a dialog involves making a trigger for creating a dialog & showing it, and then 1 trigger for each possible option you can click.

In jass, someone called Vexorian wrote an easy jass function that does exactly the same, but instead of creating multiple large triggers in GUI, all you need to do is use the "Dialog" function properly.

Basically, the function Trig_DialogExampleAction shows an example of using the Dialog function. As you can see, it's 6 lines of code, which would be 4 triggers in GUI. This is the power of jass: you can create 1 function that automizes everything related to dialogs, and whenever you need to show another dialog, all you need to do is write a few lines of code and you have it.

Vexorian wrote the "Dialog" function, and he uses 3 arguments: a player, a prompttext and a list of options.
He did so because he wanted to make the dialog function a flexible function. Using those 3 arguments, you can create ANY dialog you want with 1 line of code, instead of creating lots of GUI triggers. And that's the strength of jass: you can use arguments and returnvalues to make flexible functions.
 
Level 2
Joined
Jul 20, 2004
Messages
27
I had to change a few things in your code to make it work. I understand why JASS is greater then GUI, which is why I wanna use it. However, once again I'm none the wiser cause this example is way way beyond me. I grateful for your attempts as assisting, but so far I have only gotten more confused.

JASS:
function Function2 takes nothing returns string
 local string message = SomeFunction("Hello World")
endfunction

function SomeFunction takes string returns nothing
 local string a message
    a message = 'Something is wrong'
    call DisplayTextToPlayer(Player(0), 0, 0, a message)
endfunction

Theres suppose to be 5 errors in this example, if you could show me which, maybe I'll have and epiphany

Edit: thanks for the example of the dialog however, I'm sure in 1-5 days I'll be doing great things with it. Also just out of pure curiousity. Is learning JASS a good way of getting into programming?
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
JASS:
function Function2 takes nothing returns string // where does it return a string? 
 local string message = SomeFunction("Hello World") // SomeFunction should be ABOVE this to be called. SomeFunction also does'nt return a string
endfunction

function SomeFunction takes string returns nothing
 local string a message // the string is called 'a', what's that message?
    a message = 'Something is wrong' // again, what's that message? strings are encapsulated by "'s, not ''s (" not ')
    call DisplayTextToPlayer(Player(0), 0, 0, a message) // and yet again, what is that message doing there?
endfunction
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
The 5 errors are:
1) Function2 returns string, however in the function's body it never actually returns a string. Suggested fix:
function Function2 takes nothing returns nothing

2) The argument in SomeFunction does not use an identifier.
function SomeFunction takes string returns nothing should be "takes string somestring", where "somestring" is the identifier, which is used to identify the parameter. Without specifying an identifier you have no way of identifying (and thus using) the parameter in your function.
3) "a message" is not a valid identifier. Identifiers are not allowed to have spaces in them. Suggested fix: local string amessage

4) Asigning variables requires the keyword "set". a message = 'something is wrong' should become set amessage = 'something is wrong'

5) 'something is wrong' is not a string. Strings use double quotation marks instead of single quotation marks. Single quotation marks are used to write ASCII integers. Suggested fix: set amessage = "something is wrong"

6) In jass, a function can only use other functions if those functions have been defined BEFORE. Therefor, if Function2 uses SomeFunction, SomeFunction must be defined FIRST.
JASS:
function SomeFunction takes string somestring returns string
    local string amessage
    set amessage = "Something is wrong"
    call DisplayTextToPlayer(Player(0), 0, 0, amessage)
    return amessage
endfunction

function Function2 takes nothing returns nothing
    local string message = SomeFunction("Hello World")
endfunction

Lol, that's 6 errors :D

Edit: thanks for the example of the dialog however, I'm sure in 1-5 days I'll be doing great things with it. Also just out of pure curiousity. Is learning JASS a good way of getting into programming?

It works in 2 directions: jass can get you into programming, and having experience in programming allows you to learn jass fast.
 
Level 2
Joined
Jul 20, 2004
Messages
27
The 5 errors are:
1) Function2 returns string, however in the function's body it never actually returns a string. Suggested fix:
function Function2 takes nothing returns nothing

2) The argument in SomeFunction does not use an identifier.
function SomeFunction takes string returns nothing should be "takes string somestring", where "somestring" is the identifier, which is used to identify the parameter. Without specifying an identifier you have no way of identifying (and thus using) the parameter in your function.
3) "a message" is not a valid identifier. Identifiers are not allowed to have spaces in them. Suggested fix: local string amessage

4) Asigning variables requires the keyword "set". a message = 'something is wrong' should become set amessage = 'something is wrong'

5) 'something is wrong' is not a string. Strings use double quotation marks instead of single quotation marks. Single quotation marks are used to write ASCII integers. Suggested fix: set amessage = "something is wrong"

6) In jass, a function can only use other functions if those functions have been defined BEFORE. Therefor, if Function2 uses SomeFunction, SomeFunction must be defined FIRST.
JASS:
function SomeFunction takes string somestring returns string
    local string amessage
    set amessage = "Something is wrong"
    call DisplayTextToPlayer(Player(0), 0, 0, amessage)
    return amessage
endfunction

function Function2 takes nothing returns nothing
    local string message = SomeFunction("Hello World")
endfunction

Lol, that's 6 errors :D



It works in 2 directions: jass can get you into programming, and having experience in programming allows you to learn jass fast.

Alright that made sense, except maybe the part about "somestring", that part remains a mystery to me. However theres alot of the concepts you just stated which wasn't at all clearified in the tutorial, but was assumed that the reader knew. This seem to be a wide problem in that tutorial and its agrovating

However an most importantly, I don't see the reasoning being making it that complex. To be honest if there were an event to trigger that trigger, I don't even know what it would it. Would it display Hello World? or Something is wrong? or both?
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
Ok, I'll try to explain the somestring part.

This is a function definition:

JASS:
function identifier takes parameterlist returns type
functionbody
endfunction

identifier
An identifier = a string that starts with a character and is followed by only alphanumeric characters. For example:
amessage is a valid identifier.
a message is invalid because it contains spaces.
x2 is valid
2x is invalid (starts with a number)

parameterlist
A parameterlist is defined as:
Comaseparated parameterlist. i.e.
parameter, parameter, parameter, ...
You can use nothing to specify an empty parameterlist.

parameter
A parameter is defined as:
type identifier

type
A type is a single keyword that denotes a variable type. There are various variabletypes:
real

integer

boolean

string

unit

etc. etc.
nothing is used when no type is returned.

functionbody
The functionbody is the body of the function. Everything inside the functionbody will be executed when the function is called.

The purpose of an identifier is to identify something, i.e. in order to be able to use it. For example, my name is Eleandor. If you wouldn't know my name, there would be no way for you to be able to ask me something.

Every function starts with an identifier, in order to be able to identify the function.

Every parameter ends with an identifier, in order to be able to identify the parameter. It starts with a type in order to be able to identify the type of the parameter.
A parameter can be used inside the functionbody. So if I have MyFunction take real x, then I can use "x" to identify this parameter. For this reason, a parameter needs an identifier, in order to be able to identify it.

If I have
JASS:
function MyFunction takes real x returns real
    return x*x*x
endfunction

// somewhere else
local real omgwtfbbq = 5.0
local real y = MyFunction(omgwtfbbq)
Then when you reach the line y = MyFunction(omgwtfbbq), MyFunction will run taking x = omgwtfbbq. In other words: MyFunction takes real x, and upon calling MyFunction(omgwtfbbq), it will take omgwtfbbq, and whenever you use "x" inside MyFunction, it will be "replaced" by omgwtfbbq.

Thus, in-game, MyFunction temporarily becomes:
JASS:
function MyFunction takes real omgwtfbbq returns real
    return omgwtfbbq*omgwtfbbq*omgwtfbbq
endfunction

(The last thing I said is not completely correct, but it shows more or less what happens)
 
Level 2
Joined
Jul 20, 2004
Messages
27
Ended up making another reply. That wasn't really my intend but I'll leave it as it is. This reply is basicly fill now :(
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
a real is a decimal number, for example:
0.05
-123.001

an integer is a whole number, for example:
1
-15

Like I said in my above reply: why string something? it doens't make sense just to write "somestring"
You can write anything instead of "somestring". As long as it's a valid identifier. This identifier is used to identify this parameter, i.e. to be able to use it. In that example, I called the string parameter "somestring". This means that whenever I use "somestring", I will be using that stringparameter.

I could go on, but I think you get the picture. PLAIN ENGLISH! I won't understand programming babla. Please remember your dealing with someone who couldn't make a calculator code if his life depended on it.
I'm trying real hard :D
Well, I tried explaining what I mean when I say "identifier" or "parameter". I tried explaining the terminology in plain english...
Try rereading my previous post and tell me if there's still something you don't understand.
 
Level 2
Joined
Jul 20, 2004
Messages
27
Ok, I'll try to explain the somestring part.

This is a function definition:

JASS:
function identifier takes parameterlist returns type
functionbody
endfunction

identifier
An identifier = a string that starts with a character and is followed by only alphanumeric characters. For example:
amessage is a valid identifier.
a message is invalid because it contains spaces.
x2 is valid
2x is invalid (starts with a number)

parameterlist
A parameterlist is defined as:
Comaseparated parameterlist. i.e.
parameter, parameter, parameter, ...
You can use nothing to specify an empty parameterlist.

parameter
A parameter is defined as:
type identifier

type
A type is a single keyword that denotes a variable type. There are various variabletypes:
real

integer

boolean

string

unit

etc. etc.
nothing is used when no type is returned.

functionbody
The functionbody is the body of the function. Everything inside the functionbody will be executed when the function is called.

The purpose of an identifier is to identify something, i.e. in order to be able to use it. For example, my name is Eleandor. If you wouldn't know my name, there would be no way for you to be able to ask me something.

Every function starts with an identifier, in order to be able to identify the function.

Every parameter ends with an identifier, in order to be able to identify the parameter. It starts with a type in order to be able to identify the type of the parameter.
A parameter can be used inside the functionbody. So if I have MyFunction take real x, then I can use "x" to identify this parameter. For this reason, a parameter needs an identifier, in order to be able to identify it.

If I have
JASS:
function MyFunction takes real x returns real
    return x*x*x
endfunction

// somewhere else
local real omgwtfbbq = 5.0
local real y = MyFunction(omgwtfbbq)
Then when you reach the line y = MyFunction(omgwtfbbq), MyFunction will run taking x = omgwtfbbq. In other words: MyFunction takes real x, and upon calling MyFunction(omgwtfbbq), it will take omgwtfbbq, and whenever you use "x" inside MyFunction, it will be "replaced" by omgwtfbbq.

Thus, in-game, MyFunction temporarily becomes:
JASS:
function MyFunction takes real omgwtfbbq returns real
    return omgwtfbbq*omgwtfbbq*omgwtfbbq
endfunction

(The last thing I said is not completely correct, but it shows more or less what happens)

Alright, I'd like to know what all the types are, and what makes them what they are. As for your example, I dont understand that. Should it be:

JASS:
function MyFunction takes real x returns real
    return x*x*x
endfunction

// somewhere else
local real omgwtfbbq = 5.0
local real x = MyFunction(omgwtfbbq)

That makes sense to me anyway. Also now I got the point in the tutorial where the if/then/else are explained, and that is just total nosense to me. Also my Syntax Check says theres errors in it. Would you mind having a look and correct it?

JASS:
function Set_Damage_To_Unit takes integer level returns real
    // this example will set the damage amount in a spell, according to a level
    // (unit level, ability level, etc)
    local real dam
    if level < 2 then
        set dam = 0
    elseif level == 1 then
        set dam = 50.
// probably you noticed the point after the number. this is used to indicate the number is a real one.
// Is a good programming practice to do that in order to avoid annoying debugging mistakes.
    elseif level == 2 then
        set dam = 63.
    elseif level == 3 then
        set dam = 125.
    else
        set dam = 125. + 50. * (level - 1)
    return dam
endfunction


Edit:
You can write anything instead of "somestring". As long as it's a valid identifier. This identifier is used to identify this parameter, i.e. to be able to use it. In that example, I called the string parameter "somestring". This means that whenever I use "somestring", I will be using that stringparameter.

What I mean is, why somestring in this instance? I mean that doesn't indentify anything in that trigger. Had you said "takes string amessage returns string" it would have made sense, cause I can see that that is used in the functionbody. The only other way of rationalize it, is to say that ANYTHING can be writen there, with no direct link to anything, which I can accept, but it still doesn't make sense why it is that way
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
Alright, I'd like to know what all the types are, and what makes them what they are. As for your example, I dont understand that. Should it be:

Taken from blizzard's common.j:
JASS:
type integer
type real
type boolean
type string
type code
type handle

type event              extends     handle  // a reference to an event registration
type player             extends     handle  // a single player reference
type widget             extends     handle  // an interactive game object with life
type unit               extends     widget  // a single unit reference
type destructable       extends     widget
type item               extends     widget
type force              extends     handle
type group              extends     handle
type trigger            extends     handle
type triggercondition   extends     handle
type triggeraction      extends     handle
type timer              extends     handle
type location           extends     handle
type region             extends     handle
type rect               extends     handle
type boolexpr           extends     handle
type sound              extends     handle
type conditionfunc      extends     boolexpr
type filterfunc         extends     boolexpr
type unitpool           extends     handle
type itempool           extends     handle
type race               extends     handle
type alliancetype       extends     handle
type racepreference     extends     handle
type gamestate          extends     handle
type igamestate         extends     gamestate
type fgamestate         extends     gamestate
type playerstate        extends     handle
type playergameresult   extends     handle
type unitstate          extends     handle

type eventid            extends     handle
type gameevent          extends     eventid
type playerevent        extends     eventid
type playerunitevent    extends     eventid
type unitevent          extends     eventid
type limitop            extends     eventid
type widgetevent        extends     eventid
type dialogevent        extends     eventid
type unittype           extends     handle

type gamespeed          extends     handle
type gamedifficulty     extends     handle
type gametype           extends     handle
type mapflag            extends     handle
type mapvisibility      extends     handle
type mapsetting         extends     handle
type mapdensity         extends     handle
type mapcontrol         extends     handle
type playerslotstate    extends     handle
type volumegroup		extends		handle
type camerafield        extends     handle
type camerasetup        extends     handle
type playercolor        extends     handle
type placement          extends     handle
type startlocprio       extends     handle
type raritycontrol      extends     handle
type blendmode          extends     handle
type texmapflags        extends     handle
type effect             extends     handle
type effecttype         extends     handle
type weathereffect      extends     handle
type fogstate           extends     handle
type fogmodifier        extends     handle
type dialog             extends     handle
type button             extends     handle
type quest              extends     handle
type questitem          extends     handle
type defeatcondition    extends     handle
type timerdialog        extends     handle
type leaderboard        extends     handle
type trackable          extends     handle
type gamecache          extends     handle
(p.s. don't worry about all the "extends handle" stuff, when you're ready for it you'll understand that)

And no, it shouldn't be like that.
A parameter is defined as: type identifier
For example: real x (with "real" the type, and "x" the identifier)
x can thus be seen as the "name" of the parameter.
The whole point of giving a parameter a name is in order to be able to use this parameter inside the function.
So to use the MyFunction example again:
MyFunction takes real x returns real. Thus, no matter what parameter you will specify when you call MyFunction, MyFunction will name that parameter "x".
So if we say: local real y = MyFunction(omgwtfbbq) then MyFunction renames "omgwtfbbq" into "x", and will calculate omgwtfbbq³. Then when MyFunction returns, the value omgwtfbbq³ is returned, and is assigned to the real variable that we've named y. That real variable is named y simply because we want to name it y. It does NOT need to be called "x" because MyFunction also uses x.

Also my Syntax Check says theres errors in it. Would you mind having a look and correct it?
Sure. You forgot "endif" at the end of your whole if/then/else part.
JASS:
function Set_Damage_To_Unit takes integer level returns real
    // this example will set the damage amount in a spell, according to a level
    // (unit level, ability level, etc)
    local real dam
    if level < 2 then
        set dam = 0
    elseif level == 1 then
        set dam = 50.
// probably you noticed the point after the number. this is used to indicate the number is a real one.
// Is a good programming practice to do that in order to avoid annoying debugging mistakes.
    elseif level == 2 then
        set dam = 63.
    elseif level == 3 then
        set dam = 125.
    else
        set dam = 125. + 50. * (level - 1)
    endif
    return dam
endfunction
 
Level 2
Joined
Jul 20, 2004
Messages
27
I'm still totally lost, but try a bit on my own now.

However you didn't answer my question. Isn't amessage the correct thing to write in place of somestring?
 
Level 2
Joined
Jul 20, 2004
Messages
27
Exercise.


Create a function which displays all the numbers which can divide perfectly a given number. The function should be in that way:
JASS:
function DivideNumbers takes integer n returns nothing
    // your code here
endfunction

For exaple, if we make
JASS:
DivideNumbers(12)
we should see on the game screen something like this:

1, 2, 3, 4, 6, 12

I have no clue how to accomplish this. I'm really close to giving up now
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
It's not thát hard ;)

First we need to find a good formula to see if a divides b.

We're using integers (whole numbers), not reals!
If (b/a)*a is equal to b then a is a good divider.
For example:
(4/2)*2 equals 4, thus 2 is a good divider
(4/3)*3 equals 3, thus 3 is not a good divider

Note: 4/3 in integer calculus is equal to 1, because we're using integers, not reals!
So now we have a good formula to find all correct numbers.

Now we need to loop through all possible numbers that can be a divider and check if it's a divider. So for example if we want to know what numbers divide 12, we loop from 1 to 12.

JASS:
function DivideNumbers takes integer n returns nothing
    local integer i = 1 // i is a looping index
    loop
        exitwhen i > n // if i is larger than n, it cannot possibly be a divider.
        if (n/i)*i == n then // if this condition is true, then the number is a divider
            call BJDebugMsg(I2S(i)) // We display the number i. Note, we need to convert i to a string first, we do this through using I2S(i)
        endif
        set i = i + 1
    endloop
endfunction

Maybe try a different tutorial to teach you the basics... The code alone of the tutorial you're currently reading contains errors, so...
 
Level 2
Joined
Jul 20, 2004
Messages
27
Well its the tutorial on this site... And can't really find others.

Btw why I2S? what does it stand for?

Otherwise it made total since, although I don't seem to be able to add an event to it, so I can actually see it working

JASS:
function Trig_DivideNumbers_Actions takes integer n returns nothing
    local integer i = 1
    loop
        exitwhen i > 12
        if (12/i)*i == n then
            call BJDebugMsg(I2S(i))
        endif
        set i = i + 1
    endloop
endfunction
//===========================================================================
function InitTrig_DivideNumbers takes nothing returns nothing
    set gg_trg_DivideNumbers = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_DivideNumbers, Player(0), "loop", true )
    call TriggerAddAction( gg_trg_DivideNumbers, function Trig_DivideNumbers_Actions )
endfunction

Why doesn't it work?
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
Well its the tutorial on this site... And can't really find others.
http://www.hiveworkshop.com/forums/jass-ai-scripts-tutorials-280/jass-a-concise-introduction-62279/
http://www.hiveworkshop.com/forums/...tter-understanding-reviewed-purplepoot-85170/
http://www.hiveworkshop.com/forums/jass-ai-scripts-tutorials-280/learning-jass-104271/
http://www.hiveworkshop.com/forums/jass-ai-scripts-tutorials-280/jass-tutorial-16456/


Btw why I2S? what does it stand for?
I2S converts an integer to a string; BJDebugMsg() requires a string, which is why we need to convert the integer i to a string.

Otherwise it made total since, although I don't seem to be able to add an event to it, so I can actually see it working
The event works, the problem is your action:
The function provided in TriggerAddAction needs to be like
function Trig_DivideNumbers_Actions takes nothing returns nothing

I suggest doing:
JASS:
function DivideNumbers takes integer n returns nothing
    local integer i = 1
    loop
        exitwhen i > 12
        if (12/i)*i == n then
            call BJDebugMsg(I2S(i))
        endif
        set i = i + 1
    endloop
endfunction

function Trig_DivideNumbers_Actions takes nothing returns nothing
    call DivideNumbers(36)
endfunction
//===========================================================================
function InitTrig_DivideNumbers takes nothing returns nothing
    set gg_trg_DivideNumbers = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_DivideNumbers, Player(0), "loop", true )
    call TriggerAddAction( gg_trg_DivideNumbers, function Trig_DivideNumbers_Actions )
endfunction
 
Level 8
Joined
Aug 4, 2006
Messages
357
should be:
JASS:
function DivideNumbers takes integer n returns nothing
    local integer i = 1
    loop
        exitwhen i > n
        if (n/i)*i == n then
            call BJDebugMsg(I2S(i))
        endif
        set i = i + 1
    endloop
endfunction

function Trig_DivideNumbers_Actions takes nothing returns nothing
    call DivideNumbers(36)
endfunction
//===========================================================================
function InitTrig_DivideNumbers takes nothing returns nothing
    set gg_trg_DivideNumbers = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_DivideNumbers, Player(0), "loop", true )
    call TriggerAddAction( gg_trg_DivideNumbers, function Trig_DivideNumbers_Actions )
endfunction
 
Level 8
Joined
Nov 9, 2008
Messages
502
So...have I got it right if I were to say every function needs to take information needed for its purpose and that each function has designated parameters one must learn to use the function? Do these parameters ever change within functions?
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
Yes, that's how you could describe parameters. They're information you send to the function. CreateUnit needs information related to the player, unit-type, X/Y coordinates and facing angle. Thus, CreateUnit takes player, integer, real, real, real.

These parameters can be modified inside the function. However, modifying the parameters only modify them inside the function. Outside the function, the variables you've passed to the function remain the same.
 
Level 8
Joined
Nov 9, 2008
Messages
502
Hmm suddenly jass looks a lot simpler.

JASS:
//===========================================================================
function InitTrig_DivideNumbers takes nothing returns nothing
    set gg_trg_DivideNumbers = CreateTrigger( )
    call TriggerRegisterPlayerChatEvent( gg_trg_DivideNumbers, Player(0), "loop", true )
    call TriggerAddAction( gg_trg_DivideNumbers, function Trig_DivideNumbers_Actions )
endfunction

What is this part?
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
Every function that is called "InitTrig" is a function that automatically runs when the map is loading. Its purpose is to initialize the trigger.

This initialization is done through creating a new trigger (set gg_trg_DivideNumbers = CreateTrigger()) and then registering an event, and adding an action. Obviously, TriggerAddCondition adds a condition, and triggers are not limited to only 1 action function. This as opposed to GUI where you can only add 1 action or 1 condition.

Similar effects can be achieved in vjass through using initializers in libraries, scopes & structs.
 
Level 8
Joined
Nov 9, 2008
Messages
502
Ok so if I were to right my own function it would be something like:

1.Body
2.Assign actions (call the body)?
3.The init (write events and conditions)?

JASS:
set gg_trg_DivideNumbers = CreateTrigger( )
is always the same (except name)?

And you're telling me none of that is needed if vJass is used?
 
Status
Not open for further replies.
Top