Methods

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
I am having issues with calling methods from within other methods. Am I retarded for thinking this is possible?

JASS:
private struct hero
        unit hero
        unit dummy
        trigger pickup
        string state
        
        method Pickup takes nothing returns nothing
        endmethod
        
        static method create takes player p, integer id, real x, real y, real face returns hero
            local hero h=hero.allocate()
            set h.hero=CreateUnit(p,id,x,y,face)
            set h.dummy=CreateUnit(p,DummyId,GetRectMaxX(bj_mapInitialPlayableArea),GetRectMaxY(bj_mapInitialPlayableArea),0.)
            call SetUnitScale(h.dummy,.001,.001,.001)
            call SetUnitInvulnerable(h.dummy,true)
            call UnitAddAbility(h.dummy,'AInv')
            set h.pickup=CreateTrigger()
            call TriggerRegisterUnitEvent(h.pickup,h.hero,EVENT_UNIT_PICKUP_ITEM)
            call TriggerAddAction(h.pickup,hero.Pickup)
            set h.state="Main"
            call GroupAddUnit(HeroGroup,h.hero)
            return h
        endmethod
        
        method setstate takes string state returns nothing
            set this.state=state
        endmethod
        
        method getstate takes nothing returns string
            return this.state
        endmethod
        
        method onDestroy takes nothing returns nothing
            call GroupRemoveUnit(HeroGroup,this.hero)
            call RemoveUnit(this.hero)
            call RemoveUnit(this.dummy)
        endmethod
        
        item array inventory [6]
        
        method getitem takes integer which returns item
            return this.inventory[which]
        endmethod
        
    endstruct


Gives me: "Undeclared variable Bag3__hero" and "Syntax error" on the TriggerAddAction line.

My guess is it's somehow not recognizing the method I want to call.

Side note: What does static mean? It doesn't seem to do anything... but create must be static...
 

SFilip

Gone but not forgotten
Reaction score
634
Only static methods can be used as code.
Static methods are methods which aren't specific to some instance of a struct.
Quick example:
Code:
    local mystruct test = mystruct.create()
    test.dosomething() // this is a standard method
    mystruct.iamstatic() // this is a static method
Notice that the first is called for that specific variable called "test" while the second is called for the struct type itself.
When it comes to the way they are used static methods aren't too far from normal function calls, but they are handled differently (through a trigger) so only use them when you want to be able to call something from any part of your code (as opposed to functions which can't be called above the definition).
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
To get the struct instance in the static method, do I need to pass the struct, or can I use this.blah ?
 

SFilip

Gone but not forgotten
Reaction score
634
You need to pass it somehow, no this in statics.
But this is where the problem comes, you can't attach anything to the trigger since every method is already a part of a dummy trigger already.
Once again using a normal function is recommended for this.
 

Waaaaagh

I lost all my rep and my title being a jerk
Reaction score
70
Nah, I think I'll use a static method, since I can keep everything inside one struct. Thnx SFlip.
 
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