• 🏆 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] Help With Creating an Event in Jass

Status
Not open for further replies.
Level 1
Joined
Jul 6, 2012
Messages
5
I'm trying to make a function that detects when a key (for instance "down" key) is pressed by any player. I'm trying to write it in Jass, not in GUI. Can somebody help me write the code for one key as I am not that experienced with Jass.

I need the native GetTriggerPlayer to work as a reference in the code, what I have now isn't working so I won't bother posting it.
 
Level 6
Joined
May 13, 2009
Messages
260
JASS:
scope GigakarFunction initializer InitFunc

    function DoStuff takes nothing returns nothing
        call BJDebugMsg("I'm alive")
    endfunction
    
    function InitFunc takes nothing returns nothing
        local trigger keyPressTrg = CreateTrigger()
        
        call TriggerRegisterPlayerEvent(keyPressTrg, Player(0), EVENT_PLAYER_ARROW_DOWN_DOWN)
        call TriggerAddAction(keyPressTrg, function DoStuff)
    endfunction
endscope
 
Level 1
Joined
Jul 6, 2012
Messages
5
Thanks, that made things much simpler. However, I've stumbeled upon another problem. Why does this show two messages showing two "1" when I test the map as player 1? I want player 1 to n to be able to call function PressDown individually.

JASS:
scope PressDown initializer InitPressDown

    function PressDown takes nothing returns nothing
        set udg_Down[GetPlayerId(GetTriggerPlayer())+1] = true
        call BJDebugMsg("1")
    endfunction
    
    function InitPressDown takes nothing returns nothing
        
        local integer i = 0
        local trigger keyPressTrg = CreateTrigger()
        
        loop
        
        call TriggerRegisterPlayerEvent(keyPressTrg, Player(i), EVENT_PLAYER_ARROW_DOWN_DOWN)
        call TriggerAddAction(keyPressTrg, function PressDown)
        
        //loopcount
        
        set i = i + 1
        
        if i == 2 then
            exitwhen true
        endif
        
        endloop
        
    endfunction
endscope
 
Level 1
Joined
Jul 6, 2012
Messages
5
Hmm, I can't seem to get this to work:

JASS:
scope PressDown initializer InitPressDown

    function PressDown takes nothing returns nothing
        set udg_Down[GetPlayerId(GetTriggerPlayer())] = true
        call BJDebugMsg("1")
    endfunction
    
    function InitPressDown takes nothing returns nothing
        
        local integer i
        local trigger keyPressTrg = CreateTrigger()
        
        loop
        
        call TriggerRegisterPlayerEvent(keyPressTrg, Player(i), EVENT_PLAYER_ARROW_DOWN_DOWN)
        
        set i = i + 1
        
        exitwhen i == 9
        
        endloop
        
        call TriggerAddAction(keyPressTrg, function PressDown)
        
    endfunction
endscope
 
It's because i is uninitialized.

You should declare it like this:

local integer i = 0

edit
Also dude, I would really recommend indenting to make your code more readable :p

JASS:
scope PressDown initializer InitPressDown

    function PressDown takes nothing returns nothing
        set udg_Down[GetPlayerId(GetTriggerPlayer())] = true
        call BJDebugMsg("1")
    endfunction
    
    function InitPressDown takes nothing returns nothing
        local integer i
        local trigger keyPressTrg = CreateTrigger()
        
        loop
            call TriggerRegisterPlayerEvent(keyPressTrg, Player(i), EVENT_PLAYER_ARROW_DOWN_DOWN)
            set i = i + 1
            exitwhen i == 9
        endloop
        
        call TriggerAddAction(keyPressTrg, function PressDown)
    endfunction
endscope

That looks much better ^.^

And don't forget to null the trigger at the end of the InitPressDown function, else it will leak. (Well, triggers aren't much of a problem in these cases, but it's always better to null all handles.)

set keyPressTrg = null

You should also do this for all units, players, destructables, widgets, and every other handle.
(Well, technically, not every handle, just the things that extend the 'agent' in common.j and things that extend things that extend the agent and so on :p)

For example, images don't need to be nulled because they directly extend the handle and not the agent. (type image extends handle).
playerstates also don't need to be nulled.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
You should also do this for all units, players, destructables, widgets, and every other handle.
(Well, technically, not every handle, just the things that extend the 'agent' in common.j and things that extend things that extend the agent and so on :p)

For example, images don't need to be nulled because they directly extend the handle and not the agent. (type image extends handle).
playerstates also don't need to be nulled.

Just a guess, or it is something well tested ?
 
Status
Not open for further replies.
Top