• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!
  • ✅ The POLL for Hive's Texturing Contest #34 is OPEN! Vote for the TOP 3 SKINS! 🔗Click here to cast your vote!
  • ✅ The POLL for Hive's Techtree Contest #20 is OPEN! Vote for the TOP 3 FACTIONS! 🔗Click here to cast your vote!

[vJASS] Non-static trigger condition inside a struct?

Status
Not open for further replies.
Is it not possible to use a non-static method as a condition for a trigger? This script gives me the error "this is not an struct name", about this.RallyPoint when adding it as a condition.

JASS:
   struct Barracks
        readonly unit barracks
        readonly group units = CreateGroup()
        readonly trigger trgRally = CreateTrigger()
        integer spawn_amount
        integer spawn_type
     
        private method RallyPoint takes nothing returns boolean
            if OrderId2String(GetIssuedOrderId()) == "setrally" then
                call GroupPointOrder(this.units, "attack", GetOrderPointX(), GetOrderPointY())
            endif
            return false
        endmethod
     
        public static method create takes unit barracks, integer spawn_amount, integer spawn_type returns Barracks
            local Barracks this = Barracks.allocate()
            local unit createdUnit
            local real x = GetUnitX(barracks) + 200
            local real y = GetUnitY(barracks) - 200
            local integer index = 0
         
            set this.barracks = barracks
            set this.spawn_amount = spawn_amount
            set this.spawn_type = spawn_type
         
            call UnitAddAbility(barracks, 'ARal')
            call IssuePointOrder(barracks, "setrally", x, y)
         
            loop
                set createdUnit = CreateUnit(GetOwningPlayer(barracks), spawn_type, x, y, bj_UNIT_FACING)
                call GroupAddUnit(this.units, createdUnit)
                set index = index + 1
                exitwhen index == spawn_amount
            endloop
         
            call TriggerRegisterUnitEvent(this.trgRally, barracks, EVENT_UNIT_ISSUED_POINT_ORDER)
            call TriggerAddCondition(this.trgRally, function this.RallyPoint)
         
            return this
        endmethod
     
        method destroy takes nothing returns nothing
            call DestroyGroup(this.units)
            call DestroyTrigger(this.trgRally)
            call this.deallocate()
        endmethod
    endstruct
 
Last edited:
In general, it isn't possible to use a non-static method as a callback function because callback functions in JASS must take no parameters. Non-static methods (a.k.a. instance methods) are compiled down into a function that takes an integer under the hood (that "integer" represents the Barracks instance).

The simplest way around this is to save that integer instance in a hashtable and then load it in the condition. You'll need to convert the condition into a static method as well to get around the compiler error. I don't remember the exact syntax, but it'd be something like this:
JASS:
// Change to static method
private static method RallyPoint takes nothing returns boolean
    // Load the instance
    local Barracks this = LoadInteger(hash, GetHandleId(GetTriggeringTrigger()), 0)
    
    if OrderId2String(GetIssuedOrderId()) == "setrally" then
        call GroupPointOrder(this.units, "attack", GetOrderPointX(), GetOrderPointY())
    endif
    return false
endmethod 

public static method create takes unit barracks, integer spawn_amount, integer spawn_type returns Barracks
    // ... 
    
    call TriggerRegisterUnitEvent(this.trgRally, barracks, EVENT_UNIT_ISSUED_POINT_ORDER)
    call TriggerAddCondition(this.trgRally, Condition(function thistype.RallyPoint)) // Use Condition(function thistype.<method name>)

    // Save the instance
    call SaveInteger(hash, GetHandleId(this.trgRally), 0, barracks)
     
    return this
endmethod
 
In general, it isn't possible to use a non-static method as a callback function because callback functions in JASS must take no parameters. Non-static methods (a.k.a. instance methods) are compiled down into a function that takes an integer under the hood (that "integer" represents the Barracks instance).
Thanks for the explanation! I'll try your solution soon, thanks!
 
Status
Not open for further replies.
Back
Top