• 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] Jass Beginner Help

Status
Not open for further replies.
Level 1
Joined
Nov 10, 2007
Messages
1
Hey guys, I'm starting to learn jass. I do know some programming language so the whole logic/variable system for the most part makes sense to me although I am somewhat out of practice. What I don't know exactly is how to relate my data to warcraft. For instance:
JASS:
globals
trigger gg_trg_random_positions
endglobals
function IntTrig_position_numbers takes nothing returns nothing
local integer counter_1=1
local integer counter_2=2
local integer counter_3=3
local integer counter_4=1
local integer array position_numbers
local real array random_numbers
loop
    exitwhen random_numbers[11] >= .01
    set random_numbers[counter_1] = GetRandomReal(0.01,10000.00) 
    set counter_1 = counter_1+1
endloop
set counter_1=1
loop
    exitwhen counter_4==12
    loop
        exitwhen counter_3==14
        if  random_numbers[counter_1] > random_numbers[counter_2]then
            set counter_2=counter_3
            set counter_3=counter_3+1
        else                
            set counter_1=counter_3
            set counter_3=counter_3+1
        endif
    endloop
    if  random_numbers[counter_1] >= random_numbers[counter_2]then
    set random_numbers[counter_1]=0
    set position_numbers[counter_1] = counter_4
    else
    set random_numbers[counter_2]=0
    set position_numbers[counter_2] = counter_4
    endif
    set counter_3=3
    set counter_1=1
    set counter_2=2
    set counter_4=counter_4+1       
endloop
    set counter_1=1
    loop
        exitwhen counter_1==12
        call BJDebugMsg (I2S(position_numbers[counter_1]))
        set counter_1=counter_1+1 
    endloop
    


endfunction

function InitTrig_random_positions takes nothing returns nothing
    set gg_trg_random_positions = CreateTrigger()
    call TriggerAddAction( gg_trg_random_positions, function IntTrig_position_numbers ) 
endfunction

I was able to make this after reading a couple tutorials for jass. There might be a way to do what I did in about 5 lines, but whatever. Anyway, ultimaetly what this does is makes the 'local integer array position_numbers' full of numbers 1-11, however, there are no repeats, and in a random order.

What I want to know is how to "attach" one of the array variables to a player. For instance 'position_numbers[1] be reds' and 'position_numbers[2] be blues'. This is probably fairly simple but as I said, I don't really know how to relate the data to warcraft 3 too well. From there depending on the value of the integer array, I'd like to assign them a position. For instance, whoever has number 11 gets to be leader, and whoever gets number 1 is a douche, you get the idea. I know it's hard to understand what exactly I want so yeah, but please try your best.
Figured this out for the most part, but please keep on reading

My second question is why exacly do we need parameters (the words after take and return). I understand what they are, but from reviewing tutorials and looking at scripts, I don't exactly see the point. From what I can tell (this is probably wrong so correct me) you can just get by them by having local variables and setting variables within the function.

Also, the call function can kind of act as an event right? As in it can call what time of day it is etc. to see if the function should proceed?

Lastly, I am currently on a project that is definently out of ability level at the moment, and although I am a fast learner when it comes to this these types of things I probably will be asking for a lot of help in the next couple weeks, so please bear with me. Thank you.
 
Last edited:
2. If you want to call a function, say "KillUnit"... It must know which unit to kill, right? It can't read your mind... :p

So you specify what the unit is as a parameter. Without this, we would have to make a ton of functions for specific units, which takes a long time.

3. Call just orders a function to occur, unless you say "call TriggerRegisterBlahblahEvent(...)". To check if something works, use conditions or if/then/elses. Eg:
JASS:
    if ImHungry then
        call EatPie(ME)
    else
        call PolledWait(1 hour)
        call EatPie(ME)
    endif
 
Level 11
Joined
Aug 25, 2006
Messages
971
I know you said you figured this out. But you said 'for the most part' So just encase:
Ok to make a player refer to a boolean in an array do the following.
JASS:
globals
       boolean array Kal //declare array
endglobals
function DoSomething takes player Who returns nothing
    //Now say we wanted the value of this array connected to player 'Who' to be = true
    set Kal[GetPlayerId(Who)] = true //We use GetPlayerId to turn the player into a number
    //Player red = 0 Player Blue = 1 Player Teal = 2 (etc)
endfunction

Note the array can be any type you want it to be. I used booleans just to show you.
 
Level 20
Joined
Apr 22, 2007
Messages
1,960
Functions take and returns arguments because that's how they work. Let's take the Sin native for example (oh also, 'natives' are functions found within common.j, which is the jass base function database. natives are always faster than their regular function counterparts, often found in blizzard.j. People tend to avoid blizzard.j functions (which we call BJ's), because they are generally slow and useless, but there are some useful ones).
JASS:
native Sin      takes real radians returns real

It takes a real and returns a real, so you can do things like:
JASS:
function YourFunc takes nothing returns nothing
    local real r=Sin(bj_PI)
    call BJDebugMsg( R2S( r ) )
endfunction
We're storing the result of Sin(pi) into a local real r, and then we're displaying that value to all players.
You probably noticed the bj prefixes. The small-case bj_ is always before a bj_ global variable. There are MANY bj_ globals in blizzard.j, some which may prove to be very useful. bj_ globals aren't bad at all, they're rather good, since they don't involve direct function calling. BJ functions, however, are generally bad. As you can see, we are using the BJDebugMsg() function, which is a BJ (a function from blizzard.j), and is slow, but it's often used for debugging reasons (thus the name DebugMsg). It's also alot easier to write than call DisplayTextToForce(GetPlayersAll(),0.0,0.0,"Your string here"), which does the exact same thing...

We could also directly imply the returned value of a function in a condition! Let's say we have a function that acts as a condition:
JASS:
function YourCondition takes unit u returns boolean // See we're returning a boolean value.
    return GetUnitState(u,UNIT_STATE_LIFE)>100.0
endfunction
This will simply check if the current HP of the taken unit is greater than 100.0. Let's try to add that function directly to an if-statement:
JASS:
function YourCondition takes unit u returns boolean // See we're returning a boolean value.
    return GetUnitState(u,UNIT_STATE_LIFE)>100.0
endfunction

function SomeOtherFunction takes nothing returns nothing
    local unit u=GetTriggerUnit()
    
    if YourCondition(u) then
        call KillUnit(u)
    endif
    
    set u=null
endfunction
Since YourCondition() returns a boolean value (either true or false), that condition is correct. If the function returns true than the if statement will go as follows:
JASS:
if true then
// Do other actions...
endif
When the condition's result is "true", it will run the //Do other actions... part. When the condition's result is "false", it will skip it and continue. It might condition to an "else" statement or an "elseif" statement, or even maybe "endif", which would be the end of the whole if-statement.

That's only a really basic explanation of if's/conditions/returns.
 
Last edited:
Status
Not open for further replies.
Top