Beginners' question concerning structs

Bronxernijn

You can change this now in User CP.
Reaction score
43
Dear people, I have a small problem. I am creating an object for use in the game (it is going to be expanded with other functions). This is the very first piece of JASS code I wrote. Somehow it does not do what it is supposed to (spawn a footman at the center of the region after 5 seconds). I think the event function is not totally correct. It is basically a converted GUI trigger. What did I do wrong?

JASS:
struct Capt
    real SizeCor = 12
    region Reg = CreateRegion()
    static method Create takes integer x, integer y returns Capt
        local Capt c = Capt.allocate()
        local rect r = Rect((x-c.SizeCor), (y-c.SizeCor), (x+c.SizeCor), (y+c.SizeCor))
        call RegionAddRect(c.Reg, r)
        call CreateNUnitsAtLoc(1, 'hfoo', Player(0), GetRectCenter(r), 0)
        return c
    endmethod
endstruct

function CaptInit takes nothing returns nothing
    local integer x = 30
    local integer y = 30
    local Capt capturable = Capt.Create(x, y)
endfunction
        
function CaptEvent takes nothing returns nothing
    local trigger c = gg_trg_Capturables
    call TriggerRegisterTimerEventSingle(c, 5)
    call TriggerAddAction(c, function CaptInit)
endfunction
 

Flare

Stops copies me!
Reaction score
662
[ljass]local trigger c = gg_trg_Capturables[/ljass]
gg_trg_Capturables is not initialized (it's never set to CreateTrigger, which is usually done in the Init function for that trigger)

Change it to [ljass]local trigger c = CreateTrigger ()[/ljass] and it should work.

Also, the location parameter that you are giving for CreateNUnitsAtLoc should be set to a variable ([ljass]local location l = GetRectCenter (r)[/ljass], then destroyed after it has been used, with [ljass]call RemoveLocation (l)[/ljass] so, you'd end up with something like
JASS:
    static method Create takes integer x, integer y returns Capt
        local Capt c = Capt.allocate()
        local rect r = Rect((x-c.SizeCor), (y-c.SizeCor), (x+c.SizeCor), (y+c.SizeCor))
        local location l = GetRectCenter (r) //if you're using NewGen, you might want to find what functions GetRectCenter calls, and replace this BJ with it's native equivalent
        call RegionAddRect(c.Reg, r)
        call CreateNUnitsAtLoc(1, 'hfoo', Player(0), l, 0)
        call RemoveLocation (l)
        set l = null
        return c
    endmethod


(Not really sure whether or not it's safe to destroy a rect after it's been added to a region though, so that might be a leak but I amn't 100% certain)
 

Bronxernijn

You can change this now in User CP.
Reaction score
43
Yes I know it is not yet leak-free, but I will work on that later. First I want to get my first piece of real code to work :)

I did as you told me (replaced the trigger stuff with "CreateTrigger()". But it still doesnt work. My code now looks like this:
JASS:
struct Capt
    real SizeCor = 12
    region Reg = CreateRegion()
    static method Create takes integer x, integer y returns Capt
        local Capt c = Capt.allocate()
        local rect r = Rect((x-c.SizeCor), (y-c.SizeCor), (x+c.SizeCor), (y+c.SizeCor))
        call RegionAddRect(c.Reg, r)
        call CreateNUnitsAtLoc(1, 'hfoo', Player(0), GetRectCenter(r), 0)
        call DisplayTextToForce(GetPlayersAll(), "I am getting here!")
        return c
    endmethod
endstruct

function CaptInit takes nothing returns nothing
    local integer x = 30
    local integer y = 30
    local Capt capturable = Capt.Create(x, y)
endfunction

function CaptEvent takes nothing returns nothing
    local trigger c = CreateTrigger()
    call TriggerRegisterTimerEventSingle(c, 5)
    call TriggerAddAction(c, function CaptInit)
endfunction


Problem is that there is a lot that can be wrong I think. However, the text line I inserted does not show up, so my guess is the event as a whole does not run.

And yes, I am using NewGen.
 

Viikuna

No Marlo no game.
Reaction score
265
Yep. Unlike functions names, method names usually begin with lower case letter.

[lJASS]call CreateSomething() /* and */ call something.create() [/lJASS]
 

Artificial

Without Intelligence
Reaction score
326
JASS:

scope Captielol initializer CaptEvent // added
    struct Capt
        real SizeCor = 12
        region Reg = CreateRegion()
        static method Create takes integer x, integer y returns Capt
            local Capt c = Capt.allocate()
            local rect r = Rect((x-c.SizeCor), (y-c.SizeCor), (x+c.SizeCor), (y+c.SizeCor))
            call RegionAddRect(c.Reg, r)
            call CreateNUnitsAtLoc(1, 'hfoo', Player(0), GetRectCenter(r), 0)
            call DisplayTextToForce(GetPlayersAll(), "I am getting here!")
            return c
        endmethod
    endstruct
    
    function CaptInit takes nothing returns nothing
        local integer x = 30
        local integer y = 30
        local Capt capturable = Capt.Create(x, y)
    endfunction
    
    function CaptEvent takes nothing returns nothing
        local trigger c = CreateTrigger()
        call TriggerRegisterTimerEventSingle(c, 5)
        call TriggerAddAction(c, function CaptInit)
    endfunction
endscope // added

You didn't seem to have anything that was supposed to run CaptEvent (WC and JassHelper can't automagically guess which functions you want to run at map init), so I added something that makes JH know it should be ran. =)
 

Bronxernijn

You can change this now in User CP.
Reaction score
43
Thanks, the scope did it :)

