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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top