• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Newbie syntax questions

Status
Not open for further replies.
Level 12
Joined
Feb 13, 2009
Messages
386
Greetings, after a lot of time being away I finally decided to return to WC3 modding but this time I try to not to touch GUI and I've started to learn JASS.
That's the story.

Now to facts.

I want to make a trigger which loops though players, checks if they are present and gives "selector units" (currently wisps) to buy heroes from a tavern.

I have two syntax errors regarding function( parameter ), without ( parameter ) it produces no syntax errors but then it doesn't pass the parameter "i" to the function. I feel like I am doing something really stupid here, but can you answer me, why can't I use a parameter here if I specify "takes integer i" in the functions'?

JASS:
//===========================================================================
// Trigger: GiveSelectors
//===========================================================================
function Trig_GiveSelectors_Conditions takes integer i returns boolean
    if ( not ( GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING ) ) then
        return false
    endif
    return true
endfunction

function Trig_GiveSelectors_Actions takes integer i returns nothing
    call CreateNUnitsAtLocFacingLocBJ( 1, 'ewsp', Player(i), GetRectCenter(gg_rct_HumansSpawn), GetRectCenter(GetPlayableMapRect()) )
endfunction

//===========================================================================
function InitTrig_GiveSelectors takes nothing returns nothing
    local integer i = 0
    set gg_trg_GiveSelectors = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_GiveSelectors, 1.00 )
    loop
    exitwhen i > 9
(!)     call TriggerAddCondition( gg_trg_GiveSelectors, Condition( function Trig_GiveSelectors_Conditions(i) ) )
(!)     call TriggerAddAction( gg_trg_GiveSelectors, function Trig_GiveSelectors_Actions(i) )
    set i = i + 1
    endloop
endfunction

//===========================================================================

(!) indicate 2 strings with errors (there're not present in the actual jass text).
 
Level 12
Joined
Feb 13, 2009
Messages
386
Damn it... Does it mean that the functions I use within the InitTrig_GiveSelectors function use all the local variables of InitTrig_GiveSelectors automatically? I thought you have to pass the desired variables as parameters.
 
Level 12
Joined
Feb 13, 2009
Messages
386
Let me rephrase the question and please try to ask it carefully, or I could misunderstand it...

For example I have this:
JASS:
function MyMainFunction takes nothing returns nothing
    local integer MyVar
    call TriggerAddCondition( gg_trg_MyTrigger, Condition( function Trig_Mytrigger_Conditions ) )
endfunction

Does it mean that if Trig_Mytrigger_conditions is defined as:
JASS:
function Trig_MyTrigger_Conditions takes integer MyVar returns boolean
then it automatically "takes integer MyVar" and any other variable it takes from the MyMainFunction without a need to specify it as a parameter?

I have some kind of a habit from C programming language: if a function is defined to take some parameter and the parameter is not defined in function(parameter), then it's considered to be NULL.

Also, how do I make a function to take multiple variables in a right way?
JASS:
function AwesomeSauce takes integer MyVar1, string MyVar2, boolean Bool1 returns nothing
^^ like this?

P.S. And a bit offtopic question, but it still relates to JASS. How should I save my scripts to a map file? I've extracted war3map.j and changed it, but JassCraft gives me an access violation related to SFmpq.dll and doesn't want to save.
 
Level 18
Joined
Feb 28, 2009
Messages
1,970
CnP into converted trigger.

EDIT: This trigger shoud work
JASS:
//===========================================================================
// Trigger: GiveSelectors
//===========================================================================
function Condition1 takes player WhichPlayer returns boolean
    return GetPlayerSlotState (WhichPlayer) == PLAYER_SLOT_STATE_PLAYING
endfunction

function Actions takes nothing returns nothing
    local integer i = 0
    local player p
    
    loop 
            exitwhen i > 9
        set p = Player (i)
            if Condition1 (p) then
                call CreateUnitAtLoc (p, 'hpea', GetRectCenter (GetPlayableMapRect()), 0.)
            endif
        set i = i+1
            
    endloop
    set p =null
endfunction

//===========================================================================
function InitTrig_GiveSelectors takes nothing returns nothing
    set gg_trg_GiveSelectors = CreateTrigger(  )
    call TriggerRegisterTimerEventSingle( gg_trg_GiveSelectors, 1.00 )
    call TriggerAddAction (gg_trg_GiveSelectors, function Actions)
endfunction

//===========================================================================
 
Level 14
Joined
Nov 18, 2007
Messages
1,084
If you really intend to use Jass, you are also able to optimize your code:
JASS:
//===========================================================================
// Trigger: GiveSelectors
//===========================================================================
function Actions takes nothing returns nothing
    local integer i = 0
    loop 
        exitwhen i > 9
        if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING then
            call CreateUnit(Player(i), 'hpea', GetRectCenterX(bj_mapInitialPlayableArea),GetRectCenterY(bj_mapInitialPlayableArea) , 0.)
        endif
        set i = i+1            
    endloop
endfunction
//===========================================================================
function InitTrig_GiveSelectors takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterTimerEvent( t, 1.00, false)
    call TriggerAddAction (t, function Actions)
endfunction
//===========================================================================
This code gets rid of the usage of BJ's, functions that call the natives to perform, and locations, which can cause leaks in your code if not handled properly.
You should also notice that you can directly implement Condition1 into the if-statement.
I also get rid of player p as it was used only two times in the function, but that's pretty much optional.
Though it wasn't done in my example, you can alternatively use TriggerAddCondition and made the function Actions act like one. The changes would be
JASS:
function Actions takes nothing returns boolean
with
return false right before endfunction
and use call TriggerAddCondition (t, Condition(function Actions)) instead of TriggerAddAction. The reason for doing so is that trigger conditions are faster than trigger actions.
You may also want to learn vJass to make coding easier.
 
Status
Not open for further replies.
Top