• 🏆 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] Simple Code Problem

Status
Not open for further replies.
Level 24
Joined
Feb 28, 2007
Messages
3,480
Hello, I'm trying to create a trigger that registers when a unit enters a region, then a text should be displayed, the problem is, it isn't.
JASS:
globals
    rect TriggerRect = gg_rct_Test
    region TriggerRegion = CreateRegion()
    boolean FirstSlope = false
endglobals

function Actions takes nothing returns nothing
    call BJDebugMsg("Test")
endfunction

function Init takes nothing returns nothing
    local trigger Trig = CreateTrigger()
    call RegionAddRect(TriggerRegion,TriggerRect)
    call TriggerRegisterEnterRegion(Trig,TriggerRegion,null)
    call TriggerAddAction(Trig, function Actions)
endfunction
Any help why nothing happens when a unit enters the region would be appreciated, thanks.
 
Level 14
Joined
Nov 23, 2008
Messages
187
gg_rct_Test is equal to null, when you are trying to set value of TriggerRect variable.

JASS:
globals
    rect TriggerRect = null
    region TriggerRegion = CreateRegion()
    boolean FirstSlope = false
endglobals

function Actions takes nothing returns nothing
    call BJDebugMsg("Test")
endfunction

function Init takes nothing returns nothing
    local trigger Trig = CreateTrigger()
    // try this:
    set TriggerRect = gg_rct_Test
    // or specify required rect manually:
    // set TriggerRect = Rect(-200., -200., 200., 200.)
    call RegionAddRect(TriggerRegion,TriggerRect)
    call TriggerRegisterEnterRegion(Trig,TriggerRegion,null)
    call TriggerAddAction(Trig, function Actions)
endfunction
 
Level 14
Joined
Nov 23, 2008
Messages
187
It seems, that creating region directly (when defining global variable) screws up warcraft (I had fatal errors while working on this code). This variant is tested and should work:

JASS:
globals
    // rect TriggerRect = null
    region TriggerRegion = null
    boolean FirstSlope = false
endglobals

function Actions takes nothing returns nothing
    call BJDebugMsg("Test")
endfunction

function Init takes nothing returns nothing
    local trigger Trig = CreateTrigger()
    set TriggerRegion = CreateRegion()
    // set TriggerRect = gg_rct_Test
    call RegionAddRect(TriggerRegion, gg_rct_Test)
    call TriggerRegisterEnterRegion(Trig, TriggerRegion, null)
    call TriggerAddAction(Trig, function Actions)
endfunction

Why you have maked region variable global? Is it supposed to be used in other triggers?
 
Level 24
Joined
Feb 28, 2007
Messages
3,480
New help needed!

-->

Basically, now what I really wanted to do with this code, create a unit in front of the entering unit.
JASS:
scope Slope initializer Init
    globals
        region TriggerRegion = null
        boolean FirstSlope = false
        //-----
        real dist = 1.
        real angle = GetUnitFacing(GetEnteringUnit())
        location source = GetUnitLoc(GetEnteringUnit())
        location target = PolarProjectionBJ(source,dist,angle)
    endglobals

    function Actions takes nothing returns nothing
        call CreateUnitAtLoc(Player(0),'h000',target,0.)
    endfunction

    function Init takes nothing returns nothing
        local trigger Trig = CreateTrigger()
        set TriggerRegion = CreateRegion()
        call RegionAddRect(TriggerRegion, gg_rct_Test)
        call TriggerRegisterEnterRegion(Trig,TriggerRegion,null)
        call TriggerAddAction(Trig, function Actions)
    endfunction
endscope
According to what I thought, this would create a 'h000' directly in front of the entering unit, but the result is that the unit is created in front of the entering unit but in the very far end of the map. Any help is appreciated, thanks.
 
Level 14
Joined
Nov 23, 2008
Messages
187
The way you are setting parameters is definitely wrong. Values of global variables are "calculated" once, at the map start, while values of local variables are "calculated" on every function run. Of course, any event-specific functions like GetTriggerUnit() or GetSpellTargetLoc() would work only inside functions assigned to triggers.

Hence the code should look like:

JASS:
scope Slope initializer Init
    globals
        region TriggerRegion = null
        boolean FirstSlope = false
    endglobals

    function Actions takes nothing returns nothing
        local real dist = 1.
        local real angle = GetUnitFacing(GetEnteringUnit())
        local location source = GetUnitLoc(GetEnteringUnit())
        local location target = PolarProjectionBJ(source,dist,angle)
        call CreateUnitAtLoc(Player(0), 'h000', target, 0.)
        
        // cleaning up potential leaks
        call RemoveLocation(source)
        call RemoveLocation(target)
        set source = null
        set target = null
    endfunction

    function Init takes nothing returns nothing
        local trigger Trig = CreateTrigger()
        set TriggerRegion = CreateRegion()
        call RegionAddRect(TriggerRegion, gg_rct_Test)
        call TriggerRegisterEnterRegion(Trig,TriggerRegion,null)
        call TriggerAddAction(Trig, function Actions)
    endfunction
endscope

or optimized variant:

JASS:
scope Slope initializer Init
    globals
        region TriggerRegion = null
        boolean FirstSlope = false
    endglobals

    function Actions takes nothing returns nothing
        local unit e = GetEnteringUnit()
        local real f = GetUnitFacing(e) * bj_DEGTORAD
        call CreateUnit(Player(0), 'h000', GetUnitX(e) + 2.*Cos(f), GetUnitY(e) + 2.*Sin(f), 0.)
        set e = null
    endfunction

    function Init takes nothing returns nothing
        local trigger Trig = CreateTrigger()
        set TriggerRegion = CreateRegion()
        call RegionAddRect(TriggerRegion, gg_rct_Test)
        call TriggerRegisterEnterRegion(Trig,TriggerRegion,null)
        call TriggerAddAction(Trig, function Actions)
    endfunction
endscope

Well, it seems that you are not familiar with jass. I'd recommend to read some jass tutorials for better understanding.
 
Status
Not open for further replies.
Top