I thought I wouldn't need it because converted GUI functions don't have them :p
What I learned today is that you create your triggers in the code.

EDIT: ok, an update. Sorry guys I feel bad for asking this much, I almost feel like asking for DotA spells. This is the code I have now:
JASS:
(Outdated)


Basically what it does is create a unit with a region around it, when it leaves that region it dies (just a test trigger). Is there a way to code this more efficient? Don't hold back, I'm used to C++ programming :p

For example, I would like to put the kill trigger inside the struct. How do I do this?

EDIT2: fixed it :D
JASS:
scope Captables initializer CaptInit
    
    struct Capt
        
        real SizeCor = 12
        region Reg = CreateRegion()

        static method UnitKill takes nothing returns nothing
            call KillUnit(GetTriggerUnit())
        endmethod
        
        static method create takes integer x, integer y returns Capt
            local Capt c = Capt.allocate()
            local rect r = Rect((x-c.SizeCor), (y-c.SizeCor), (x+c.SizeCor), (y+c.SizeCor))
            local trigger c1 = CreateTrigger()
            call RegionAddRect(c.Reg, r)
            call CreateUnit(Player(0), 'hfoo', x, y, 0)
            call TriggerRegisterLeaveRegion(c1, c.Reg, null)
            call TriggerAddAction(c1, function Capt.UnitKill)
            return c
        endmethod
            
    endstruct
    
    function CaptInit takes nothing returns nothing
        local integer x = 30
        local integer y = 30
        local Capt capturable = Capt.create(x, y)
    endfunction

endscope
 

ZugZugZealot

New Member
Reaction score
33
JASS:
call TriggerAddAction( trig, function Capt.UnitKill )



Towards the Init thing... There's a good part of code you don't see that blizzard has... Like C/C++ it starts with function main(). In main, it calls RunInitializationTriggers(). When you add Map Initialization to a trigger's events in GUI, it puts a call to InitTrig_<name> in RunInitializationTrigger(). When you do initializer, it puts the pointed function to be call from the main() function.
 

ZugZugZealot

New Member
Reaction score
33
If you want EVERYTHING of a trigger inside a struct... It would look something like this...


JASS:
struct SomeStruct
    static trigger trig
    
    static method Actions takes nothing returns nothing
        call KillUnit( GetTriggerUnit() )
    endmethod
    
    static method onInit takes nothing returns nothing
        local integer i = 0
        
        set .trig = CreateTrigger()
        loop
            exitwhen i &gt; 11
            call TriggerRegisterPlayerUnitEvent( .trig, Player(i), EVENT_PLAYER_UNIT_SELECTED, null )
            set i = i + 1
        endloop
        call TriggerAddAction( .trig, function SomeStruct.Actions )
    endmethod
endstruct



For code parameters, you can't use [ljass]function .<method name>[/ljass], you gotta use [ljass]function <struct name>.<method name>[/ljass]
 

Bronxernijn

You can change this now in User CP.
Reaction score
43
I have another question: boolexpr functions cannot take arguments. But how do I check whether my unit is in a group using only locals (by the way Size is a global)? code:
Edit:nvm
 

ZugZugZealot

New Member
Reaction score
33
Just like "GetTriggerUnit()" in actions.

In a boolexpr function you can do like...
JASS:
if ( GetUnitTypeId(GetFilterUnit()) == &#039;xxxx&#039; ) then


If you mean checking to see if a unit is in a unit group, then it would be [ljass]IsUnitInGroup(unit whichUnit, group whichGroup )[/ljass]
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top